From 72a8485d77586c6ce9952cd3228207ca565eebaf Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 21 Jun 2022 22:42:31 +0200 Subject: [PATCH] perf(lib): optimize fn! macro - Reduces allocations by avoiding copies produced by reverse and seq, and by avoiding a closure. - Reduces runtime by avoiding the overhead of seq's generics. --- core/core-lib.el | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/core/core-lib.el b/core/core-lib.el index 4af64aa39..7f6b1aec2 100644 --- a/core/core-lib.el +++ b/core/core-lib.el @@ -372,12 +372,17 @@ This macro was adapted from llama.el (see https://git.sr.ht/~tarsius/llama), minus font-locking, the outer function call, and minor optimizations." `(lambda ,(let ((argv (make-vector 10 nil))) (doom--fn-crawl args argv) - `(,@(let ((n 0)) - (mapcar (lambda (sym) - (cl-incf n) - (or sym (intern (format "_%%%s" n)))) - (reverse (seq-drop-while - #'null (reverse (seq-subseq argv 1)))))) + `(,@(let ((i (1- (length argv))) + (n -1) + sym arglist) + (while (> i 0) + (setq sym (aref argv i)) + (unless (and (= n -1) (null x)) + (cl-incf n) + (push (or sym (intern (format "_%%%d" (1+ n)))) + arglist)) + (cl-decf i)) + arglist) ,@(and (aref argv 0) '(&rest %*)))) ,@args))