emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/vertico e8a3e96 16/48: Minor optimization


From: Stefan Monnier
Subject: [elpa] externals/vertico e8a3e96 16/48: Minor optimization
Date: Mon, 5 Apr 2021 10:54:41 -0400 (EDT)

branch: externals/vertico
commit e8a3e965bdb781b96f9bfbfd5205379af0ffd567
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>

    Minor optimization
---
 minicomp.el | 56 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 33 insertions(+), 23 deletions(-)

diff --git a/minicomp.el b/minicomp.el
index 98c1ca6..5e6597e 100644
--- a/minicomp.el
+++ b/minicomp.el
@@ -128,33 +128,43 @@
   ;; Store the history position first in a hashtable in order to allow O(1) 
history lookup. File
   ;; names get special treatment. In principle, completion tables with 
boundaries should also get
   ;; special treatment, but files are the most important.
-  (let ((index 0)
-        ;; History disabled if `minibuffer-history-variable' eq `t'.
-        (hist (and (not (eq minibuffer-history-variable t))
-                   (symbol-value minibuffer-history-variable))))
-    (if (eq minibuffer-history-variable 'file-name-history)
-      (let* ((dir (expand-file-name (substitute-in-file-name
-                                     (or (file-name-directory input)
-                                         default-directory))))
-             (adir (abbreviate-file-name dir)))
-        (unless (equal minicomp--history-dir dir)
-          (setq minicomp--history-hash (make-hash-table :test #'equal :size 
(length hist))
-                minicomp--history-dir dir)
-          (dolist (elem hist)
-            (when-let (file (cond
-                             ((string-prefix-p dir elem) (substring elem 
(length dir)))
-                             ((string-prefix-p adir elem) (substring elem 
(length adir)))))
+  (cond
+   ((eq minibuffer-history-variable 'file-name-history)
+    (let ((dir (expand-file-name (substitute-in-file-name
+                                  (or (file-name-directory input)
+                                      default-directory)))))
+      (unless (equal minicomp--history-dir dir)
+        (setq minicomp--history-hash (make-hash-table :test #'equal :size 
(length file-name-history))
+              minicomp--history-dir dir)
+        (let* ((index 0)
+               (adir (abbreviate-file-name dir))
+               (dlen (length dir))
+               (alen (length adir)))
+          (dolist (elem file-name-history)
+            (let* ((len (length elem))
+                   (file (cond
+                          ((and (> len dlen)
+                                (eq t (compare-strings dir 0 dlen elem 0 
dlen)))
+                           (substring elem dlen))
+                          ((and (> len alen)
+                                (eq t (compare-strings adir 0 alen elem 0 
alen)))
+                           (substring elem alen)))))
+            (when file
               (when-let (slash (string-match-p "/" file))
                 (setq file (substring file 0 (1+ slash))))
               (unless (gethash file minicomp--history-hash)
                 (puthash file index minicomp--history-hash)))
-            (setq index (1+ index)))))
-      (unless minicomp--history-hash
-        (setq minicomp--history-hash (make-hash-table :test #'equal :size 
(length hist)))
-        (dolist (elem hist)
-          (unless (gethash elem minicomp--history-hash)
-            (puthash elem index minicomp--history-hash))
-          (setq index (1+ index))))))
+            (setq index (1+ index))))))))
+   ((not minicomp--history-hash)
+    (let ((index 0)
+          ;; History disabled if `minibuffer-history-variable' eq `t'.
+          (hist (and (not (eq minibuffer-history-variable t))
+                     (symbol-value minibuffer-history-variable))))
+      (setq minicomp--history-hash (make-hash-table :test #'equal :size 
(length hist)))
+      (dolist (elem hist)
+        (unless (gethash elem minicomp--history-hash)
+          (puthash elem index minicomp--history-hash))
+        (setq index (1+ index))))))
   ;; Decorate each candidate with (index<<13) + length. This way we sort first 
by index and then by
   ;; length. We assume that the candidates are shorter than 2**13 characters 
and that the history is
   ;; shorter than 2**16 entries.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]