From d8aba87a0d5ef84dfedf4c6a73884fa6dc455b24 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Wed, 19 Jun 2019 13:26:58 +0200 Subject: [PATCH] Strength-reduce `equal', `eql', `member' and `memql' When comparing against symbols, turn `equal' and `eql' into `eq', and `member' and `memql' into `memq'. * lisp/emacs-lisp/byte-opt.el (byte-optimize--constant-symbol-p) (byte-optimize-equal, byte-optimize-member): New. (member, memql, equal, eql): Use new byte-optimizers. --- lisp/emacs-lisp/byte-opt.el | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lisp/emacs-lisp/byte-opt.el b/lisp/emacs-lisp/byte-opt.el index 2e096016396..ecaa845fd3e 100644 --- a/lisp/emacs-lisp/byte-opt.el +++ b/lisp/emacs-lisp/byte-opt.el @@ -834,6 +834,36 @@ (if (= 1 (length (cdr form))) "" "s")) form)) +(defun byte-optimize--constant-symbol-p (expr) + "Whether EXPR is a constant symbol." + (and (macroexp-const-p expr) (symbolp (eval expr)))) + +(defun byte-optimize-equal (form) + ;; Replace `equal' or `eql' with `eq' if at least one arg is a symbol. + (byte-optimize-binary-predicate + (if (= (length (cdr form)) 2) + (if (or (byte-optimize--constant-symbol-p (nth 1 form)) + (byte-optimize--constant-symbol-p (nth 2 form))) + (cons 'eq (cdr form)) + form) + ;; Arity errors reported elsewhere. + form))) + +(defun byte-optimize-member (form) + ;; Replace `member' or `memql' with `memq' if the first arg is a symbol, + ;; or the second arg is a list of symbols. + (if (= (length (cdr form)) 2) + (if (or (byte-optimize--constant-symbol-p (nth 1 form)) + (let ((arg2 (nth 2 form))) + (and (macroexp-const-p arg2) + (let ((listval (eval arg2))) + (and (listp listval) + (not (memq nil (mapcar #'symbolp listval)))))))) + (cons 'memq (cdr form)) + form) + ;; Arity errors reported elsewhere. + form)) + (defun byte-optimize-memq (form) ;; (memq foo '(bar)) => (and (eq foo 'bar) '(bar)) (if (/= (length (cdr form)) 2) @@ -879,6 +909,8 @@ (put 'identity 'byte-optimizer 'byte-optimize-identity) (put 'memq 'byte-optimizer 'byte-optimize-memq) +(put 'memql 'byte-optimizer 'byte-optimize-member) +(put 'member 'byte-optimizer 'byte-optimize-member) (put '+ 'byte-optimizer 'byte-optimize-plus) (put '* 'byte-optimizer 'byte-optimize-multiply) @@ -889,7 +921,8 @@ (put '= 'byte-optimizer 'byte-optimize-binary-predicate) (put 'eq 'byte-optimizer 'byte-optimize-binary-predicate) -(put 'equal 'byte-optimizer 'byte-optimize-binary-predicate) +(put 'eql 'byte-optimizer 'byte-optimize-equal) +(put 'equal 'byte-optimizer 'byte-optimize-equal) (put 'string= 'byte-optimizer 'byte-optimize-binary-predicate) (put 'string-equal 'byte-optimizer 'byte-optimize-binary-predicate) -- 2.39.2