]> git.eshelyaron.com Git - emacs.git/commitdiff
Constant-propagate access to captured variables
authorMattias Engdegård <mattiase@acm.org>
Sat, 11 Dec 2021 21:11:08 +0000 (22:11 +0100)
committerMattias Engdegård <mattiase@acm.org>
Sat, 11 Dec 2021 21:16:55 +0000 (22:16 +0100)
* lisp/emacs-lisp/byte-opt.el (byte-optimize--substitutable-p):
Treat (internal-get-closed-var N) as constants for propagation
purposes, because that is effectively what such forms will be compiled
to.  This allows for the elimination of some lexical variables.

* test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-tests--test-cases):
Add test case.

lisp/emacs-lisp/byte-opt.el
test/lisp/emacs-lisp/bytecomp-tests.el

index f6db803b78e1ee3907203f045e78d831a10de437..2bdf1f55111aee309a3022e4fd1752b5c85eede6 100644 (file)
@@ -342,8 +342,12 @@ for speeding up processing.")
       (numberp expr)
       (stringp expr)
       (and (consp expr)
-           (memq (car expr) '(quote function))
-           (symbolp (cadr expr)))
+           (or (and (memq (car expr) '(quote function))
+                    (symbolp (cadr expr)))
+               ;; (internal-get-closed-var N) can be considered constant for
+               ;; const-prop purposes.
+               (and (eq (car expr) 'internal-get-closed-var)
+                    (integerp (cadr expr)))))
       (keywordp expr)))
 
 (defmacro byte-optimize--pcase (exp &rest cases)
index 7e51f820b7026abee550c928a8112eb1ba9219d5..a442eb473be89c9cb24f8d02e7cd9d68434b6075 100644 (file)
@@ -686,6 +686,12 @@ inner loops respectively."
                  (let* ((x 'a))
                    (list x (funcall g) (funcall h)))))))
       (funcall (funcall f 'b)))
+
+    ;; Test constant-propagation of access to captured variables.
+    (let* ((x 2)
+           (f (lambda ()
+                (let ((y x)) (list y 3 y)))))
+      (funcall f))
     )
   "List of expressions for cross-testing interpreted and compiled code.")