Optimise 3-arg +, - and *

Turn (+ a b c) into (+ (+ a b) c), and do the same for - and *.
The 2-arg operations have their own bytecode which results in a 1.5×
speed-up.  Furthermore, the transform enables other optimisations; for
example, (+ a 1 b) -> (+ (1+ a) b).

* lisp/emacs-lisp/byte-opt.el (byte-optimize-plus, byte-optimize-minus)
(byte-optimize-multiply): Transform (OP a b c) into (OP (OP a b) c).
This commit is contained in:
Mattias Engdegård 2020-07-25 19:12:26 +02:00
parent 3b44829823
commit 609cbd63c3

View file

@ -709,6 +709,9 @@
(integer (if integer-is-first arg1 arg2))
(other (if integer-is-first arg2 arg1)))
(list (if (eq integer 1) '1+ '1-) other)))
;; (+ x y z) -> (+ (+ x y) z)
((= (length args) 3)
`(+ ,(byte-optimize-plus `(+ ,(car args) ,(cadr args))) ,@(cddr args)))
;; not further optimized
((equal args (cdr form)) form)
(t (cons '+ args)))))
@ -737,6 +740,9 @@
((and (null (cdr args))
(numberp (car args)))
(- (car args)))
;; (- x y z) -> (- (- x y) z)
((= (length args) 3)
`(- ,(byte-optimize-minus `(- ,(car args) ,(cadr args))) ,@(cddr args)))
;; not further optimized
((equal args (cdr form)) form)
(t (cons '- args))))))
@ -764,6 +770,10 @@
((null args) 1)
;; (* n) -> n, where n is a number
((and (null (cdr args)) (numberp (car args))) (car args))
;; (* x y z) -> (* (* x y) z)
((= (length args) 3)
`(* ,(byte-optimize-multiply `(* ,(car args) ,(cadr args)))
,@(cddr args)))
;; not further optimized
((equal args (cdr form)) form)
(t (cons '* args)))))