branch: master
commit ee5eaf69b0c1ca2917c084121869cbe2d0a17b26
Author: Steven Allen <[email protected]>
Commit: Steven Allen <[email protected]>
Make the linux application list generation O(N) instead of O(N*N)
* counsel.el (counsel-linux-apps-list-desktop-files): Use a hashmap to
deduplicate desktop entries.
---
counsel.el | 27 +++++++++++++--------------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/counsel.el b/counsel.el
index 06d6004..76d11e4 100644
--- a/counsel.el
+++ b/counsel.el
@@ -2725,20 +2725,19 @@ And insert it into the minibuffer. Useful during
(defun counsel-linux-apps-list-desktop-files ()
"Returns an alist of ~(desktop-name . desktop-file)~ pairs for all Linux
applications."
- (cl-remove-duplicates
- (apply 'append
- (mapcar
- (lambda (dir)
- (when (file-exists-p dir)
- (let ((dir (file-name-as-directory dir)))
- (mapcar
- (lambda (file)
- (cons (subst-char-in-string ?/ ?- file) (concat dir file)))
- (directory-files dir nil ".*\\.desktop$")))))
- counsel-linux-apps-directories))
- :key 'car
- ;; Earlier directories take precedence
- :from-end t))
+ (let ((hash (make-hash-table :test #'equal))
+ result)
+ (dolist (dir (reverse counsel-linux-apps-directories))
+ (when (file-exists-p dir)
+ (let ((dir (file-name-as-directory dir)))
+ (dolist (file (directory-files dir nil ".*\\.desktop$"))
+ (let ((id (subst-char-in-string ?/ ?- file))
+ (file (concat dir file)))
+ (puthash id file hash))))))
+ (maphash (lambda (key value)
+ (push (cons key value) result))
+ hash)
+ result))
(defun counsel-linux-apps-list ()
(let ((files (counsel-linux-apps-list-desktop-files)) result)