Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] assignstrvalue() optimization



Hello,
patch is very simple – utilizes dupstring's internal strlen to not call
strlen again outside. Following test function:

strtest() {
    a=""
    i=$(( 20000 ))
    while (( i -- )); do
        a+="aaaaaaaaaaaaaaaaaaaaaaaaa"
    done
}

runs for 1511 ms with optimizations, 1829 ms with no optimizations.
Changing to i=$(( 10000 )) gives times 364 ms 414 ms, 50 ms difference
(minimum obtainable times).

-- 
  Sebastian Gniazdowski
  psprint@xxxxxxxxxxxx
diff --git a/Src/params.c b/Src/params.c
index 5005042..5fab84a 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2428,8 +2428,8 @@ assignstrvalue(Value v, char *val, int flags)
 	    char *z, *x;
 	    int zlen;
 
-	    z = dupstring(v->pm->gsu.s->getfn(v->pm));
-	    zlen = strlen(z);
+	    z = dupstring_glen(v->pm->gsu.s->getfn(v->pm), (unsigned*) &zlen);
+
 	    if ((v->flags & VALFLAG_INV) && unset(KSHARRAYS))
 		v->start--, v->end--;
 	    if (v->start < 0) {
diff --git a/Src/string.c b/Src/string.c
index b46ea60..ce0cf51 100644
--- a/Src/string.c
+++ b/Src/string.c
@@ -56,6 +56,19 @@ dupstring_wlen(const char *s, unsigned len)
 
 /**/
 mod_export char *
+dupstring_glen(const char *s, unsigned *len_ret)
+{
+    char *t;
+
+    if (!s)
+	return NULL;
+    t = (char *) zhalloc((*len_ret = strlen((char *)s)) + 1);
+    strcpy(t, s);
+    return t;
+}
+
+/**/
+mod_export char *
 ztrdup(const char *s)
 {
     char *t;

Attachment: testopt4b.zsh
Description: Binary data



Messages sorted by: Reverse Date, Date, Thread, Author