[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: (byte-compile '(append '(1 2) '(3 4)))
From: |
Philip Kaludercic |
Subject: |
Re: (byte-compile '(append '(1 2) '(3 4))) |
Date: |
Sat, 16 Mar 2024 12:46:15 +0000 |
Felician Nemeth <felician.nemeth@gmail.com> writes:
> (disassemble (byte-compile '(append '(1 2) '(3 4))))
>
> resuts in
>
> byte code:
> args: nil
> 0 constant append
> 1 constant (1 2)
> 2 constant (3 4)
> 3 call 2
> 4 return
>
> Instead I expected it to be something like
>
> byte code:
> args: nil
> 0 constant 1
> 1 constant 2
> 2 constant 3
> 3 constant 4
> 4 list4
> 5 return
>
> I've never looked at byte-code optimization before, and I'm guessing
> this is not a huge improvement, but I still wonder when all the
> arguments of side-effect-free function are constants would it make sense
> to calculate the result at compile time.
`byte-optimize-append' mentions:
;; There is (probably) too much code relying on `append' to return a
;; new list for us to do full constant-folding; these transformations
;; preserve the allocation semantics.
I am not sure what the danger is in the case of constant, quoted lists,
but I am not familiar with the byte compiler either. It seems like this
diff --git a/lisp/emacs-lisp/byte-opt.el b/lisp/emacs-lisp/byte-opt.el
index f75be3f71ad..e6f18590705 100644
--- a/lisp/emacs-lisp/byte-opt.el
+++ b/lisp/emacs-lisp/byte-opt.el
@@ -1599,6 +1599,12 @@ byte-optimize-append
(cdr args))
(cdr newargs)))
+ ;; (append '(C1...) ... '(C2...)) -> (append C1... ... C2...)
+ ((cl-loop for arg in args
+ always (and (eq (car arg) 'quote)
+ (proper-list-p (cdr arg))))
+ `',(mapcan #'cadr args))
+
;; non-terminal arg
((cdr args)
(cond
would do the trick, and the byte-code is even better:
byte code:
args: nil
0 constant (1 2 3 4)
1 return
--
Philip Kaludercic on peregrine