mirror of
https://github.com/openbsd/src.git
synced 2025-01-10 06:47:55 -08:00
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...
This commit is contained in:
parent
7868fed72c
commit
70a009b1b3
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: extern.h,v 1.11 1999/11/17 15:34:13 espie Exp $ */
|
||||
/* $OpenBSD: extern.h,v 1.12 1999/11/20 17:48:59 espie Exp $ */
|
||||
/* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */
|
||||
|
||||
/*-
|
||||
@ -53,7 +53,7 @@ extern FILE *fopen_trypath __P((const char *filename));
|
||||
|
||||
/* look.c */
|
||||
extern ndptr addent __P((const char *));
|
||||
extern int hash __P((const char *));
|
||||
extern unsigned hash __P((const char *));
|
||||
extern ndptr lookup __P((const char *));
|
||||
extern void remhash __P((const char *, int));
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: look.c,v 1.5 1999/11/17 15:34:13 espie Exp $ */
|
||||
/* $OpenBSD: look.c,v 1.6 1999/11/20 17:48:59 espie Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1989, 1993
|
||||
@ -57,14 +57,14 @@ static char sccsid[] = "@(#)look.c 8.1 (Berkeley) 6/6/93";
|
||||
|
||||
static void freent __P((ndptr));
|
||||
|
||||
int
|
||||
unsigned
|
||||
hash(name)
|
||||
const char *name;
|
||||
{
|
||||
unsigned long h = 0;
|
||||
unsigned h = 0;
|
||||
while (*name)
|
||||
h = (h << 5) + h + *name++;
|
||||
return (h % HASHSIZE);
|
||||
return (h);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -75,9 +75,11 @@ lookup(name)
|
||||
const char *name;
|
||||
{
|
||||
ndptr p;
|
||||
unsigned h;
|
||||
|
||||
for (p = hashtab[hash(name)]; p != nil; p = p->nxtptr)
|
||||
if (STREQ(name, p->name))
|
||||
h = hash(name);
|
||||
for (p = hashtab[h % HASHSIZE]; p != nil; p = p->nxtptr)
|
||||
if (h == p->hv && STREQ(name, p->name))
|
||||
break;
|
||||
return (p);
|
||||
}
|
||||
@ -90,14 +92,15 @@ ndptr
|
||||
addent(name)
|
||||
const char *name;
|
||||
{
|
||||
int h;
|
||||
unsigned h;
|
||||
ndptr p;
|
||||
|
||||
h = hash(name);
|
||||
p = (ndptr) xalloc(sizeof(struct ndblock));
|
||||
p->nxtptr = hashtab[h];
|
||||
hashtab[h] = p;
|
||||
p->nxtptr = hashtab[h % HASHSIZE];
|
||||
hashtab[h % HASHSIZE] = p;
|
||||
p->name = xstrdup(name);
|
||||
p->hv = h;
|
||||
return p;
|
||||
}
|
||||
|
||||
@ -125,14 +128,14 @@ remhash(name, all)
|
||||
ndptr xp, tp, mp;
|
||||
|
||||
h = hash(name);
|
||||
mp = hashtab[h];
|
||||
mp = hashtab[h % HASHSIZE];
|
||||
tp = nil;
|
||||
while (mp != nil) {
|
||||
if (STREQ(mp->name, name)) {
|
||||
if (mp->hv == h && STREQ(mp->name, name)) {
|
||||
mp = mp->nxtptr;
|
||||
if (tp == nil) {
|
||||
freent(hashtab[h]);
|
||||
hashtab[h] = mp;
|
||||
freent(hashtab[h % HASHSIZE]);
|
||||
hashtab[h % HASHSIZE] = mp;
|
||||
}
|
||||
else {
|
||||
xp = tp->nxtptr;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: mdef.h,v 1.7 1999/09/14 08:35:17 espie Exp $ */
|
||||
/* $OpenBSD: mdef.h,v 1.8 1999/11/20 17:49:00 espie Exp $ */
|
||||
/* $NetBSD: mdef.h,v 1.7 1996/01/13 23:25:27 pk Exp $ */
|
||||
|
||||
/*
|
||||
@ -124,6 +124,7 @@ struct ndblock { /* hastable structure */
|
||||
char *name; /* entry name.. */
|
||||
char *defn; /* definition.. */
|
||||
int type; /* type of the entry.. */
|
||||
unsigned hv;
|
||||
ndptr nxtptr; /* link to next entry.. */
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user