From ca2c477d01a45032268f1af995230e1624bb556e Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Sat, 25 Apr 2026 12:29:00 +0200 Subject: [PATCH] Improve 'package-get-descriptor' performance * lisp/emacs-lisp/package.el (package-get-descriptor): Use 'assq' to look up element in package alists instead of iterative over them manually. This was initially not possible, since we merged all alists into one, which would require using a consing function like 'map-merge-with' to look up all package descriptor objects we want to consider in a single 'assq' call. As we ended up not merging lists, we can assume that there are no duplicate keys in the alists, making the efficient 'assq' viable. --- lisp/emacs-lisp/package.el | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el index fd623aa1533..6fb1e1562a0 100644 --- a/lisp/emacs-lisp/package.el +++ b/lisp/emacs-lisp/package.el @@ -4963,25 +4963,22 @@ and `archive'. Any other symbol will be converted to a singleton list. The order is significant, in that the first hit will be returned. If specified, PRED is a function that takes a single `package-desc' argument and prevents the object from being returned if the predicate returns nil." - (unless sources - (setq sources '(installed archive))) (cond ((package-desc-p pkg) (and (or (not pred) (funcall pred pkg)) pkg)) ((or (and (stringp pkg) (setq pkg (intern pkg))) (symbolp pkg)) (catch 'found - (dolist (source (if (eq sources t) - '(installed builtin archive) - (delete-dups (ensure-list sources)))) - (dolist (ent (pcase-exhaustive source - ('installed (package--alist)) - ('builtin package--builtin-alist) - ('archive (package--archive-contents)))) - (when (eq (car ent) pkg) - (dolist (desc (cdr ent)) - (when (or (null pred) (funcall pred desc)) - (throw 'found desc)))))))) + (dolist (source (cond + ((eq sources nil) '(installed archive)) + ((eq sources t) '(installed builtin archive)) + ((delete-dups (ensure-list sources))))) + (dolist (desc (alist-get pkg (pcase-exhaustive source + ('installed (package--alist)) + ('builtin package--builtin-alist) + ('archive (package--archive-contents))))) + (when (or (null pred) (funcall pred desc)) + (throw 'found desc)))))) ((error "Failed to recognize package %S" pkg)))) (provide 'package)