emacs-diffs
[Top][All Lists]
Advanced

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

master 609cbd6: Optimise 3-arg +, - and *


From: Mattias Engdegård
Subject: master 609cbd6: Optimise 3-arg +, - and *
Date: Sat, 25 Jul 2020 13:25:18 -0400 (EDT)

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

    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).
---
 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 194ceee..6f801be 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)))))



reply via email to

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