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

* Re: [PATCH] Mild setarrvalue() optimization
  2016-11-08 10:27 [PATCH] Mild setarrvalue() optimization Sebastian Gniazdowski
@ 2016-11-08 15:46 ` Daniel Shahaf
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Shahaf @ 2016-11-08 15:46 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: zsh-workers

Sebastian Gniazdowski wrote on Tue, Nov 08, 2016 at 02:27:04 -0800:
> there's freearray() at end of setarrvalue(). It can be replaced with
> free() if ownership of all strings will be given away:

I see this has been applied.

s/calloc/malloc/ should save a few more milliseconds.

Also reduce code duplication.

Cheers,

Daniel

diff --git a/Src/params.c b/Src/params.c
index 19a8c29..9f449c5 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2651,8 +2651,8 @@ setarrvalue(Value v, char **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));
+	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("");
diff --git a/Src/mem.c b/Src/mem.c
index 8c7eb80..0ac4009 100644
--- a/Src/mem.c
+++ b/Src/mem.c
@@ -976,18 +976,8 @@ zalloc(size_t size)
 mod_export void *
 zshcalloc(size_t size)
 {
-    void *ptr;
-
-    if (!size)
-	size = 1;
-    queue_signals();
-    if (!(ptr = (void *) malloc(size))) {
-	zerr("fatal error: out of memory");
-	exit(1);
-    }
-    unqueue_signals();
+    void *ptr = zalloc(size);
     memset(ptr, 0, size);
-
     return ptr;
 }
 


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