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.
This commit is contained in:
Henrik Lissner 2022-06-21 22:42:31 +02:00
parent 3b3c008b1b
commit 72a8485d77
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -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))