Subject: Re: bug#1229: generate-new-buffer-name could be more efficient

From: Glenn Morris <[email-masked]>
Date: Tue, 03 Jul 2012 03:33:16 -0400

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;