zsh-workers
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: Daniel Shahaf <d.s@daniel.shahaf.name>
Cc: zsh workers <zsh-workers@zsh.org>
Subject: Re: Array appends are quadratic
Date: Fri, 27 Nov 2015 09:26:28 +0100	[thread overview]
Message-ID: <CAHYJk3S6_opij6An1DWt+8UDvDerooSyjkXfUfYJgBjUfNMC0Q@mail.gmail.com> (raw)
In-Reply-To: <20151127073730.GI1899@tarsus.local2>

On Fri, Nov 27, 2015 at 8:37 AM, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> Making N appends takes O(N²) time:
>
>     % zsh -fc 'local -a a; time ( repeat 5000 { a+=(foo) } )'
>     ( repeat 5000; do; a+=(foo) ; done; )  1.26s user 0.00s system 99% cpu 1.268 total
>     % zsh -fc 'local -a a; time ( repeat 10000 { a+=(foo) } )'
>     ( repeat 10000; do; a+=(foo) ; done; )  5.02s user 0.00s system 99% cpu 5.028 total
>     % zsh -fc 'local -a a; time ( repeat 20000 { a+=(foo) } )'
>     ( repeat 20000; do; a+=(foo) ; done; )  19.94s user 0.08s system 99% cpu 20.036 total
>
>     % zsh -fc 'local -a a; N=5000; time ( a[N]=(); integer i; while (( i++ < N )) { a[i]=foo } )'
>     ( a[N]=() ; integer i; while (( i++ < N )); do; a[i]=foo ; done; )  2.10s user 0.00s system 99% cpu 2.004 total
>     % zsh -fc 'local -a a; N=10000; time ( a[N]=(); integer i; while (( i++ < N )) { a[i]=foo } )'
>     ( a[N]=() ; integer i; while (( i++ < N )); do; a[i]=foo ; done; )  8.33s user 0.41s system 99% cpu 8.506 total
>     % zsh -fc 'local -a a; N=20000; time ( a[N]=(); integer i; while (( i++ < N )) { a[i]=foo } )'
>     ( a[N]=() ; integer i; while (( i++ < N )); do; a[i]=foo ; done; )  34.92s user 0.00s system 99% cpu 34.246 total
>
> This is because each append reallocates the array with one extra
> element.  This may be confirmed by these debug prints:
[...]
> The first example triggers the assignaparam() debug print with values
> 1,2,…,5000.  The second example triggers the setarrvalue() debug print
> with values 5000,5000,…,5000, and has five thousand iterations.
>
> Complexity could be reduced to O(N) through doubling reallocation
> (whenever reallocating, allocate not oldsize + O(1) but oldsize * O(1)).
>
> Is there a reason not to do that?  Any edge cases to take care of?

According to your debug prints, the array is reallocated every time it
is assigned to, even when the required element is already allocated.
Doubling the allocation size would do nothing to fix that, presumably.

% a[100]=foo
 params.c:2585: setarrvalue: Reallocating 'a' to size 100
% a[5]=bar
 params.c:2585: setarrvalue: Reallocating 'a' to size 101
% a[10]=bar
 params.c:2585: setarrvalue: Reallocating 'a' to size 101

Do we have an... off-by-two bug somewhere?

-- 
Mikael Magnusson


  reply	other threads:[~2015-11-27  8:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-27  7:37 Daniel Shahaf
2015-11-27  8:26 ` Mikael Magnusson [this message]
2015-11-27 10:59   ` Bart Schaefer
2015-11-28 18:05     ` Daniel Shahaf
2015-11-28 19:36       ` Peter Stephenson
2015-11-30  3:20         ` Daniel Shahaf
2015-12-03 23:36         ` Daniel Shahaf

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

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

  git send-email \
    --in-reply-to=CAHYJk3S6_opij6An1DWt+8UDvDerooSyjkXfUfYJgBjUfNMC0Q@mail.gmail.com \
    --to=mikachu@gmail.com \
    --cc=d.s@daniel.shahaf.name \
    --cc=zsh-workers@zsh.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.vuxu.org/mirror/zsh/

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 NNTP newsgroup(s).