]> git.eshelyaron.com Git - emacs.git/commitdiff
Improved `null` (alias `not`) optimisation
authorMattias Engdegård <mattiase@acm.org>
Tue, 16 Aug 2022 17:03:46 +0000 (19:03 +0200)
committerMattias Engdegård <mattiase@acm.org>
Tue, 16 Aug 2022 18:44:50 +0000 (20:44 +0200)
Take static boolean information of the argument into account.

* lisp/emacs-lisp/byte-opt.el (byte-optimize-not): New.

lisp/emacs-lisp/byte-opt.el

index 74a6523cecba814c8f395c522d3a8f17531f5702..bbe8135f04a0379ffa38f7cde0af4a299852d9d9 100644 (file)
@@ -1306,11 +1306,22 @@ See Info node `(elisp) Integer Basics'."
         condition
       form)))
 
+(defun byte-optimize-not (form)
+  (and (= (length form) 2)
+       (let ((arg (nth 1 form)))
+         (cond ((null arg) t)
+               ((macroexp-const-p arg) nil)
+               ((byte-compile-nilconstp arg) `(progn ,arg t))
+               ((byte-compile-trueconstp arg) `(progn ,arg nil))
+               (t form)))))
+
 (put 'and   'byte-optimizer #'byte-optimize-and)
 (put 'or    'byte-optimizer #'byte-optimize-or)
 (put 'cond  'byte-optimizer #'byte-optimize-cond)
 (put 'if    'byte-optimizer #'byte-optimize-if)
 (put 'while 'byte-optimizer #'byte-optimize-while)
+(put 'not   'byte-optimizer #'byte-optimize-not)
+(put 'null  'byte-optimizer #'byte-optimize-not)
 
 ;; byte-compile-negation-optimizer lives in bytecomp.el
 (put '/= 'byte-optimizer #'byte-compile-negation-optimizer)