Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
[PATCH] zrealloc on array+=( )
- X-seq: zsh-workers 40649
- From: Sebastian Gniazdowski <psprint2@xxxxxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: [PATCH] zrealloc on array+=( )
- Date: Sun, 26 Feb 2017 04:06:44 -0800
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.com; h= content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= mesmtp; bh=rCfCJFGPK0lzeQjMz/pAJlLBGQQ=; b=L2yRt7+bY7/NPQYZgRpfg eXa1KF/ivH7bHqAOOtG0yLufbMYgsdyjmOKIY3uhHg5WexqpD4J0mZDru7HrHo6T 4sEMYLCSztfRMl3MtpG5FWgiNS3RWXZ0XnldMei2UWJ0mYS39YJ7KINDx4vWZpqA o3zYxSZoFkShyLNgjjCOCc=
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:message-id:mime-version:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=smtpout; bh=rCfCJFGPK0lzeQjMz/pAJlLBG QQ=; b=pv84Pj2bY4Z/RGZga9WznxxjmnjQuGr3b9D96O//6FnxxXlotm4HYGX+u EDhYYEmgqTkx4W5e+6DwkeJjVR09BLT+HFW1vv6qXCt3FCTSwOAejeJUyHw9vg1w DxPxXo25cL+UO8/QCGXh05QTf3ep0VvMx5VjKIxExVvtMiZlt8=
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
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@xxxxxxxxxxxx
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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author