From 3e5298fc96c98d2296432d31ee1d3de86a4c466c Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Fri, 17 Sep 2021 14:01:20 +0200 Subject: [PATCH] Improve performance of seq-union * lisp/emacs-lisp/seq.el (seq-union): Improve performance by using nreverse instead of seq-reverse. --- lisp/emacs-lisp/seq.el | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 87aba66daa7..ae5988296d8 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -471,13 +471,13 @@ negative integer or 0, nil is returned." (cl-defgeneric seq-union (sequence1 sequence2 &optional testfn) "Return a list of all elements that appear in either SEQUENCE1 or SEQUENCE2. Equality is defined by TESTFN if non-nil or by `equal' if nil." - (let ((accum (lambda (acc elt) - (if (seq-contains-p acc elt testfn) - acc - (cons elt acc))))) - (seq-reverse - (seq-reduce accum sequence2 - (seq-reduce accum sequence1 '()))))) + (let* ((accum (lambda (acc elt) + (if (seq-contains-p acc elt testfn) + acc + (cons elt acc)))) + (result (seq-reduce accum sequence2 + (seq-reduce accum sequence1 '())))) + (nreverse result))) ;;;###autoload (cl-defgeneric seq-intersection (sequence1 sequence2 &optional testfn) -- 2.39.5