change in hash algorithm so that it does not need empty slot

(tables can be 100% full)
This commit is contained in:
Roberto Ierusalimschy 2005-01-05 16:20:51 -02:00
parent 65726f3e2e
commit e2498e079e
9 changed files with 125 additions and 108 deletions

9
lgc.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 2.18 2004/12/06 17:53:42 roberto Exp roberto $ ** $Id: lgc.c,v 2.19 2004/12/13 12:15:11 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -63,7 +63,7 @@
static void removeentry (Node *n) { static void removeentry (Node *n) {
setnilvalue(gval(n)); /* remove corresponding value ... */ lua_assert(ttisnil(gval(n)));
if (iscollectable(gkey(n))) if (iscollectable(gkey(n)))
setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */ setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */
} }
@ -162,7 +162,6 @@ static int traversetable (global_State *g, Table *h) {
const TValue *mode; const TValue *mode;
if (h->metatable) if (h->metatable)
markobject(g, h->metatable); markobject(g, h->metatable);
lua_assert(h->lsizenode || h->node == g->dummynode);
mode = gfasttm(g, h->metatable, TM_MODE); mode = gfasttm(g, h->metatable, TM_MODE);
if (mode && ttisstring(mode)) { /* is there a weak mode? */ if (mode && ttisstring(mode)) { /* is there a weak mode? */
weakkey = (strchr(svalue(mode), 'k') != NULL); weakkey = (strchr(svalue(mode), 'k') != NULL);
@ -368,8 +367,10 @@ static void cleartable (GCObject *l) {
while (i--) { while (i--) {
Node *n = gnode(h, i); Node *n = gnode(h, i);
if (!ttisnil(gval(n)) && /* non-empty entry? */ if (!ttisnil(gval(n)) && /* non-empty entry? */
(iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) {
setnilvalue(gval(n)); /* remove value ... */
removeentry(n); /* remove entry from table */ removeentry(n); /* remove entry from table */
}
} }
l = h->gclist; l = h->gclist;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 2.7 2004/11/01 15:06:50 roberto Exp roberto $ ** $Id: lobject.h,v 2.8 2004/12/04 18:10:22 roberto Exp roberto $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -330,7 +330,7 @@ typedef struct Table {
struct Table *metatable; struct Table *metatable;
TValue *array; /* array part */ TValue *array; /* array part */
Node *node; Node *node;
Node *firstfree; /* this position is free; all positions after it are full */ Node *lastfree; /* any free position is before this position */
GCObject *gclist; GCObject *gclist;
int sizearray; /* size of `array' array */ int sizearray; /* size of `array' array */
} Table; } Table;
@ -351,6 +351,8 @@ typedef struct Table {
extern const TValue luaO_nilobject; extern const TValue luaO_nilobject;
#define ceillog2(x) (luaO_log2((x)-1) + 1)
int luaO_log2 (unsigned int x); int luaO_log2 (unsigned int x);
int luaO_int2fb (unsigned int x); int luaO_int2fb (unsigned int x);
int luaO_fb2int (int x); int luaO_fb2int (int x);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 2.11 2004/12/07 18:31:16 roberto Exp $ ** $Id: lparser.c,v 2.12 2005/01/04 15:55:12 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -547,7 +547,7 @@ static void constructor (LexState *ls, expdesc *t) {
check_match(ls, '}', '{', line); check_match(ls, '}', '{', line);
lastlistfield(fs, &cc); lastlistfield(fs, &cc);
SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh+1)); /* set initial table size */ SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */
} }
/* }====================================================================== */ /* }====================================================================== */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.c,v 2.19 2004/12/13 12:15:11 roberto Exp $ ** $Id: lstate.c,v 2.20 2005/01/04 15:55:12 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -189,9 +189,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
g->grayagain = NULL; g->grayagain = NULL;
g->weak = NULL; g->weak = NULL;
g->tmudata = NULL; g->tmudata = NULL;
setnilvalue(gkey(g->dummynode));
setnilvalue(gval(g->dummynode));
gnext(g->dummynode) = NULL;
g->totalbytes = sizeof(LG); g->totalbytes = sizeof(LG);
g->gcpace = GCDIV; g->gcpace = GCDIV;
g->incgc = 1; g->incgc = 1;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 2.9 2004/12/06 17:53:42 roberto Exp roberto $ ** $Id: lstate.h,v 2.10 2004/12/13 12:15:11 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -90,7 +90,6 @@ typedef struct global_State {
lua_CFunction panic; /* to be called in unprotected errors */ lua_CFunction panic; /* to be called in unprotected errors */
TValue _registry; TValue _registry;
struct lua_State *mainthread; struct lua_State *mainthread;
Node dummynode[1]; /* common node array for all empty tables */
TString *tmname[TM_N]; /* array with tag-method names */ TString *tmname[TM_N]; /* array with tag-method names */
} global_State; } global_State;

191
ltable.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 2.12 2004/12/04 18:10:22 roberto Exp $ ** $Id: ltable.c,v 2.13 2005/01/04 15:55:12 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -68,6 +68,13 @@
#define numints cast(int, sizeof(lua_Number)/sizeof(int)) #define numints cast(int, sizeof(lua_Number)/sizeof(int))
const Node luaH_dummynode = {
{{NULL}, LUA_TNIL}, /* value */
{{NULL}, LUA_TNIL, NULL} /* key */
};
/* /*
** hash for lua_Numbers ** hash for lua_Numbers
*/ */
@ -176,31 +183,32 @@ int luaH_next (lua_State *L, Table *t, StkId key) {
*/ */
static void computesizes (int nums[], int ntotal, int *narray, int *nhash) { static int computesizes (int nums[], int *narray) {
int i; int i;
int a = nums[0]; /* number of elements smaller than 2^i */ int twotoi; /* 2^i */
int na = a; /* number of elements to go to array part */ int a = 0; /* number of elements smaller than 2^i */
int n = (na == 0) ? -1 : 0; /* (log of) optimal size for array part */ int na = 0; /* number of elements to go to array part */
for (i = 1; a < *narray && *narray >= twoto(i-1); i++) { int n = 0; /* optimal size for array part */
for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
if (nums[i] > 0) { if (nums[i] > 0) {
a += nums[i]; a += nums[i];
if (a >= twoto(i-1)) { /* more than half elements in use? */ if (a > twotoi/2) { /* more than half elements present? */
n = i; n = twotoi; /* optimal size (till now) */
na = a; na = a; /* all elements smaller than n will go to array part */
} }
} }
if (a == *narray) break; /* all elements already counted */
} }
lua_assert(na <= *narray && *narray <= ntotal); *narray = n;
*nhash = ntotal - na; lua_assert(*narray/2 <= na && na <= *narray);
*narray = (n == -1) ? 0 : twoto(n); return na;
lua_assert(na <= *narray && na >= *narray/2);
} }
static int countint (const TValue *key, int *nums) { static int countint (const TValue *key, int *nums) {
int k = arrayindex(key); int k = arrayindex(key);
if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
nums[luaO_log2(k-1)+1]++; /* count as such */ nums[ceillog2(k)]++; /* count as such */
return 1; return 1;
} }
else else
@ -208,40 +216,44 @@ static int countint (const TValue *key, int *nums) {
} }
static void numuse (const Table *t, int *narray, int *nhash, const TValue *ek) { static int numusearray (const Table *t, int *nums) {
int nums[MAXBITS+1]; int lg;
int i, lg; int ttlg; /* 2^lg */
int totaluse = 0; int ause = 0; /* summation of `nums' */
/* count elements in array part */ int i = 1; /* count to traverse all array keys */
for (i=0, lg=0; lg<=MAXBITS; lg++) { /* for each slice [2^(lg-1) to 2^lg) */ for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */
int ttlg = twoto(lg); /* 2^lg */ int lc = 0; /* counter */
if (ttlg > t->sizearray) { int lim = ttlg;
ttlg = t->sizearray; if (lim > t->sizearray) {
if (i >= ttlg) break; lim = t->sizearray; /* adjust upper limit */
if (i > lim)
break; /* no more elements to count */
} }
nums[lg] = 0; /* count elements in range (2^(lg-1), 2^lg] */
for (; i<ttlg; i++) { for (; i <= lim; i++) {
if (!ttisnil(&t->array[i])) { if (!ttisnil(&t->array[i-1]))
nums[lg]++; lc++;
totaluse++;
}
} }
nums[lg] += lc;
ause += lc;
} }
for (; lg<=MAXBITS; lg++) nums[lg] = 0; /* reset other counts */ return ause;
*narray = totaluse; /* all previous uses were in array part */ }
/* count elements in hash part */
i = sizenode(t);
static int numusehash (const Table *t, int *nums, int *pnasize) {
int totaluse = 0; /* total number of elements */
int ause = 0; /* summation of `nums' */
int i = sizenode(t);
while (i--) { while (i--) {
Node *n = &t->node[i]; Node *n = &t->node[i];
if (!ttisnil(gval(n))) { if (!ttisnil(gval(n))) {
*narray += countint(key2tval(n), nums); ause += countint(key2tval(n), nums);
totaluse++; totaluse++;
} }
} }
/* count extra key */ *pnasize += ause;
*narray += countint(ek, nums); return totaluse;
totaluse++;
computesizes(nums, totaluse, narray, nhash);
} }
@ -254,18 +266,18 @@ static void setarrayvector (lua_State *L, Table *t, int size) {
} }
static void setnodevector (lua_State *L, Table *t, int lsize) { static void setnodevector (lua_State *L, Table *t, int size) {
int i; int lsize;
int size = twoto(lsize); if (size == 0) { /* no elements to hash part? */
if (lsize > MAXBITS) t->node = cast(Node *, &luaH_dummynode); /* use common `dummynode' */
luaG_runerror(L, "table overflow"); lsize = 0;
if (lsize == 0) { /* no elements to hash part? */
t->node = G(L)->dummynode; /* use common `dummynode' */
lua_assert(ttisnil(gkey(t->node))); /* assert invariants: */
lua_assert(ttisnil(gval(t->node)));
lua_assert(gnext(t->node) == NULL); /* (`dummynode' must be empty) */
} }
else { else {
int i;
lsize = ceillog2(size);
if (lsize > MAXBITS)
luaG_runerror(L, "table overflow");
size = twoto(lsize);
t->node = luaM_newvector(L, size, Node); t->node = luaM_newvector(L, size, Node);
for (i=0; i<size; i++) { for (i=0; i<size; i++) {
gnext(&t->node[i]) = NULL; gnext(&t->node[i]) = NULL;
@ -274,31 +286,19 @@ static void setnodevector (lua_State *L, Table *t, int lsize) {
} }
} }
t->lsizenode = cast(lu_byte, lsize); t->lsizenode = cast(lu_byte, lsize);
t->firstfree = gnode(t, size-1); /* first free position to be used */ t->lastfree = gnode(t, size); /* all positions are free */
} }
static void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
int i; int i;
int oldasize = t->sizearray; int oldasize = t->sizearray;
int oldhsize = t->lsizenode; int oldhsize = t->lsizenode;
Node *nold; Node *nold = t->node; /* save old hash ... */
Node temp[1];
if (oldhsize)
nold = t->node; /* save old hash ... */
else { /* old hash is `dummynode' */
lua_assert(t->node == G(L)->dummynode);
temp[0] = t->node[0]; /* copy it to `temp' */
nold = temp;
setnilvalue(gkey(G(L)->dummynode)); /* restate invariant */
setnilvalue(gval(G(L)->dummynode));
lua_assert(gnext(G(L)->dummynode) == NULL);
}
if (nasize > oldasize) /* array part must grow? */ if (nasize > oldasize) /* array part must grow? */
setarrayvector(L, t, nasize); setarrayvector(L, t, nasize);
/* create new hash part with appropriate size */ /* create new hash part with appropriate size */
setnodevector(L, t, nhsize); setnodevector(L, t, nhsize);
/* re-insert elements */
if (nasize < oldasize) { /* array part must shrink? */ if (nasize < oldasize) { /* array part must shrink? */
t->sizearray = nasize; t->sizearray = nasize;
/* re-insert elements from vanishing slice */ /* re-insert elements from vanishing slice */
@ -309,28 +309,39 @@ static void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
/* shrink array */ /* shrink array */
luaM_reallocvector(L, t->array, oldasize, nasize, TValue); luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
} }
/* re-insert elements in hash part */ /* re-insert elements from hash part */
for (i = twoto(oldhsize) - 1; i >= 0; i--) { for (i = twoto(oldhsize) - 1; i >= 0; i--) {
Node *old = nold+i; Node *old = nold+i;
if (!ttisnil(gval(old))) if (!ttisnil(gval(old)))
setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
} }
if (oldhsize) if (nold != &luaH_dummynode)
luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
} }
void luaH_resizearray (lua_State *L, Table *t, int nasize) { void luaH_resizearray (lua_State *L, Table *t, int nasize) {
luaH_resize(L, t, nasize, t->lsizenode); int nsize = (t->node == &luaH_dummynode) ? 0 : sizenode(t);
resize(L, t, nasize, nsize);
} }
static void rehash (lua_State *L, Table *t, const TValue *ek) { static void rehash (lua_State *L, Table *t, const TValue *ek) {
int nasize, nhsize; int nasize, na;
/* compute new sizes for array and hash parts */ int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
numuse(t, &nasize, &nhsize, ek); int i;
int totaluse;
for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
nasize = numusearray(t, nums); /* count keys in array part */
totaluse = nasize; /* all those keys are integer keys */
totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */
/* count extra key */
nasize += countint(ek, nums);
totaluse++;
/* compute new size for array part */
na = computesizes(nums, &nasize);
/* resize the table to new computed sizes */ /* resize the table to new computed sizes */
luaH_resize(L, t, nasize, luaO_log2(nhsize)+1); resize(L, t, nasize, totaluse - na);
} }
@ -349,21 +360,30 @@ Table *luaH_new (lua_State *L, int narray, int nhash) {
t->array = NULL; t->array = NULL;
t->sizearray = 0; t->sizearray = 0;
t->lsizenode = 0; t->lsizenode = 0;
t->node = NULL; t->node = cast(Node *, &luaH_dummynode);
setarrayvector(L, t, narray); setarrayvector(L, t, narray);
setnodevector(L, t, luaO_log2(nhash)+1); setnodevector(L, t, nhash);
return t; return t;
} }
void luaH_free (lua_State *L, Table *t) { void luaH_free (lua_State *L, Table *t) {
if (t->lsizenode) if (t->node != &luaH_dummynode)
luaM_freearray(L, t->node, sizenode(t), Node); luaM_freearray(L, t->node, sizenode(t), Node);
luaM_freearray(L, t->array, t->sizearray, TValue); luaM_freearray(L, t->array, t->sizearray, TValue);
luaM_free(L, t); luaM_free(L, t);
} }
static Node *getfreepos (lua_State *L, Table *t) {
while (t->lastfree-- > t->node) {
if (ttisnil(gkey(t->lastfree)))
return t->lastfree;
}
return NULL; /* could not find a free place */
}
/* /*
** inserts a new key into a hash table; first, check whether key's main ** inserts a new key into a hash table; first, check whether key's main
@ -374,10 +394,15 @@ void luaH_free (lua_State *L, Table *t) {
*/ */
static TValue *newkey (lua_State *L, Table *t, const TValue *key) { static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
Node *mp = luaH_mainposition(t, key); Node *mp = luaH_mainposition(t, key);
if (!ttisnil(gval(mp))) { /* main position is not free? */ if (!ttisnil(gval(mp)) || mp == &luaH_dummynode) {
/* `mp' of colliding node */ Node *othern;
Node *othern = luaH_mainposition(t, key2tval(mp)); Node *n = getfreepos(L, t); /* get a free place */
Node *n = t->firstfree; /* get a free place */ if (n == NULL) { /* cannot find a free place? */
rehash(L, t, key); /* grow table */
return luaH_set(L, t, key); /* re-insert key into grown table */
}
lua_assert(n != &luaH_dummynode);
othern = luaH_mainposition(t, key2tval(mp));
if (othern != mp) { /* is colliding node out of its main position? */ if (othern != mp) { /* is colliding node out of its main position? */
/* yes; move colliding node into free position */ /* yes; move colliding node into free position */
while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
@ -396,15 +421,7 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
luaC_barriert(L, t, key); luaC_barriert(L, t, key);
lua_assert(ttisnil(gval(mp))); lua_assert(ttisnil(gval(mp)));
for (;;) { /* correct `firstfree' */ return gval(mp);
if (ttisnil(gkey(t->firstfree)))
return gval(mp); /* OK; table still has a free place */
else if (t->firstfree == t->node) break; /* cannot decrement from here */
else (t->firstfree)--;
}
/* no more free places; must create one */
rehash(L, t, key); /* grow table */
return luaH_set(L, t, key); /* re-insert in new table */
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.h,v 2.3 2004/10/06 18:34:16 roberto Exp roberto $ ** $Id: ltable.h,v 2.4 2005/01/04 15:55:12 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -18,6 +18,8 @@
#define key2tval(n) (cast(const TValue *, gkey(n))) #define key2tval(n) (cast(const TValue *, gkey(n)))
extern const Node luaH_dummynode;
const TValue *luaH_getnum (Table *t, int key); const TValue *luaH_getnum (Table *t, int key);
TValue *luaH_setnum (lua_State *L, Table *t, int key); TValue *luaH_setnum (lua_State *L, Table *t, int key);
const TValue *luaH_getstr (Table *t, TString *key); const TValue *luaH_getstr (Table *t, TString *key);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 2.14 2004/10/06 18:34:16 roberto Exp $ ** $Id: ltests.c,v 2.15 2004/11/01 15:06:50 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -200,7 +200,6 @@ static void checktable (global_State *g, Table *h) {
GCObject *hgc = obj2gco(h); GCObject *hgc = obj2gco(h);
if (h->metatable) if (h->metatable)
checkobjref(g, hgc, h->metatable); checkobjref(g, hgc, h->metatable);
lua_assert(h->lsizenode || h->node == g->dummynode);
mode = gfasttm(g, h->metatable, TM_MODE); mode = gfasttm(g, h->metatable, TM_MODE);
if (mode && ttisstring(mode)) { /* is there a weak mode? */ if (mode && ttisstring(mode)) { /* is there a weak mode? */
weakkey = (strchr(svalue(mode), 'k') != NULL); weakkey = (strchr(svalue(mode), 'k') != NULL);
@ -542,8 +541,8 @@ static int table_query (lua_State *L) {
t = hvalue(obj_at(L, 1)); t = hvalue(obj_at(L, 1));
if (i == -1) { if (i == -1) {
lua_pushinteger(L, t->sizearray); lua_pushinteger(L, t->sizearray);
lua_pushinteger(L, sizenode(t)); lua_pushinteger(L, t->node == &luaH_dummynode ? 0 : sizenode(t));
lua_pushinteger(L, t->firstfree - t->node); lua_pushinteger(L, t->lastfree - t->node);
} }
else if (i < t->sizearray) { else if (i < t->sizearray) {
lua_pushinteger(L, i); lua_pushinteger(L, i);

4
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.18 2004/12/03 20:35:33 roberto Exp $ ** $Id: lvm.c,v 2.19 2005/01/04 15:55:12 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -462,7 +462,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
case OP_NEWTABLE: { case OP_NEWTABLE: {
int b = GETARG_B(i); int b = GETARG_B(i);
int c = GETARG_C(i); int c = GETARG_C(i);
sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c) - 1)); sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
L->ci->savedpc = pc; L->ci->savedpc = pc;
luaC_checkGC(L); /***/ luaC_checkGC(L); /***/
base = L->base; base = L->base;