zsh-workers
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <psprint2@fastmail.com>
To: zsh-workers@zsh.org
Subject: [PATCH] zrealloc on array+=( )
Date: Sun, 26 Feb 2017 04:06:44 -0800	[thread overview]
Message-ID: <1488110804.4168166.893166432.40AFA0C2@webmail.messagingengine.com> (raw)

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

Hello,
optimization that finally targets much common use case and can sometimes
lead to faster Zsh startup.

fun() {
    local -a arr
    repeat 1000; arr+=( "The appended test string of medium length" );
}
local -F SECONDS=0; fun; echo $SECONDS

100 milliseconds vs 4 milliseconds. Just 1000 repetitions. For 5000, it
is 2.2 seconds vs 4 milliseconds.

I aggregate logs during startup, but didn't get gain as I forgot that I
aggregate as strings (~1000 $'\n' entries). Other tests shown that this
is best aggregation available – strings. So I'm in luck. But now there
could be superior and expected array aggregation.

-- 
  Sebastian Gniazdowski
  psprint2@fastmail.com

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

diff --git a/Src/params.c b/Src/params.c
index 19cbb1c..2af24b5 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2723,24 +2723,58 @@ setarrvalue(Value v, char **val)
 		*p++ = *r++;
 	    }
 	} else {
-	    p = new = (char **) zalloc(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;
+            /* arr+=( ... )
+             * arr[${#arr}+x,...] = */
+            if (post_assignment_length > pre_assignment_length &&
+                    pre_assignment_length <= v->start &&
+                    pre_assignment_length > 0 &&
+                    v->pm->gsu.a->setfn == arrsetfn)
+            {
+                p = new = (char **) zrealloc(old, sizeof(char *)
+                                           * (post_assignment_length + 1));
+
+                p += pre_assignment_length; /* after old elements */
+
+                /* Consider 1 < 0, case for a=( 1 ); a[1,..] =
+                 *          1 < 1, case for a=( 1 ); a[2,..] = */
+                if (pre_assignment_length < v->start) {
+                    /* Above <= implies we should run for i < v->start */
+                    for (i = pre_assignment_length; i < v->start; i++) {
+                        *p++ = ztrdup("");
+                    }
+                }
+
+                for (r = val; *r;) {
+                    /* Give away ownership of the string */
+                    *p++ = *r++;
+                }
+
+                /* v->end doesn't matter:
+                 * a=( 1 2 ); a[4,100]=( a b ); echo "${(q@)a}"
+                 * 1 2 '' a b */
+                *p = NULL;
+
+                v->pm->u.arr = NULL;
+                v->pm->gsu.a->setfn(v->pm, new);
+            } else {
+                p = new = (char **) zalloc(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);
+            }
 
 	    DPUTS2(p - new != post_assignment_length, "setarrvalue: wrong allocation: %d 1= %lu",
 		   post_assignment_length, (unsigned long)(p - new));
-
-	    v->pm->gsu.a->setfn(v->pm, new);
 	}
 
         /* Ownership of all strings has been

             reply	other threads:[~2017-02-26 12:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-26 12:06 Sebastian Gniazdowski [this message]
2017-03-04 13:47 Sebastian Gniazdowski
2017-03-04 17:22 ` Bart Schaefer

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=1488110804.4168166.893166432.40AFA0C2@webmail.messagingengine.com \
    --to=psprint2@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).