unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Tianxiang Xiong <tianxiang.xiong@gmail.com>
To: "Clément Pit-Claudel" <cpitclaudel@gmail.com>
Cc: Emacs developers <emacs-devel@gnu.org>
Subject: Re: Performance issue w/ `cl-loop`s `collect...into`
Date: Sat, 7 Apr 2018 22:56:56 -0700	[thread overview]
Message-ID: <CACMkxiwmEB6VB8Zj7_s-u0ktYrS7JLQyL1PS4BxW78UvM3_8tQ@mail.gmail.com> (raw)
In-Reply-To: <41631665-6cd6-7096-8866-5ab9559a14ef@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2716 bytes --]

Yikes--wasn't expecting that. Similar code in SBCL runs nearly
instantaneously.

(ql:quickload :alexandria)
(macroexpand-1
 '(loop for i in (alexandria:iota 130000)
        collect (cons (write-to-string i) i) into pairs
        finally (return (length pairs))))

;; =>
(BLOCK NIL
  (LET ((I NIL) (#:LOOP-LIST-585 (ALEXANDRIA.0.DEV:IOTA 130000)))
    (DECLARE (TYPE LIST #:LOOP-LIST-585))
    (SB-LOOP::WITH-LOOP-LIST-COLLECTION-HEAD (#:LOOP-LIST-HEAD-586
                                              #:LOOP-LIST-TAIL-587 PAIRS)
      (TAGBODY
       SB-LOOP::NEXT-LOOP
        (WHEN (ENDP #:LOOP-LIST-585) (GO SB-LOOP::END-LOOP))
        (SB-LOOP::LOOP-REALLY-DESETQ I (CAR #:LOOP-LIST-585))
        (SB-LOOP::LOOP-REALLY-DESETQ #:LOOP-LIST-585 (CDR #:LOOP-LIST-585))
        (SB-LOOP::LOOP-COLLECT-RPLACD
         (#:LOOP-LIST-HEAD-586 #:LOOP-LIST-TAIL-587 PAIRS)
         (LIST (CONS (WRITE-TO-STRING I) I)))
        (GO SB-LOOP::NEXT-LOOP)
       SB-LOOP::END-LOOP
        (RETURN (LENGTH PAIRS))))))


On Sat, Apr 7, 2018 at 8:26 PM, Clément Pit-Claudel <cpitclaudel@gmail.com>
wrote:

> 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.
>
>
>

[-- Attachment #2: Type: text/html, Size: 4723 bytes --]

  reply	other threads:[~2018-04-08  5:56 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-08  0:51 Performance issue w/ `cl-loop`s `collect...into` Tianxiang Xiong
2018-04-08  3:26 ` Clément Pit-Claudel
2018-04-08  5:56   ` Tianxiang Xiong [this message]
2018-04-08  6:12     ` Clément Pit-Claudel
2018-04-08  8:50       ` Tianxiang Xiong
2018-04-08 13:19         ` Clément Pit-Claudel
2018-04-08 16:07         ` Stefan Monnier
2018-04-08 19:58           ` Tianxiang Xiong
2018-04-08 21:13             ` Stefan Monnier
2018-04-08 23:29               ` Tianxiang Xiong
2018-04-09  1:10                 ` Tianxiang Xiong
2018-04-09  1:59                   ` Stefan Monnier
2018-04-09  2:16                     ` Tianxiang Xiong
2018-04-09  2:20                       ` Stefan Monnier
2018-04-09  3:34                         ` Tianxiang Xiong
2018-04-09  3:38                           ` Tianxiang Xiong
2018-04-09 12:07                           ` Stefan Monnier
2018-04-09 12:22                           ` Basil L. Contovounesios
2018-04-09 15:28                             ` Tianxiang Xiong
2018-04-09 15:33                               ` Tianxiang Xiong
2018-04-14  7:01                                 ` Tianxiang Xiong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CACMkxiwmEB6VB8Zj7_s-u0ktYrS7JLQyL1PS4BxW78UvM3_8tQ@mail.gmail.com \
    --to=tianxiang.xiong@gmail.com \
    --cc=cpitclaudel@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).