GNU bug report logs - #1229
generate-new-buffer-name could be more efficient

Previous Next

Package: emacs;

Reported by: Glenn Morris <rgm <at> gnu.org>

Date: Thu, 23 Oct 2008 07:10:04 UTC

Severity: wishlist

Found in version 23.0.60

Fixed in version 24.2

Done: Glenn Morris <rgm <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 1229 in the body.
You can then email your comments to 1229 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report stored:
bug#1229; Package emacs. Full text and rfc822 format available.

Message #3 received at quiet <at> emacsbugs.donarmstrong.com (full text, mbox):

From: Glenn Morris <rgm <at> gnu.org>
To: quiet <quiet <at> debbugs.gnu.org>
Subject: generate-new-buffer-name could be more efficient
Date: Thu, 23 Oct 2008 02:58:49 -0400
Package: emacs
Severity: wishlist
Version: 23.0.60

As mentioned in bug#122, generate-new-buffer-name could be more
efficient for invisible buffers:

http://debbugs.gnu.org/cgi/bugreport.cgi?bug=122#182

    Oh, yes, of course. 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.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#1229; Package emacs. (Tue, 03 Jul 2012 07:38:02 GMT) Full text and rfc822 format available.

Message #6 received at 1229 <at> debbugs.gnu.org (full text, mbox):

From: Glenn Morris <rgm <at> gnu.org>
To: 1229 <at> debbugs.gnu.org
Subject: Re: bug#1229: generate-new-buffer-name could be more efficient
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;





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#1229; Package emacs. (Tue, 03 Jul 2012 14:04:02 GMT) Full text and rfc822 format available.

Message #9 received at 1229 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Glenn Morris <rgm <at> gnu.org>
Cc: 1229 <at> debbugs.gnu.org
Subject: Re: bug#1229: generate-new-buffer-name could be more efficient
Date: Tue, 03 Jul 2012 09:58:30 -0400
> Something like the following?

Yes.

> +       sprintf (number, "-%"pD"d", Frandom (make_number (999999)));

I don't like this arbitrary constant.  Maybe we could use something
like "N * Flength (Vbuffer_alist)".



        Stefan




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#1229; Package emacs. (Tue, 03 Jul 2012 17:26:01 GMT) Full text and rfc822 format available.

Message #12 received at 1229 <at> debbugs.gnu.org (full text, mbox):

From: Glenn Morris <rgm <at> gnu.org>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: 1229 <at> debbugs.gnu.org
Subject: Re: bug#1229: generate-new-buffer-name could be more efficient
Date: Tue, 03 Jul 2012 13:21:12 -0400
Stefan Monnier wrote:

>> +       sprintf (number, "-%"pD"d", Frandom (make_number (999999)));
>
> I don't like this arbitrary constant.  Maybe we could use something
> like "N * Flength (Vbuffer_alist)".

OK; though personally I don't see that it matters. IIUC, mkstemp,
fileio.c's make_temp_name, etc, all use a finite number of possible
random states, and they don't have the <2>... fallback that
generate-new-buffer-name does.

(Another option is to extract the random scheme that make_temp_name uses
to a separate function and use that here too.)

What do you want the arbitrary constant N to be ? :)




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#1229; Package emacs. (Tue, 03 Jul 2012 17:32:02 GMT) Full text and rfc822 format available.

Message #15 received at 1229 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Glenn Morris <rgm <at> gnu.org>
Cc: 1229 <at> debbugs.gnu.org
Subject: Re: bug#1229: generate-new-buffer-name could be more efficient
Date: Tue, 03 Jul 2012 13:27:10 -0400
>> Something like the following?
> Yes.
>> +       sprintf (number, "-%"pD"d", Frandom (make_number (999999)));
> I don't like this arbitrary constant.  Maybe we could use something
> like "N * Flength (Vbuffer_alist)".

Not sure what I was smoking, sorry, just ignore that comment.


        Stefan




bug marked as fixed in version 24.2, send any further explanations to 1229 <at> debbugs.gnu.org and Glenn Morris <rgm <at> gnu.org> Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Tue, 03 Jul 2012 17:55:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Wed, 01 Aug 2012 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 12 years and 330 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.