From: Mattias EngdegÄrd Date: Wed, 19 Jun 2019 11:26:58 +0000 (+0200) Subject: Strength-reduce `equal', `eql', `member' and `memql' X-Git-Tag: emacs-27.0.90~2160 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=d8aba87a0d5ef84dfedf4c6a73884fa6dc455b24;p=emacs.git 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. --- 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)