emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 1d8712bcc9 2/2: Strength-reduce apply with (list ...) as tail arg


From: Mattias Engdegård
Subject: master 1d8712bcc9 2/2: Strength-reduce apply with (list ...) as tail argument
Date: Sun, 14 Aug 2022 09:49:29 -0400 (EDT)

branch: master
commit 1d8712bcc994be23577aa3f30cb64834b99d403e
Author: Mattias Engdegård <mattiase@acm.org>
Commit: Mattias Engdegård <mattiase@acm.org>

    Strength-reduce apply with (list ...) as tail argument
    
    * lisp/emacs-lisp/byte-opt.el (byte-optimize-apply):
    Transform (apply F ... (list X ...)) -> (funcall F ... X ...)
---
 lisp/emacs-lisp/byte-opt.el | 37 +++++++++++++++++++------------------
 1 file changed, 19 insertions(+), 18 deletions(-)

diff --git a/lisp/emacs-lisp/byte-opt.el b/lisp/emacs-lisp/byte-opt.el
index a7edecfac7..fdeb5db0ec 100644
--- a/lisp/emacs-lisp/byte-opt.el
+++ b/lisp/emacs-lisp/byte-opt.el
@@ -1207,25 +1207,26 @@ See Info node `(elisp) Integer Basics'."
       form)))
 
 (defun byte-optimize-apply (form)
-  ;; If the last arg is a literal constant, turn this into a funcall.
-  ;; The funcall optimizer can then transform (funcall 'foo ...) -> (foo ...).
-  (if (= (length form) 2)
-      ;; single-argument `apply' is not worth optimizing (bug#40968)
-      form
-    (let ((fn (nth 1 form))
-         (last (nth (1- (length form)) form))) ; I think this really is fastest
-      (or (if (or (null last)
-                 (eq (car-safe last) 'quote))
-             (if (listp (nth 1 last))
-                 (let ((butlast (nreverse (cdr (reverse (cdr (cdr form)))))))
-                   (nconc (list 'funcall fn) butlast
-                          (mapcar (lambda (x) (list 'quote x)) (nth 1 last))))
+  (let ((len (length form)))
+    (if (>= len 2)
+        (let ((fn (nth 1 form))
+             (last (nth (1- len) form)))
+          (cond
+           ;; (apply F ... '(X Y ...)) -> (funcall F ... 'X 'Y ...)
+           ((or (null last)
+                (eq (car-safe last) 'quote))
+            (let ((last-value (nth 1 last)))
+             (if (listp last-value)
+                  `(funcall ,fn ,@(butlast (cddr form))
+                            ,@(mapcar (lambda (x) (list 'quote x)) last-value))
                (byte-compile-warn-x
-                 last
-                "last arg to apply can't be a literal atom: `%s'"
-                last)
-               nil))
-         form))))
+                 last "last arg to apply can't be a literal atom: `%s'" last)
+               nil)))
+           ;; (apply F ... (list X Y ...)) -> (funcall F ... X Y ...)
+           ((eq (car-safe last) 'list)
+            `(funcall ,fn ,@(butlast (cddr form)) ,@(cdr last)))
+           (t form)))
+      form)))
 
 (put 'funcall 'byte-optimizer #'byte-optimize-funcall)
 (put 'apply   'byte-optimizer #'byte-optimize-apply)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]