zsh-workers
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <psprint@fastmail.com>
To: zsh-workers@zsh.org
Subject: Re: Possible huge setarrvalue optimization
Date: Fri, 18 Nov 2016 01:32:20 -0800	[thread overview]
Message-ID: <1479461540.1340250.791913609.27FAD722@webmail.messagingengine.com> (raw)
In-Reply-To: <1479449829.1305485.791811385.14DDFE28@webmail.messagingengine.com>

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

Hello,
I attach patch that needs one problem to be resolved. Gains for
following test code:

test_fun() {
    arr[20000]=""
    repeat 10000; do
        arr[10000]="x"
    done
}

are huge: 37969 ms with no optimization, 244 ms with optimization. The
screensaver runs for 495 sec without optimization, 51 sec with opt
(still slower than hash which runs for 23 sec).

I changed:

        if (v->end <= pre_assignment_length)
            post_assignment_length += pre_assignment_length - v->end +
            1;

to:

        if (v->end <= pre_assignment_length)
            post_assignment_length += pre_assignment_length - v->end;

And this allows to test post* == pre*. I think the "+ 1" isn't needed,
v->end can be treated as "number of elements affected already".
Subtracting the number of elements from pre_assignment_length gives
correct value: elements that will be copied from old.

The thing that has to be resolved: special setters. The patch contains
one special handler tested:

if ( pre_assignment_length != post_assignment_length ||
v->pm->gsu.a->setfn == set_region_active ) {

But this doesn't work as the symbol `set_region_active` isn't available.
How to solve this, and what are other special handlers? One test is
failing (A06) – append to typeset -U array, because the special setter
isn't called.

-- 
  Sebastian Gniazdowski
  psprint@fastmail.com

[-- Attachment #2: array_big_opt.diff --]
[-- Type: text/plain, Size: 1953 bytes --]

diff --git a/Src/params.c b/Src/params.c
index ef72cba..e23a0af 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2654,24 +2654,34 @@ setarrvalue(Value v, char **val)
 	    v->end = v->start;
 
 	post_assignment_length = v->start + arrlen(val);
-	if (v->end <= pre_assignment_length)
-	    post_assignment_length += pre_assignment_length - v->end + 1;
-
-	p = new = (char **) zshcalloc(sizeof(char *)
-		                      * (post_assignment_length + 1));
-
-	for (i = 0; i < v->start; i++)
-	    *p++ = i < pre_assignment_length ? ztrdup(*q++) : ztrdup("");
-	for (r = val; *r;) {
-            /* Give away ownership of the string */
-	    *p++ = *r++;
-	}
-	if (v->end < pre_assignment_length)
-	    for (q = old + v->end; *q;)
-		*p++ = ztrdup(*q++);
-	*p = NULL;
 
-	v->pm->gsu.a->setfn(v->pm, new);
+	if (v->end <= pre_assignment_length)
+	    post_assignment_length += pre_assignment_length - v->end;
+
+        if ( pre_assignment_length != post_assignment_length || v->pm->gsu.a->setfn == set_region_active ) {
+            p = new = (char **) zshcalloc(sizeof(char *)
+                                          * (post_assignment_length + 1));
+
+            for (i = 0; i < v->start; i++)
+                *p++ = i < pre_assignment_length ? ztrdup(*q++) : ztrdup("");
+            for (r = val; *r;) {
+                /* Give away ownership of the string */
+                *p++ = *r++;
+            }
+            if (v->end < pre_assignment_length)
+                for (q = old + v->end; *q;)
+                    *p++ = ztrdup(*q++);
+            *p = NULL;
+
+            v->pm->gsu.a->setfn(v->pm, new);
+        } else {
+            /* v->start is 0-based */
+            p = old + v->start;
+            for (r = val; *r;) {
+                /* Give away ownership of the string */
+                *p++ = *r++;
+            }
+        }
 
         /* Ownership of all strings has been
          * given away, can plainly free */

[-- Attachment #3: testopt7.zsh --]
[-- Type: application/octet-stream, Size: 242 bytes --]

#!/usr/local/bin/zsh-arr-assign
#!/usr/local/bin/zsh-arr-assign-before

zmodload zsh/zprof

typeset -a arr

test_fun() {
    arr[20000]=""
    repeat 10000; do
        arr[10000]="x"
    done
}

test_fun

print -rl $arr[@] $arr[10000]

zprof

  parent reply	other threads:[~2016-11-18  9:32 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-18  6:17 Sebastian Gniazdowski
2016-11-18  7:09 ` Sebastian Gniazdowski
2016-11-18  9:32 ` Sebastian Gniazdowski [this message]
2016-11-18 12:20   ` Sebastian Gniazdowski
2016-11-20 11:46     ` Daniel Shahaf
2016-11-20 17:41       ` Bart Schaefer
2016-11-20 20:54         ` Sebastian Gniazdowski
2016-11-20 21:19         ` Peter Stephenson
2016-12-24 17:19         ` Daniel Shahaf
2017-01-04 18:31           ` Sebastian Gniazdowski
2017-01-05  4:13             ` Daniel Shahaf
2017-01-05 13:32               ` Sebastian Gniazdowski
2017-01-05 15:47                 ` 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=1479461540.1340250.791913609.27FAD722@webmail.messagingengine.com \
    --to=psprint@fastmail.com \
    --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).