From 609cbd63c31a21ca521507695abeda1203134c99 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Sat, 25 Jul 2020 19:12:26 +0200 Subject: [PATCH] Optimise 3-arg +, - and * MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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). --- lisp/emacs-lisp/byte-opt.el | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lisp/emacs-lisp/byte-opt.el b/lisp/emacs-lisp/byte-opt.el index 194ceee176f..6f801be5457 100644 --- a/lisp/emacs-lisp/byte-opt.el +++ b/lisp/emacs-lisp/byte-opt.el @@ -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))))) -- 2.39.5