summaryrefslogtreecommitdiff
path: root/usr.bin/m4/main.c
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>1999-11-20 17:49:01 +0000
committerMarc Espie <espie@cvs.openbsd.org>1999-11-20 17:49:01 +0000
commit1bfcf7518da45ac3a68d846c823c56b36c9cfe4d (patch)
tree5baa2667a5d652709e9dce57fa0d5ef30f99c1e9 /usr.bin/m4/main.c
parent1b4625eed721e8d12995f70c88587cf78fad5a1e (diff)
Optimization: cache the hashed value to avoid negative comparisons.
With 2^32 possible hash values, this means that collisions no longer incur supplementary string compares, which was most of the reason for STREQ in the first place...
Diffstat (limited to 'usr.bin/m4/main.c')
-rw-r--r--usr.bin/m4/main.c17
1 files changed, 9 insertions, 8 deletions
diff --git a/usr.bin/m4/main.c b/usr.bin/m4/main.c
index bfc3af1bda4..93b7952b0ac 100644
--- a/usr.bin/m4/main.c
+++ b/usr.bin/m4/main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: main.c,v 1.18 1999/11/17 15:34:13 espie Exp $ */
+/* $OpenBSD: main.c,v 1.19 1999/11/20 17:48:59 espie Exp $ */
/* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
/*-
@@ -47,7 +47,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: main.c,v 1.18 1999/11/17 15:34:13 espie Exp $";
+static char rcsid[] = "$OpenBSD: main.c,v 1.19 1999/11/20 17:48:59 espie Exp $";
#endif
#endif /* not lint */
@@ -440,7 +440,7 @@ inspect(tp)
char *name = tp;
char *etp = tp+MAXTOK;
ndptr p;
- unsigned long h = 0;
+ unsigned h = 0;
while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
h = (h << 5) + h + (*tp++ = c);
@@ -450,8 +450,8 @@ inspect(tp)
*tp = EOS;
- for (p = hashtab[h%HASHSIZE]; p != nil; p = p->nxtptr)
- if (STREQ(name, p->name))
+ for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
+ if (h == p->hv && STREQ(name, p->name))
break;
return p;
}
@@ -467,16 +467,17 @@ static void
initkwds()
{
size_t i;
- int h;
+ unsigned h;
ndptr p;
for (i = 0; i < MAXKEYS; i++) {
h = hash(keywrds[i].knam);
p = (ndptr) xalloc(sizeof(struct ndblock));
- p->nxtptr = hashtab[h];
- hashtab[h] = p;
+ p->nxtptr = hashtab[h % HASHSIZE];
+ hashtab[h % HASHSIZE] = p;
p->name = keywrds[i].knam;
p->defn = null;
+ p->hv = h;
p->type = keywrds[i].ktyp | STATIC;
}
}