commit e2498e079e4636217e89f0b28844c4b5df4f8793
parent 65726f3e2e226f6a350a5dba643c13c8edd34965
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Wed, 5 Jan 2005 16:20:29 -0200
change in hash algorithm so that it does not need empty slot
(tables can be 100% full)
Diffstat:
9 files changed, 125 insertions(+), 108 deletions(-)
diff --git a/lgc.c b/lgc.c
@@ -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
** See Copyright Notice in lua.h
*/
@@ -63,7 +63,7 @@
static void removeentry (Node *n) {
- setnilvalue(gval(n)); /* remove corresponding value ... */
+ lua_assert(ttisnil(gval(n)));
if (iscollectable(gkey(n)))
setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */
}
@@ -162,7 +162,6 @@ static int traversetable (global_State *g, Table *h) {
const TValue *mode;
if (h->metatable)
markobject(g, h->metatable);
- lua_assert(h->lsizenode || h->node == g->dummynode);
mode = gfasttm(g, h->metatable, TM_MODE);
if (mode && ttisstring(mode)) { /* is there a weak mode? */
weakkey = (strchr(svalue(mode), 'k') != NULL);
@@ -368,8 +367,10 @@ static void cleartable (GCObject *l) {
while (i--) {
Node *n = gnode(h, i);
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 */
+ }
}
l = h->gclist;
}
diff --git a/lobject.h b/lobject.h
@@ -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
** See Copyright Notice in lua.h
*/
@@ -330,7 +330,7 @@ typedef struct Table {
struct Table *metatable;
TValue *array; /* array part */
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;
int sizearray; /* size of `array' array */
} Table;
@@ -351,6 +351,8 @@ typedef struct Table {
extern const TValue luaO_nilobject;
+#define ceillog2(x) (luaO_log2((x)-1) + 1)
+
int luaO_log2 (unsigned int x);
int luaO_int2fb (unsigned int x);
int luaO_fb2int (int x);
diff --git a/lparser.c b/lparser.c
@@ -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
** See Copyright Notice in lua.h
*/
@@ -547,7 +547,7 @@ static void constructor (LexState *ls, expdesc *t) {
check_match(ls, '}', '{', line);
lastlistfield(fs, &cc);
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 */
}
/* }====================================================================== */
diff --git a/lstate.c b/lstate.c
@@ -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
** 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->weak = NULL;
g->tmudata = NULL;
- setnilvalue(gkey(g->dummynode));
- setnilvalue(gval(g->dummynode));
- gnext(g->dummynode) = NULL;
g->totalbytes = sizeof(LG);
g->gcpace = GCDIV;
g->incgc = 1;
diff --git a/lstate.h b/lstate.h
@@ -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
** See Copyright Notice in lua.h
*/
@@ -90,7 +90,6 @@ typedef struct global_State {
lua_CFunction panic; /* to be called in unprotected errors */
TValue _registry;
struct lua_State *mainthread;
- Node dummynode[1]; /* common node array for all empty tables */
TString *tmname[TM_N]; /* array with tag-method names */
} global_State;
diff --git a/ltable.c b/ltable.c
@@ -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)
** See Copyright Notice in lua.h
*/
@@ -68,6 +68,13 @@
#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
*/
@@ -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 a = nums[0]; /* number of elements smaller than 2^i */
- int na = a; /* number of elements to go to array part */
- int n = (na == 0) ? -1 : 0; /* (log of) optimal size for array part */
- for (i = 1; a < *narray && *narray >= twoto(i-1); i++) {
+ int twotoi; /* 2^i */
+ int a = 0; /* number of elements smaller than 2^i */
+ int na = 0; /* number of elements to go to array part */
+ int n = 0; /* optimal size for array part */
+ for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
if (nums[i] > 0) {
a += nums[i];
- if (a >= twoto(i-1)) { /* more than half elements in use? */
- n = i;
- na = a;
+ if (a > twotoi/2) { /* more than half elements present? */
+ n = twotoi; /* optimal size (till now) */
+ 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);
- *nhash = ntotal - na;
- *narray = (n == -1) ? 0 : twoto(n);
- lua_assert(na <= *narray && na >= *narray/2);
+ *narray = n;
+ lua_assert(*narray/2 <= na && na <= *narray);
+ return na;
}
static int countint (const TValue *key, int *nums) {
int k = arrayindex(key);
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;
}
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) {
- int nums[MAXBITS+1];
- int i, lg;
- int totaluse = 0;
- /* count elements in array part */
- for (i=0, lg=0; lg<=MAXBITS; lg++) { /* for each slice [2^(lg-1) to 2^lg) */
- int ttlg = twoto(lg); /* 2^lg */
- if (ttlg > t->sizearray) {
- ttlg = t->sizearray;
- if (i >= ttlg) break;
+static int numusearray (const Table *t, int *nums) {
+ int lg;
+ int ttlg; /* 2^lg */
+ int ause = 0; /* summation of `nums' */
+ int i = 1; /* count to traverse all array keys */
+ for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */
+ int lc = 0; /* counter */
+ int lim = ttlg;
+ if (lim > t->sizearray) {
+ lim = t->sizearray; /* adjust upper limit */
+ if (i > lim)
+ break; /* no more elements to count */
}
- nums[lg] = 0;
- for (; i<ttlg; i++) {
- if (!ttisnil(&t->array[i])) {
- nums[lg]++;
- totaluse++;
- }
+ /* count elements in range (2^(lg-1), 2^lg] */
+ for (; i <= lim; i++) {
+ if (!ttisnil(&t->array[i-1]))
+ lc++;
}
+ nums[lg] += lc;
+ ause += lc;
}
- for (; lg<=MAXBITS; lg++) nums[lg] = 0; /* reset other counts */
- *narray = totaluse; /* all previous uses were in array part */
- /* count elements in hash part */
- i = sizenode(t);
+ return ause;
+}
+
+
+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--) {
Node *n = &t->node[i];
if (!ttisnil(gval(n))) {
- *narray += countint(key2tval(n), nums);
+ ause += countint(key2tval(n), nums);
totaluse++;
}
}
- /* count extra key */
- *narray += countint(ek, nums);
- totaluse++;
- computesizes(nums, totaluse, narray, nhash);
+ *pnasize += ause;
+ return totaluse;
}
@@ -254,18 +266,18 @@ static void setarrayvector (lua_State *L, Table *t, int size) {
}
-static void setnodevector (lua_State *L, Table *t, int lsize) {
- int i;
- int size = twoto(lsize);
- if (lsize > MAXBITS)
- luaG_runerror(L, "table overflow");
- 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) */
+static void setnodevector (lua_State *L, Table *t, int size) {
+ int lsize;
+ if (size == 0) { /* no elements to hash part? */
+ t->node = cast(Node *, &luaH_dummynode); /* use common `dummynode' */
+ lsize = 0;
}
else {
+ int i;
+ lsize = ceillog2(size);
+ if (lsize > MAXBITS)
+ luaG_runerror(L, "table overflow");
+ size = twoto(lsize);
t->node = luaM_newvector(L, size, Node);
for (i=0; i<size; i++) {
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->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 oldasize = t->sizearray;
int oldhsize = t->lsizenode;
- Node *nold;
- 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);
- }
+ Node *nold = t->node; /* save old hash ... */
if (nasize > oldasize) /* array part must grow? */
setarrayvector(L, t, nasize);
/* create new hash part with appropriate size */
setnodevector(L, t, nhsize);
- /* re-insert elements */
if (nasize < oldasize) { /* array part must shrink? */
t->sizearray = nasize;
/* 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 */
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--) {
Node *old = nold+i;
if (!ttisnil(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 */
}
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) {
- int nasize, nhsize;
- /* compute new sizes for array and hash parts */
- numuse(t, &nasize, &nhsize, ek);
+ int nasize, na;
+ int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
+ 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 */
- 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->sizearray = 0;
t->lsizenode = 0;
- t->node = NULL;
+ t->node = cast(Node *, &luaH_dummynode);
setarrayvector(L, t, narray);
- setnodevector(L, t, luaO_log2(nhash)+1);
+ setnodevector(L, t, nhash);
return 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->array, t->sizearray, TValue);
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
@@ -374,10 +394,15 @@ void luaH_free (lua_State *L, Table *t) {
*/
static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
Node *mp = luaH_mainposition(t, key);
- if (!ttisnil(gval(mp))) { /* main position is not free? */
- /* `mp' of colliding node */
- Node *othern = luaH_mainposition(t, key2tval(mp));
- Node *n = t->firstfree; /* get a free place */
+ if (!ttisnil(gval(mp)) || mp == &luaH_dummynode) {
+ Node *othern;
+ Node *n = getfreepos(L, t); /* 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? */
/* yes; move colliding node into free position */
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;
luaC_barriert(L, t, key);
lua_assert(ttisnil(gval(mp)));
- for (;;) { /* correct `firstfree' */
- 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 */
+ return gval(mp);
}
diff --git a/ltable.h b/ltable.h
@@ -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)
** See Copyright Notice in lua.h
*/
@@ -18,6 +18,8 @@
#define key2tval(n) (cast(const TValue *, gkey(n)))
+extern const Node luaH_dummynode;
+
const TValue *luaH_getnum (Table *t, int key);
TValue *luaH_setnum (lua_State *L, Table *t, int key);
const TValue *luaH_getstr (Table *t, TString *key);
diff --git a/ltests.c b/ltests.c
@@ -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
** See Copyright Notice in lua.h
*/
@@ -200,7 +200,6 @@ static void checktable (global_State *g, Table *h) {
GCObject *hgc = obj2gco(h);
if (h->metatable)
checkobjref(g, hgc, h->metatable);
- lua_assert(h->lsizenode || h->node == g->dummynode);
mode = gfasttm(g, h->metatable, TM_MODE);
if (mode && ttisstring(mode)) { /* is there a weak mode? */
weakkey = (strchr(svalue(mode), 'k') != NULL);
@@ -542,8 +541,8 @@ static int table_query (lua_State *L) {
t = hvalue(obj_at(L, 1));
if (i == -1) {
lua_pushinteger(L, t->sizearray);
- lua_pushinteger(L, sizenode(t));
- lua_pushinteger(L, t->firstfree - t->node);
+ lua_pushinteger(L, t->node == &luaH_dummynode ? 0 : sizenode(t));
+ lua_pushinteger(L, t->lastfree - t->node);
}
else if (i < t->sizearray) {
lua_pushinteger(L, i);
diff --git a/lvm.c b/lvm.c
@@ -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
** See Copyright Notice in lua.h
*/
@@ -462,7 +462,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
case OP_NEWTABLE: {
int b = GETARG_B(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;
luaC_checkGC(L); /***/
base = L->base;