From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Glenn Morris Newsgroups: gmane.emacs.bugs Subject: bug#1229: generate-new-buffer-name could be more efficient Date: Tue, 03 Jul 2012 03:33:16 -0400 Message-ID: <7bojxfgeb.fsf@fencepost.gnu.org> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1341300813 615 80.91.229.3 (3 Jul 2012 07:33:33 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 3 Jul 2012 07:33:33 +0000 (UTC) To: 1229@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Jul 03 09:33:32 2012 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Slxc3-0007TY-Bj for geb-bug-gnu-emacs@m.gmane.org; Tue, 03 Jul 2012 09:33:31 +0200 Original-Received: from localhost ([::1]:60861 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Slxc2-0004lL-Ch for geb-bug-gnu-emacs@m.gmane.org; Tue, 03 Jul 2012 03:33:30 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:36360) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Slxbw-0004lD-Ju for bug-gnu-emacs@gnu.org; Tue, 03 Jul 2012 03:33:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Slxbu-0004nN-Ok for bug-gnu-emacs@gnu.org; Tue, 03 Jul 2012 03:33:24 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:34635) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Slxbu-0004nH-Kt for bug-gnu-emacs@gnu.org; Tue, 03 Jul 2012 03:33:22 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.72) (envelope-from ) id 1SlxgQ-0006KP-2j for bug-gnu-emacs@gnu.org; Tue, 03 Jul 2012 03:38:02 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: Resent-From: Glenn Morris Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 03 Jul 2012 07:38:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 1229 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 1229-submit@debbugs.gnu.org id=B1229.134130107924317 (code B ref 1229); Tue, 03 Jul 2012 07:38:02 +0000 Original-Received: (at 1229) by debbugs.gnu.org; 3 Jul 2012 07:37:59 +0000 Original-Received: from localhost ([127.0.0.1]:44181 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1SlxgN-0006K9-92 for submit@debbugs.gnu.org; Tue, 03 Jul 2012 03:37:59 -0400 Original-Received: from fencepost.gnu.org ([208.118.235.10]:42224) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1SlxgL-0006K3-Vk for 1229@debbugs.gnu.org; Tue, 03 Jul 2012 03:37:58 -0400 Original-Received: from rgm by fencepost.gnu.org with local (Exim 4.71) (envelope-from ) id 1Slxbo-0002Fz-Hp; Tue, 03 Jul 2012 03:33:16 -0400 X-Spook: Operation Iraqi Freedom CDC smuggle Forte Treasury X-Ran: cxX'U-j9==.1R^63Y@aFFjz-Y3tbk3r|S_bMX; List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:61521 Archived-At: 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 `', 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 `', 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;