sh: Optimize variable code by storing the length of the name.

Obtained from:	NetBSD
This commit is contained in:
Jilles Tjoelker 2011-05-08 16:15:50 +00:00
parent c9cd76313b
commit 3a99ed469a
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=221668
2 changed files with 125 additions and 124 deletions

View File

@ -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;
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);
(void) 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);
(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,54 +840,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(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
@ -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;
}

View File

@ -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 */