branch: elpa/projectile
commit b5936d67c70f5d18094d31eb488304a9a0be8d16
Author: Bozhidar Batsov <[email protected]>
Commit: Bozhidar Batsov <[email protected]>

    Pre-compute timestamps in sort-by-modification/access-time
    
    The sort comparator was calling file-attributes on every comparison,
    resulting in O(n log n) stat calls.  Pre-compute all timestamps into
    a hash table first (O(n) stats), then sort using cached values.
    
    Also use file-attribute-modification-time/file-attribute-access-time
    accessors instead of raw nth.
---
 projectile.el | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/projectile.el b/projectile.el
index f880e717d2..d35df0094c 100644
--- a/projectile.el
+++ b/projectile.el
@@ -2688,20 +2688,24 @@ Parameters MODE VARIABLE VALUE are passed directly to
 
 (defun projectile-sort-by-modification-time (files)
   "Sort FILES by modification time."
-  (let ((default-directory (projectile-project-root)))
+  (let ((default-directory (projectile-project-root))
+        (mtimes (make-hash-table :test 'equal :size (length files))))
+    (dolist (file files)
+      (puthash file (file-attribute-modification-time (file-attributes file)) 
mtimes))
     (seq-sort (lambda (file1 file2)
-                (let ((file1-mtime (nth 5 (file-attributes file1)))
-                      (file2-mtime (nth 5 (file-attributes file2))))
-                  (not (time-less-p file1-mtime file2-mtime))))
+                (not (time-less-p (gethash file1 mtimes)
+                                  (gethash file2 mtimes))))
               files)))
 
 (defun projectile-sort-by-access-time (files)
   "Sort FILES by access time."
-  (let ((default-directory (projectile-project-root)))
+  (let ((default-directory (projectile-project-root))
+        (atimes (make-hash-table :test 'equal :size (length files))))
+    (dolist (file files)
+      (puthash file (file-attribute-access-time (file-attributes file)) 
atimes))
     (seq-sort (lambda (file1 file2)
-                (let ((file1-atime (nth 4 (file-attributes file1)))
-                      (file2-atime (nth 4 (file-attributes file2))))
-                  (not (time-less-p file1-atime file2-atime))))
+                (not (time-less-p (gethash file1 atimes)
+                                  (gethash file2 atimes))))
               files)))
 
 

Reply via email to