zsh-workers
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: zsh-workers@zsh.org
Subject: [PATCH 1/2] Optimize indexing array parameters.
Date: Sat, 30 Jul 2016 13:11:50 +0000	[thread overview]
Message-ID: <1469884311-3264-1-git-send-email-danielsh@tarsus.local2> (raw)

% () { for 1 in $prefix/zsh/bin/zsh Src/zsh; do $1 -f -c 'a=( {1..1000000} ); repeat 3 time ( repeat 300 : $a[1]  )'; done }
( repeat 300; do; : $a[1]; done; )  1.68s user 0.01s system 98% cpu 1.718 total
( repeat 300; do; : $a[1]; done; )  1.69s user 0.01s system 99% cpu 1.710 total
( repeat 300; do; : $a[1]; done; )  1.69s user 0.01s system 99% cpu 1.714 total

( repeat 300; do; : $a[1]; done; )  0.00s user 0.01s system 72% cpu 0.022 total
( repeat 300; do; : $a[1]; done; )  0.00s user 0.01s system 72% cpu 0.022 total
( repeat 300; do; : $a[1]; done; )  0.01s user 0.01s system 69% cpu 0.023 total
---
 Src/params.c |  2 +-
 Src/subst.c  | 15 +++++++++++----
 Src/utils.c  | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/Src/params.c b/Src/params.c
index e7a7365..33f177e 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2026,7 +2026,7 @@ getstrvalue(Value v)
 	else {
 	    if (v->start < 0)
 		v->start += arrlen(ss);
-	    s = (v->start >= arrlen(ss) || v->start < 0) ?
+	    s = (arrlen_le(ss, v->start) || v->start < 0) ?
 		(char *) hcalloc(1) : ss[v->start];
 	}
 	return s;
diff --git a/Src/subst.c b/Src/subst.c
index 8e704b1..e3af156 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -2548,12 +2548,19 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
 		 * necessary joining of arrays until this point
 		 * to avoid the multsub() horror.
 		 */
-		int tmplen = arrlen(v->pm->gsu.a->getfn(v->pm));
 
-		if (v->start < 0)
+		/* arrlen() is expensive, so only compute it if needed. */
+		int tmplen = -1;
+
+		if (v->start < 0) {
+		    tmplen = arrlen(v->pm->gsu.a->getfn(v->pm));
 		    v->start += tmplen + ((v->flags & VALFLAG_INV) ? 1 : 0);
-		if (!(v->flags & VALFLAG_INV) &&
-		    (v->start >= tmplen || v->start < 0))
+		}
+		if (!(v->flags & VALFLAG_INV))
+		    if (v->start < 0 ||
+			(tmplen != -1
+			 ? v->start >= tmplen
+			 : arrlen_le(v->pm->gsu.a->getfn(v->pm), v->start)))
 		    vunset = 1;
 	    }
 	    if (!vunset) {
diff --git a/Src/utils.c b/Src/utils.c
index 95be1fb..95da960 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -2280,6 +2280,46 @@ arrlen(char **s)
     return count;
 }
 
+/* Return TRUE iff arrlen(s) >= lower_bound, but more efficiently. */
+
+/**/
+mod_export char
+arrlen_ge(char **s, unsigned lower_bound)
+{
+    while (lower_bound--)
+	if (!*s++)
+	    return 0 /* FALSE */;
+
+    return 1 /* TRUE */;
+}
+
+/* Return TRUE iff arrlen(s) > lower_bound, but more efficiently. */
+
+/**/
+mod_export char
+arrlen_gt(char **s, unsigned lower_bound)
+{
+    return arrlen_ge(s, 1+lower_bound);
+}
+
+/* Return TRUE iff arrlen(s) <= upper_bound, but more efficiently. */
+
+/**/
+mod_export char
+arrlen_le(char **s, unsigned upper_bound)
+{
+    return arrlen_lt(s, 1+upper_bound);
+}
+
+/* Return TRUE iff arrlen(s) < upper_bound, but more efficiently. */
+
+/**/
+mod_export char
+arrlen_lt(char **s, unsigned upper_bound)
+{
+    return !arrlen_ge(s, upper_bound);
+}
+
 /* Skip over a balanced pair of parenthesis. */
 
 /**/


             reply	other threads:[~2016-07-30 13:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-30 13:11 Daniel Shahaf [this message]
2016-07-30 13:11 ` [PATCH 2/2] Start using the new arrlen_ge() / arrlen_le() helpers 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=1469884311-3264-1-git-send-email-danielsh@tarsus.local2 \
    --to=d.s@daniel.shahaf.name \
    --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).