branch: externals/dash
commit c6d7342616eff79f11ff205fa9e4b446329fa210
Author: Basil L. Contovounesios <[email protected]>
Commit: Basil L. Contovounesios <[email protected]>

    Minor loop optimization
    
    * dash.el (--each, --each-while): Restrict scope of it and it-index
    bindings, and prefer let over setq for minor performance gains.
---
 dash.el | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/dash.el b/dash.el
index efc4da665b..0001bda37c 100644
--- a/dash.el
+++ b/dash.el
@@ -68,12 +68,12 @@ This is the anaphoric counterpart to `-each'."
   (let ((l (make-symbol "list"))
         (i (make-symbol "i")))
     `(let ((,l ,list)
-           (,i 0)
-           it it-index)
-       (ignore it it-index)
+           (,i 0))
        (while ,l
-         (setq it (pop ,l) it-index ,i ,i (1+ ,i))
-         ,@body))))
+         (let ((it (pop ,l)) (it-index ,i))
+           (ignore it it-index)
+           ,@body)
+         (setq ,i (1+ ,i))))))
 
 (defun -each (list fn)
   "Call FN on each element of LIST.
@@ -110,11 +110,16 @@ This is the anaphoric counterpart to `-each-while'."
         (elt (make-symbol "elt")))
     `(let ((,l ,list)
            (,i 0)
-           ,elt it it-index)
-       (ignore it it-index)
-       (while (and ,l (setq ,elt (car-safe ,l) it ,elt it-index ,i) ,pred)
-         (setq it ,elt it-index ,i ,i (1+ ,i) ,l (cdr ,l))
-         ,@body))))
+           ,elt)
+       (while (when ,l
+                (setq ,elt (car-safe ,l))
+                (let ((it ,elt) (it-index ,i))
+                  (ignore it it-index)
+                  ,pred))
+         (let ((it ,elt) (it-index ,i))
+           (ignore it it-index)
+           ,@body)
+         (setq ,i (1+ ,i) ,l (cdr ,l))))))
 
 (defun -each-while (list pred fn)
   "Call FN on each ITEM in LIST, while (PRED ITEM) is non-nil.

Reply via email to