diff options
| author | Mattias EngdegÄrd | 2019-06-19 13:26:58 +0200 |
|---|---|---|
| committer | Mattias EngdegÄrd | 2019-06-28 22:47:27 +0200 |
| commit | d8aba87a0d5ef84dfedf4c6a73884fa6dc455b24 (patch) | |
| tree | 122a6b6856c50059f09843f9a93b1ae30fef10d7 | |
| parent | d58fc4e8ece8ccafd5ef430a57c2a8b417c8e038 (diff) | |
| download | emacs-d8aba87a0d5ef84dfedf4c6a73884fa6dc455b24.tar.gz emacs-d8aba87a0d5ef84dfedf4c6a73884fa6dc455b24.zip | |
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.
| -rw-r--r-- | lisp/emacs-lisp/byte-opt.el | 35 |
1 files changed, 34 insertions, 1 deletions
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 @@ | |||
| 834 | (if (= 1 (length (cdr form))) "" "s")) | 834 | (if (= 1 (length (cdr form))) "" "s")) |
| 835 | form)) | 835 | form)) |
| 836 | 836 | ||
| 837 | (defun byte-optimize--constant-symbol-p (expr) | ||
| 838 | "Whether EXPR is a constant symbol." | ||
| 839 | (and (macroexp-const-p expr) (symbolp (eval expr)))) | ||
| 840 | |||
| 841 | (defun byte-optimize-equal (form) | ||
| 842 | ;; Replace `equal' or `eql' with `eq' if at least one arg is a symbol. | ||
| 843 | (byte-optimize-binary-predicate | ||
| 844 | (if (= (length (cdr form)) 2) | ||
| 845 | (if (or (byte-optimize--constant-symbol-p (nth 1 form)) | ||
| 846 | (byte-optimize--constant-symbol-p (nth 2 form))) | ||
| 847 | (cons 'eq (cdr form)) | ||
| 848 | form) | ||
| 849 | ;; Arity errors reported elsewhere. | ||
| 850 | form))) | ||
| 851 | |||
| 852 | (defun byte-optimize-member (form) | ||
| 853 | ;; Replace `member' or `memql' with `memq' if the first arg is a symbol, | ||
| 854 | ;; or the second arg is a list of symbols. | ||
| 855 | (if (= (length (cdr form)) 2) | ||
| 856 | (if (or (byte-optimize--constant-symbol-p (nth 1 form)) | ||
| 857 | (let ((arg2 (nth 2 form))) | ||
| 858 | (and (macroexp-const-p arg2) | ||
| 859 | (let ((listval (eval arg2))) | ||
| 860 | (and (listp listval) | ||
| 861 | (not (memq nil (mapcar #'symbolp listval)))))))) | ||
| 862 | (cons 'memq (cdr form)) | ||
| 863 | form) | ||
| 864 | ;; Arity errors reported elsewhere. | ||
| 865 | form)) | ||
| 866 | |||
| 837 | (defun byte-optimize-memq (form) | 867 | (defun byte-optimize-memq (form) |
| 838 | ;; (memq foo '(bar)) => (and (eq foo 'bar) '(bar)) | 868 | ;; (memq foo '(bar)) => (and (eq foo 'bar) '(bar)) |
| 839 | (if (/= (length (cdr form)) 2) | 869 | (if (/= (length (cdr form)) 2) |
| @@ -879,6 +909,8 @@ | |||
| 879 | 909 | ||
| 880 | (put 'identity 'byte-optimizer 'byte-optimize-identity) | 910 | (put 'identity 'byte-optimizer 'byte-optimize-identity) |
| 881 | (put 'memq 'byte-optimizer 'byte-optimize-memq) | 911 | (put 'memq 'byte-optimizer 'byte-optimize-memq) |
| 912 | (put 'memql 'byte-optimizer 'byte-optimize-member) | ||
| 913 | (put 'member 'byte-optimizer 'byte-optimize-member) | ||
| 882 | 914 | ||
| 883 | (put '+ 'byte-optimizer 'byte-optimize-plus) | 915 | (put '+ 'byte-optimizer 'byte-optimize-plus) |
| 884 | (put '* 'byte-optimizer 'byte-optimize-multiply) | 916 | (put '* 'byte-optimizer 'byte-optimize-multiply) |
| @@ -889,7 +921,8 @@ | |||
| 889 | 921 | ||
| 890 | (put '= 'byte-optimizer 'byte-optimize-binary-predicate) | 922 | (put '= 'byte-optimizer 'byte-optimize-binary-predicate) |
| 891 | (put 'eq 'byte-optimizer 'byte-optimize-binary-predicate) | 923 | (put 'eq 'byte-optimizer 'byte-optimize-binary-predicate) |
| 892 | (put 'equal 'byte-optimizer 'byte-optimize-binary-predicate) | 924 | (put 'eql 'byte-optimizer 'byte-optimize-equal) |
| 925 | (put 'equal 'byte-optimizer 'byte-optimize-equal) | ||
| 893 | (put 'string= 'byte-optimizer 'byte-optimize-binary-predicate) | 926 | (put 'string= 'byte-optimizer 'byte-optimize-binary-predicate) |
| 894 | (put 'string-equal 'byte-optimizer 'byte-optimize-binary-predicate) | 927 | (put 'string-equal 'byte-optimizer 'byte-optimize-binary-predicate) |
| 895 | 928 | ||