From: Sebastian Gniazdowski <psprint@fastmail.com>
To: zsh-workers@zsh.org
Subject: [PATCH] Mild setarrvalue() optimization
Date: Tue, 08 Nov 2016 02:27:04 -0800 [thread overview]
Message-ID: <1478600824.1774565.780940761.5A517B9E@webmail.messagingengine.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 906 bytes --]
Hello,
there's freearray() at end of setarrvalue(). It can be replaced with
free() if ownership of all strings will be given away:
for (r = val; *r;)
*p++ = ztrdup(*r++);
becomes:
for (r = val; *r;)
*p++ = *r++;
and freearray(val) -> free(val).
It is very provable that this is correct: `val` must have data of the
same kind as ztrdup() produces, as it is simply passed to freearray(),
where zsfree() is called on each string.
Test function:
test_fun() {
local -a arr
arr=( "a" )
repeat 20; do
arr+=( $arr )
done
}
Runs 2189 ms when no optimization, 1653 ms when optimized (when ran 3
times, test script attached). This is a mild optimization as most often
short arrays are being assigned, not long like in the test function, but
still, this can save one's day in certain situations.
--
Sebastian Gniazdowski
psprint@fastmail.com
[-- Attachment #2: array_mild_opt.diff --]
[-- Type: text/plain, Size: 668 bytes --]
diff --git a/Src/params.c b/Src/params.c
index 330f22b..5a3e0d3 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2648,14 +2648,18 @@ setarrvalue(Value v, char **val)
for (i = 0; i < v->start; i++)
*p++ = i < pre_assignment_length ? ztrdup(*q++) : ztrdup("");
for (r = val; *r;)
- *p++ = ztrdup(*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);
- freearray(val);
+
+ /* Ownership of all strings has been
+ * given away, can plainly free */
+ free(val);
}
}
[-- Attachment #3: test1.zsh --]
[-- Type: application/octet-stream, Size: 211 bytes --]
#!/usr/local/bin/zsh-arrsm-opt
#!/usr/local/bin/zsh-clean
zmodload zsh/zprof
test_fun() {
local -a arr
arr=( "a" )
repeat 20; do
arr+=( $arr )
done
}
test_fun
test_fun
test_fun
zprof
next reply other threads:[~2016-11-08 10:27 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-11-08 10:27 Sebastian Gniazdowski [this message]
2016-11-08 15:46 ` 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=1478600824.1774565.780940761.5A517B9E@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).