zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] Mild setarrvalue() optimization
@ 2016-11-08 10:27 Sebastian Gniazdowski
  2016-11-08 15:46 ` Daniel Shahaf
  0 siblings, 1 reply; 2+ messages in thread
From: Sebastian Gniazdowski @ 2016-11-08 10:27 UTC (permalink / raw)
  To: zsh-workers

[-- 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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-11-08 15:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-08 10:27 [PATCH] Mild setarrvalue() optimization Sebastian Gniazdowski
2016-11-08 15:46 ` Daniel Shahaf

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).