Commit b2f1b848 authored by Francesco Potortì's avatar Francesco Potortì
Browse files

Overall speedup when using many buffers.

(uniquify-fix-item-base, uniquify-fix-item-filename,
uniquify-fix-item-buffer): Changed defmacro to defalias (cosmetic change).
(uniquify-fix-item-unrationalized-buffer): Deleted: was the fourth
place in the item, but waas never used.
(uniquify-fix-item-min-proposed): New defalias: the fourth place
in the item is now used as cache for the proposed name.
(uniquify-rationalize-file-buffer-names): Move computation made on
newbuffile out of the loop, in the newbuffile-nd local var.  Use
dolist (cosmetic change).  Compute the proposed name for the most
common case and cache it in the fourth place in the item.
(uniquify-rationalize-file-buffer-names): Used to return a list
of flags indicating renamed buffers, but that return value was
never used.
(uniquify-item-lessp): Replaces uniquify-filename-lessp, works on
the cached proposed name, does much less consing and is quicker.
(uniquify-filename-lessp): Deleted.
(uniquify-rationalize-a-list): Use dolist (cosmetic change).  Do
not bind locally the uniquify-possibly-resolvable flag.  Use the
cached proposed name if possible.
(uniquify-get-proposed-name): Arguments changed, callers changed.
(uniquify-rationalize-conflicting-sublist): Explicitely reset the
uniquify-possibly-resolvable flag, which is no more bound locally.
(uniquify-rename-buffer): Do not set the unrationalised-buffer
flag, which is replaced by the cached proposed name.
parent be8cf99e
...@@ -74,6 +74,8 @@ ...@@ -74,6 +74,8 @@
;; Andre Srinivasan <andre@visigenic.com> 9 Sep 97 ;; Andre Srinivasan <andre@visigenic.com> 9 Sep 97
;; Add uniquify-list-buffers-directory-modes ;; Add uniquify-list-buffers-directory-modes
;; Stefan Monnier <monnier@cs.yale.edu> 17 Nov 2000 ;; Stefan Monnier <monnier@cs.yale.edu> 17 Nov 2000
;; Cleanup of uniquify-*-lessp reduced consing when using lots of buffers
;; Francesco Potort <pot@gnu.org> (ideas by rms and monnier) 2001-07-18
;; Valuable feedback was provided by ;; Valuable feedback was provided by
;; Paul Smith <psmith@baynetworks.com>, ;; Paul Smith <psmith@baynetworks.com>,
...@@ -171,19 +173,10 @@ contains the name of the directory which the buffer is visiting.") ...@@ -171,19 +173,10 @@ contains the name of the directory which the buffer is visiting.")
(file-name-nondirectory (directory-file-name file-name))) (file-name-nondirectory (directory-file-name file-name)))
;; uniquify-fix-list data structure ;; uniquify-fix-list data structure
(defmacro uniquify-fix-item-base (a) (defalias 'uniquify-fix-item-base 'car)
`(car ,a)) (defalias 'uniquify-fix-item-filename 'cadr)
(defmacro uniquify-fix-item-filename (a) (defsubst uniquify-fix-item-buffer (x) (car (cdr (cdr x))))
`(car (cdr ,a))) (defsubst uniquify-fix-item-min-proposed (x) (nth 3 x))
(defmacro uniquify-fix-item-buffer (a)
`(car (cdr (cdr ,a))))
;; Not a macro: passed to mapcar.
(defun uniquify-fix-item-unrationalized-buffer (item)
(or (car (cdr (cdr (cdr item)))) nil)) ;maybe better in the future
(defun uniquify-fix-item-filename-lessp (fixlist1 fixlist2)
(uniquify-filename-lessp (uniquify-fix-item-filename fixlist1)
(uniquify-fix-item-filename fixlist2)))
;; Internal variables used free ;; Internal variables used free
(defvar uniquify-non-file-buffer-names nil) (defvar uniquify-non-file-buffer-names nil)
...@@ -197,37 +190,36 @@ If `uniquify-min-dir-content' > 0, always pulls that many ...@@ -197,37 +190,36 @@ If `uniquify-min-dir-content' > 0, always pulls that many
file name elements. Arguments cause only a subset of buffers to be renamed." file name elements. Arguments cause only a subset of buffers to be renamed."
(interactive) (interactive)
(let (fix-list (let (fix-list
uniquify-non-file-buffer-names) uniquify-non-file-buffer-names
(let ((buffers (buffer-list))) (newbuffile-nd (and newbuffile
(while buffers (uniquify-file-name-nondirectory newbuffile))))
(let* ((buffer (car buffers)) (dolist (buffer (buffer-list))
(bfn (if (eq buffer newbuf) (let* ((bfn (if (eq buffer newbuf)
(and newbuffile (and newbuffile
(expand-file-name (expand-file-name
(if (file-directory-p newbuffile) (if (file-directory-p newbuffile)
(directory-file-name newbuffile) (directory-file-name newbuffile)
newbuffile))) newbuffile)))
(uniquify-buffer-file-name buffer))) (uniquify-buffer-file-name buffer)))
(rawname (and bfn (uniquify-file-name-nondirectory bfn))) (rawname (and bfn (uniquify-file-name-nondirectory bfn)))
(deserving (and rawname (deserving (and rawname
(not (and uniquify-ignore-buffers-re (not (and uniquify-ignore-buffers-re
(string-match uniquify-ignore-buffers-re (string-match uniquify-ignore-buffers-re
(buffer-name buffer)))) (buffer-name buffer))))
(or (not newbuffile) (or (not newbuffile)
(equal rawname (equal rawname newbuffile-nd))))
(uniquify-file-name-nondirectory newbuffile)))))) (min-proposed (if deserving
(if deserving (uniquify-get-proposed-name
(push (list rawname bfn buffer nil) fix-list) rawname bfn uniquify-min-dir-content))))
(push (list (buffer-name buffer)) (if deserving
uniquify-non-file-buffer-names))) (push (list rawname bfn buffer min-proposed) fix-list)
(setq buffers (cdr buffers)))) (push (list (buffer-name buffer)) uniquify-non-file-buffer-names))))
;; selects buffers whose names may need changing, and others that ;; selects buffers whose names may need changing, and others that
;; may conflict. ;; may conflict.
(setq fix-list (setq fix-list
(sort fix-list 'uniquify-fix-item-filename-lessp)) (sort fix-list 'uniquify-item-lessp))
;; bringing conflicting names together ;; bringing conflicting names together
(uniquify-rationalize-a-list fix-list uniquify-min-dir-content) (uniquify-rationalize-a-list fix-list uniquify-min-dir-content)))
(mapcar 'uniquify-fix-item-unrationalized-buffer fix-list)))
;; uniquify's version of buffer-file-name; result never contains trailing slash ;; uniquify's version of buffer-file-name; result never contains trailing slash
(defun uniquify-buffer-file-name (buffer) (defun uniquify-buffer-file-name (buffer)
...@@ -249,45 +241,35 @@ in `uniquify-list-buffers-directory-modes', otherwise returns nil." ...@@ -249,45 +241,35 @@ in `uniquify-list-buffers-directory-modes', otherwise returns nil."
(car dired-directory) (car dired-directory)
dired-directory))))))))) dired-directory)))))))))
;; This examines the filename components in reverse order. (defun uniquify-item-lessp (item1 item2)
(defun uniquify-filename-lessp (s1 s2) (string-lessp (uniquify-fix-item-min-proposed item1)
(let ((s1f (uniquify-file-name-nondirectory s1)) (uniquify-fix-item-min-proposed item2)))
(s2f (uniquify-file-name-nondirectory s2)))
(and (not (equal s2f ""))
(or (string-lessp s1f s2f)
(and (equal s1f s2f)
(let ((s1d (file-name-directory s1))
(s2d (file-name-directory s2)))
(and (not (<= (length s2d) 1))
(or (<= (length s1d) 1)
(uniquify-filename-lessp
(substring s1d 0 -1)
(substring s2d 0 -1))))))))))
(defun uniquify-rationalize-a-list (fix-list depth) (defun uniquify-rationalize-a-list (fix-list depth)
(let (conflicting-sublist ; all elements have the same proposed name (let (conflicting-sublist ; all elements have the same proposed name
(old-name "") (old-name "")
proposed-name uniquify-possibly-resolvable) proposed-name)
(while fix-list (dolist (item fix-list)
(let ((item (car fix-list))) (setq proposed-name
(setq proposed-name (uniquify-get-proposed-name item depth)) (if (= depth uniquify-min-dir-content)
(if (not (equal proposed-name old-name)) (uniquify-fix-item-min-proposed item)
(progn (uniquify-get-proposed-name (uniquify-fix-item-base item)
(uniquify-rationalize-conflicting-sublist (uniquify-fix-item-filename item)
conflicting-sublist old-name depth) depth)))
(setq conflicting-sublist nil))) (unless (equal proposed-name old-name)
(push item conflicting-sublist) (uniquify-rationalize-conflicting-sublist conflicting-sublist
(setq old-name proposed-name)) old-name depth)
(setq fix-list (cdr fix-list))) (setq conflicting-sublist nil))
(push item conflicting-sublist)
(setq old-name proposed-name))
(uniquify-rationalize-conflicting-sublist (uniquify-rationalize-conflicting-sublist
conflicting-sublist old-name depth))) conflicting-sublist old-name depth)))
(defun uniquify-get-proposed-name (item depth) (defun uniquify-get-proposed-name (base filename depth)
(let (index (let (index
(extra-string "") (extra-string "")
(n depth) (fn filename)
(base (uniquify-fix-item-base item)) (n depth))
(fn (uniquify-fix-item-filename item)))
(while (and (> n 0) (while (and (> n 0)
(setq index (string-match (setq index (string-match
(concat "\\(^\\|/[^/]*\\)/" (concat "\\(^\\|/[^/]*\\)/"
...@@ -348,8 +330,9 @@ in `uniquify-list-buffers-directory-modes', otherwise returns nil." ...@@ -348,8 +330,9 @@ in `uniquify-list-buffers-directory-modes', otherwise returns nil."
(or (and (not (string= old-name "")) (or (and (not (string= old-name ""))
(uniquify-rename-buffer (car conflicting-sublist) old-name)) (uniquify-rename-buffer (car conflicting-sublist) old-name))
t)) t))
(if uniquify-possibly-resolvable (when uniquify-possibly-resolvable
(uniquify-rationalize-a-list conflicting-sublist (1+ depth))))) (setq uniquify-possibly-resolvable nil)
(uniquify-rationalize-a-list conflicting-sublist (1+ depth)))))
(defun uniquify-rename-buffer (item newname) (defun uniquify-rename-buffer (item newname)
(let ((buffer (uniquify-fix-item-buffer item))) (let ((buffer (uniquify-fix-item-buffer item)))
...@@ -359,8 +342,7 @@ in `uniquify-list-buffers-directory-modes', otherwise returns nil." ...@@ -359,8 +342,7 @@ in `uniquify-list-buffers-directory-modes', otherwise returns nil."
(uniquify-buffer-name-style nil)) (uniquify-buffer-name-style nil))
(set-buffer buffer) (set-buffer buffer)
(rename-buffer newname) (rename-buffer newname)
(set-buffer unset)))) (set-buffer unset)))))
(rplaca (nthcdr 3 item) t))
(defun uniquify-reverse-components (instring) (defun uniquify-reverse-components (instring)
(let ((sofar '()) (let ((sofar '())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment