Improve flatten-tree performance

* lisp/subr.el (flatten-tree): Improve performance by calling
‘cons’ once rather than twice when a cons cell is popped.
This commit is contained in:
Paul Eggert 2018-12-17 09:54:14 -08:00
parent 4d77c4ac3b
commit 8664ba18c7

View file

@ -5460,14 +5460,14 @@ elements are removed.
TREE can be anything that can be made into a list. For each
element in TREE, if it is a cons cell return its car
recursively. Otherwise return the element."
(let (elems)
(setq tree (list tree))
(while (let ((elem (pop tree)))
(cond ((consp elem)
(setq tree (cons (car elem) (cons (cdr elem) tree))))
(elem
(push elem elems)))
tree))
(let (elems)
(while (consp tree)
(let ((elem (pop tree)))
(while (consp elem)
(push (cdr elem) tree)
(setq elem (car elem)))
(if elem (push elem elems))))
(if tree (push tree elems))
(nreverse elems)))
;; Technically, `flatten-list' is a misnomer, but we provide it here