From owner-svn-src-head@FreeBSD.ORG Sun May 8 16:15:51 2011 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 16301106566B; Sun, 8 May 2011 16:15:51 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 07B898FC13; Sun, 8 May 2011 16:15:51 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id p48GFoKp053637; Sun, 8 May 2011 16:15:50 GMT (envelope-from jilles@svn.freebsd.org) Received: (from jilles@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id p48GFo5V053634; Sun, 8 May 2011 16:15:50 GMT (envelope-from jilles@svn.freebsd.org) Message-Id: <201105081615.p48GFo5V053634@svn.freebsd.org> From: Jilles Tjoelker Date: Sun, 8 May 2011 16:15:50 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r221668 - head/bin/sh X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 08 May 2011 16:15:51 -0000 Author: jilles Date: Sun May 8 16:15:50 2011 New Revision: 221668 URL: http://svn.freebsd.org/changeset/base/221668 Log: sh: Optimize variable code by storing the length of the name. Obtained from: NetBSD Modified: head/bin/sh/var.c head/bin/sh/var.h Modified: head/bin/sh/var.c ============================================================================== --- head/bin/sh/var.c Sun May 8 15:55:52 2011 (r221667) +++ head/bin/sh/var.c Sun May 8 16:15:50 2011 (r221668) @@ -136,8 +136,8 @@ static const int locale_categories[7] = LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0 }; -static struct var **hashvar(const char *); static int varequal(const char *, const char *); +static struct var *find_var(const char *, struct var ***, int *); static int localevar(const char *); /* @@ -174,20 +174,18 @@ initvar(void) struct var **vpp; for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { - if ((vp->flags & VEXPORT) == 0) { - vpp = hashvar(ip->text); - vp->next = *vpp; - *vpp = vp; - vp->text = __DECONST(char *, ip->text); - vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; - vp->func = ip->func; - } + if (find_var(ip->text, &vpp, &vp->name_len) != NULL) + continue; + vp->next = *vpp; + *vpp = vp; + vp->text = __DECONST(char *, ip->text); + vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; + vp->func = ip->func; } /* * PS1 depends on uid */ - if ((vps1.flags & VEXPORT) == 0) { - vpp = hashvar("PS1="); + if (find_var("PS1", &vpp, &vps1.name_len) == NULL) { vps1.next = *vpp; *vpp = &vps1; vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); @@ -323,50 +321,46 @@ void setvareq(char *s, int flags) { struct var *vp, **vpp; - int len; + int nlen; if (aflag) flags |= VEXPORT; - vpp = hashvar(s); - for (vp = *vpp ; vp ; vp = vp->next) { - if (varequal(s, vp->text)) { - if (vp->flags & VREADONLY) { - len = strchr(s, '=') - s; - error("%.*s: is read only", len, s); - } - if (flags & VNOSET) - return; - INTOFF; + vp = find_var(s, &vpp, &nlen); + if (vp != NULL) { + if (vp->flags & VREADONLY) + error("%.*s: is read only", vp->name_len, s); + if (flags & VNOSET) + return; + INTOFF; - if (vp->func && (flags & VNOFUNC) == 0) - (*vp->func)(strchr(s, '=') + 1); + if (vp->func && (flags & VNOFUNC) == 0) + (*vp->func)(s + vp->name_len + 1); - if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) - ckfree(vp->text); + if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) + ckfree(vp->text); - vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); - vp->flags |= flags; - vp->text = s; - - /* - * We could roll this to a function, to handle it as - * a regular variable function callback, but why bother? - * - * Note: this assumes iflag is not set to 1 initially. - * As part of init(), this is called before arguments - * are looked at. - */ - if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && - iflag == 1) - chkmail(1); - if ((vp->flags & VEXPORT) && localevar(s)) { - change_env(s, 1); - (void) setlocale(LC_ALL, ""); - updatecharset(); - } - INTON; - return; + vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); + vp->flags |= flags; + vp->text = s; + + /* + * We could roll this to a function, to handle it as + * a regular variable function callback, but why bother? + * + * Note: this assumes iflag is not set to 1 initially. + * As part of init(), this is called before arguments + * are looked at. + */ + if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && + iflag == 1) + chkmail(1); + if ((vp->flags & VEXPORT) && localevar(s)) { + change_env(s, 1); + (void) setlocale(LC_ALL, ""); + updatecharset(); } + INTON; + return; } /* not found */ if (flags & VNOSET) @@ -374,6 +368,7 @@ setvareq(char *s, int flags) vp = ckmalloc(sizeof (*vp)); vp->flags = flags; vp->text = s; + vp->name_len = nlen; vp->next = *vpp; vp->func = NULL; INTOFF; @@ -415,14 +410,10 @@ lookupvar(const char *name) { struct var *v; - for (v = *hashvar(name) ; v ; v = v->next) { - if (varequal(v->text, name)) { - if (v->flags & VUNSET) - return NULL; - return strchr(v->text, '=') + 1; - } - } - return NULL; + v = find_var(name, NULL, NULL); + if (v == NULL || v->flags & VUNSET) + return NULL; + return v->text + v->name_len + 1; } @@ -447,15 +438,12 @@ bltinlookup(const char *name, int doall) } if (result != NULL) return result; - for (v = *hashvar(name) ; v ; v = v->next) { - if (varequal(v->text, name)) { - if ((v->flags & VUNSET) - || (!doall && (v->flags & VEXPORT) == 0)) - return NULL; - return strchr(v->text, '=') + 1; - } - } - return NULL; + + v = find_var(name, NULL, NULL); + if (v == NULL || v->flags & VUNSET || + (!doall && (v->flags & VEXPORT) == 0)) + return NULL; + return v->text + v->name_len + 1; } @@ -665,22 +653,18 @@ exportcmd(int argc, char **argv) if ((p = strchr(name, '=')) != NULL) { p++; } else { - vpp = hashvar(name); - for (vp = *vpp ; vp ; vp = vp->next) { - if (varequal(vp->text, name)) { - - vp->flags |= flag; - if ((vp->flags & VEXPORT) && localevar(vp->text)) { - change_env(vp->text, 1); - (void) setlocale(LC_ALL, ""); - updatecharset(); - } - goto found; + vp = find_var(name, NULL, NULL); + if (vp != NULL) { + vp->flags |= flag; + if ((vp->flags & VEXPORT) && localevar(vp->text)) { + change_env(vp->text, 1); + (void) setlocale(LC_ALL, ""); + updatecharset(); } + continue; } } setvar(name, p, flag); -found:; } } else { for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { @@ -747,8 +731,7 @@ mklocal(char *name) memcpy(lvp->text, optlist, sizeof optlist); vp = NULL; } else { - vpp = hashvar(name); - for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next); + vp = find_var(name, &vpp, NULL); if (vp == NULL) { if (strchr(name, '=')) setvareq(savestr(name), VSTRFIXED); @@ -761,7 +744,7 @@ mklocal(char *name) lvp->text = vp->text; lvp->flags = vp->flags; vp->flags |= VSTRFIXED|VTEXTFIXED; - if (strchr(name, '=')) + if (name[vp->name_len] == '=') setvareq(savestr(name), 0); } } @@ -857,55 +840,34 @@ unsetvar(const char *s) struct var **vpp; struct var *vp; - vpp = hashvar(s); - for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { - if (varequal(vp->text, s)) { - if (vp->flags & VREADONLY) - return (1); - INTOFF; - if (*(strchr(vp->text, '=') + 1) != '\0') - setvar(s, nullstr, 0); - if ((vp->flags & VEXPORT) && localevar(vp->text)) { - change_env(s, 0); - setlocale(LC_ALL, ""); - updatecharset(); - } - vp->flags &= ~VEXPORT; - vp->flags |= VUNSET; - if ((vp->flags & VSTRFIXED) == 0) { - if ((vp->flags & VTEXTFIXED) == 0) - ckfree(vp->text); - *vpp = vp->next; - ckfree(vp); - } - INTON; - return (0); - } + vp = find_var(s, &vpp, NULL); + if (vp == NULL) + return (0); + if (vp->flags & VREADONLY) + return (1); + INTOFF; + if (vp->text[vp->name_len + 1] != '\0') + setvar(s, nullstr, 0); + if ((vp->flags & VEXPORT) && localevar(vp->text)) { + change_env(s, 0); + setlocale(LC_ALL, ""); + updatecharset(); } - + vp->flags &= ~VEXPORT; + vp->flags |= VUNSET; + if ((vp->flags & VSTRFIXED) == 0) { + if ((vp->flags & VTEXTFIXED) == 0) + ckfree(vp->text); + *vpp = vp->next; + ckfree(vp); + } + INTON; return (0); } /* - * Find the appropriate entry in the hash table from the name. - */ - -static struct var ** -hashvar(const char *p) -{ - unsigned int hashval; - - hashval = ((unsigned char) *p) << 4; - while (*p && *p != '=') - hashval += (unsigned char) *p++; - return &vartab[hashval % VTABSIZE]; -} - - - -/* * Returns true if the two strings specify the same varable. The first * variable name is terminated by '='; the second may be terminated by * either '=' or '\0'. @@ -922,3 +884,41 @@ varequal(const char *p, const char *q) return 1; return 0; } + +/* + * Search for a variable. + * 'name' may be terminated by '=' or a NUL. + * vppp is set to the pointer to vp, or the list head if vp isn't found + * lenp is set to the number of charactets in 'name' + */ + +static struct var * +find_var(const char *name, struct var ***vppp, int *lenp) +{ + unsigned int hashval; + int len; + struct var *vp, **vpp; + const char *p = name; + + hashval = 0; + while (*p && *p != '=') + hashval = 2 * hashval + (unsigned char)*p++; + len = p - name; + + if (lenp) + *lenp = len; + vpp = &vartab[hashval % VTABSIZE]; + if (vppp) + *vppp = vpp; + + for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { + if (vp->name_len != len) + continue; + if (memcmp(vp->text, name, len) != 0) + continue; + if (vppp) + *vppp = vpp; + return vp; + } + return NULL; +} Modified: head/bin/sh/var.h ============================================================================== --- head/bin/sh/var.h Sun May 8 15:55:52 2011 (r221667) +++ head/bin/sh/var.h Sun May 8 16:15:50 2011 (r221668) @@ -51,6 +51,7 @@ struct var { struct var *next; /* next entry in hash list */ int flags; /* flags are defined above */ + int name_len; /* length of name */ char *text; /* name=value */ void (*func)(const char *); /* function to be called when */