branch: externals/compat commit bd37a3dcd67af8e0c96d05f59af2afd95c46f9c1 Author: Daniel Mendler <m...@daniel-mendler.de> Commit: Daniel Mendler <m...@daniel-mendler.de>
compat-26: Optimize assoc --- compat-26.el | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/compat-26.el b/compat-26.el index 1a370c20cc..9c209dce13 100644 --- a/compat-26.el +++ b/compat-26.el @@ -92,12 +92,17 @@ If you just want to check `major-mode', use `derived-mode-p'." (compat-defun assoc (key alist &optional testfn) ;; <compat-tests:assoc> "Handle the optional TESTFN." :extended t - (if testfn - (catch 'found - (dolist (ent alist) - (when (funcall testfn (car ent) key) - (throw 'found ent)))) - (assoc key alist))) + (cond + ((or (eq testfn #'eq) + (and (not testfn) (or (symbolp key) (integerp key)))) ;; eq_comparable_value + (assq key alist)) + ((or (eq testfn #'equal) (not testfn)) + (assoc key alist)) + (t + (catch 'found + (dolist (ent alist) + (when (funcall testfn (car ent) key) + (throw 'found ent))))))) (compat-defun alist-get (key alist &optional default remove testfn) ;; <compat-tests:alist-get> "Handle optional argument TESTFN."