unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Glenn Morris <rgm@gnu.org>
To: 1229@debbugs.gnu.org
Subject: bug#1229: generate-new-buffer-name could be more efficient
Date: Tue, 03 Jul 2012 03:33:16 -0400	[thread overview]
Message-ID: <7bojxfgeb.fsf@fencepost.gnu.org> (raw)
In-Reply-To: <e3tzb3pzkm.fsf@fencepost.gnu.org>


Stefan Monnier wrote:

>  When generate-new-buffer-name is called for a user-visible buffer,
>  fixing this would be probably too much trouble for too little
>  benefit. But for internal buffers, whose precise name doesn't
>  actually matter, we should use a different strategy, where we
>  immediately start by adding a random suffix to the buffer name, so as
>  to avoid conflicts.

Something like the following? Timing results for me with this change:

(let ((start (current-time)))
  (dotimes (i 500)
    (generate-new-buffer "a"))
  (float-time (time-since start)))

-> 0.55 seconds (and 4.3 seconds the next time)

Repeating with " a" instead of "a", it takes 0.025 seconds.


*** src/lisp.h	2012-06-30 09:13:54 +0000
--- src/lisp.h	2012-07-03 02:41:45 +0000
***************
*** 2473,2478 ****
--- 2473,2479 ----
  EXFUN (Fremhash, 2);
  
  EXFUN (Fidentity, 1);
+ EXFUN (Frandom, 1);
  EXFUN (Flength, 1);
  EXFUN (Fappend, MANY);
  EXFUN (Fconcat, MANY);

*** src/buffer.c	2012-07-03 03:57:52 +0000
--- src/buffer.c	2012-07-03 07:24:11 +0000
***************
*** 838,847 ****
  Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
  \(starting at 2) until an unused name is found, and then return that name.
  Optional second argument IGNORE specifies a name that is okay to use (if
! it is in the sequence to be tried) even if a buffer with that name exists.  */)
    (register Lisp_Object name, Lisp_Object ignore)
  {
!   register Lisp_Object gentemp, tem;
    ptrdiff_t count;
    char number[INT_BUFSIZE_BOUND (ptrdiff_t) + sizeof "<>"];
  
--- 838,852 ----
  Otherwise modify name by appending `<NUMBER>', incrementing NUMBER
  \(starting at 2) until an unused name is found, and then return that name.
  Optional second argument IGNORE specifies a name that is okay to use (if
! it is in the sequence to be tried) even if a buffer with that name exists.
! 
! If NAME begins with a space (i.e., a buffer that is not normally
! visible to users), then for efficiency reasons if buffer NAME
! already exists a random number is first appended to NAME, to speed
! up finding a new buffer.  */)
    (register Lisp_Object name, Lisp_Object ignore)
  {
!   register Lisp_Object gentemp, tem, tem2;
    ptrdiff_t count;
    char number[INT_BUFSIZE_BOUND (ptrdiff_t) + sizeof "<>"];
  
***************
*** 854,864 ****
    if (NILP (tem))
      return name;
  
    count = 1;
    while (1)
      {
        sprintf (number, "<%"pD"d>", ++count);
!       gentemp = concat2 (name, build_string (number));
        tem = Fstring_equal (gentemp, ignore);
        if (!NILP (tem))
  	return gentemp;
--- 859,880 ----
    if (NILP (tem))
      return name;
  
+   if (!strncmp (SSDATA (name), " ", 1))
+     {
+       sprintf (number, "-%"pD"d", Frandom (make_number (999999)));
+       tem2 = concat2 (name, build_string (number));
+       tem = Fget_buffer (tem2);
+       if (NILP (tem))
+ 	return tem2;
+     }
+   else
+     tem2 = name;
+ 
    count = 1;
    while (1)
      {
        sprintf (number, "<%"pD"d>", ++count);
!       gentemp = concat2 (tem2, build_string (number));
        tem = Fstring_equal (gentemp, ignore);
        if (!NILP (tem))
  	return gentemp;






       reply	other threads:[~2012-07-03  7:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <e3tzb3pzkm.fsf@fencepost.gnu.org>
2012-07-03  7:33 ` Glenn Morris [this message]
2012-07-03 13:58   ` bug#1229: generate-new-buffer-name could be more efficient Stefan Monnier
2012-07-03 17:21     ` Glenn Morris
2012-07-03 17:27     ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7bojxfgeb.fsf@fencepost.gnu.org \
    --to=rgm@gnu.org \
    --cc=1229@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).