From bd37a3dcd67af8e0c96d05f59af2afd95c46f9c1 Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Sun, 22 Jan 2023 05:22:12 +0100 Subject: 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 1a370c2..9c209dc 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) ;; "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) ;; "Handle optional argument TESTFN." -- cgit v1.0