From 7ea41a21b17e1c78cca97484c4bd97dbbf799982 Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Sat, 2 Jul 2011 21:29:43 -1000 Subject: [PATCH] sh: Optimize variable code by storing the length of the name. Obtained-from: FreeBSD SVN rev 221668 --- bin/sh/var.c | 249 ++++++++++++++++++++++++++------------------------- bin/sh/var.h | 3 +- 2 files changed, 127 insertions(+), 125 deletions(-) diff --git a/bin/sh/var.c b/bin/sh/var.c index 717fd85232..6b60b4852f 100644 --- a/bin/sh/var.c +++ b/bin/sh/var.c @@ -34,7 +34,7 @@ * SUCH DAMAGE. * * @(#)var.c 8.3 (Berkeley) 5/4/95 - * $FreeBSD: src/bin/sh/var.c,v 1.57 2011/05/06 22:31:27 jilles Exp $ + * $FreeBSD: src/bin/sh/var.c,v 1.58 2011/05/08 16:15:50 jilles Exp $ */ #include @@ -135,8 +135,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 *); /* @@ -173,20 +173,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=# "); @@ -320,50 +318,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; - - if (vp->func && (flags & VNOFUNC) == 0) - (*vp->func)(strchr(s, '=') + 1); - - 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); - setlocale(LC_ALL, ""); - updatecharset(); - } - INTON; + 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)(s + vp->name_len + 1); + + 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); + setlocale(LC_ALL, ""); + updatecharset(); } + INTON; + return; } /* not found */ if (flags & VNOSET) @@ -371,6 +365,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; @@ -412,14 +407,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; } @@ -444,15 +435,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; } @@ -662,22 +650,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); - 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); + setlocale(LC_ALL, ""); + updatecharset(); } + continue; } } setvar(name, p, flag); -found:; } } else { for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { @@ -744,8 +728,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); @@ -758,7 +741,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); } } @@ -854,53 +837,33 @@ 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(__DECONST(char *, 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(__DECONST(char *, 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 @@ -918,3 +881,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; +} diff --git a/bin/sh/var.h b/bin/sh/var.h index 4d62501789..b9b4a849da 100644 --- a/bin/sh/var.h +++ b/bin/sh/var.h @@ -34,7 +34,7 @@ * SUCH DAMAGE. * * @(#)var.h 8.2 (Berkeley) 5/4/95 - * $FreeBSD: src/bin/sh/var.h,v 1.20 2011/05/06 22:31:27 jilles Exp $ + * $FreeBSD: src/bin/sh/var.h,v 1.21 2011/05/08 16:15:50 jilles Exp $ */ /* @@ -55,6 +55,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 */ -- 2.41.0