emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Performance issue w/ `cl-loop`s `collect...into`


From: Clément Pit-Claudel
Subject: Re: Performance issue w/ `cl-loop`s `collect...into`
Date: Sat, 7 Apr 2018 23:26:00 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0

On 2018-04-07 20:51, Tianxiang Xiong wrote:
> The following runs nearly instantaneously:
> 
> (progn
>   (cl-loop for i in (number-sequence 0 130000)
>      collect (cons (number-to-string i) i))
>   :done)

This expands to the following:

(progn
  (cl-block nil
    (let* ((#:--cl-var-- (number-sequence 0 130000))
           (i nil)
           (#:--cl-var-- nil))
      (while (consp #:--cl-var--)
        (setq i (car #:--cl-var--))
        (setq #:--cl-var-- (cons (cons (number-to-string i) i) #:--cl-var--))
        (setq #:--cl-var-- (cdr #:--cl-var--)))
      (nreverse #:--cl-var--)))
  :done)

> This seems to take a long time (didn't wait for it to finish):
> 
> (progn
>   (cl-loop for i in (number-sequence 0 130000)
>      collect (cons (number-to-string i) i) into pairs)
>   :done)

Whereas that expands to this:

(progn
  (cl-block nil
    (let* ((#:--cl-var-- (number-sequence 0 130000))
           (i nil)
           (pairs nil))
      (while (consp #:--cl-var--)
        (setq i (car #:--cl-var--))
        (setq pairs (nconc pairs (list (cons (number-to-string i) i))))
        (setq #:--cl-var-- (cdr #:--cl-var--)))
      nil))
  :done)

> Is this a known issue? I couldn't find anything in the bug tracker about it.

The second form is quadratic, maybe because user code is allowed to access the 
accumulation variable during iteration?

It should likely be documented, but it doesn't seem to be ATM.

Clément.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]