using 'lastfree == NULL' to signal that table is using the dummy

node for its hash part + new macro 'allocsizenode'
This commit is contained in:
Roberto Ierusalimschy 2016-11-07 10:38:35 -02:00
parent 697593d8d5
commit 7b1fba69b7
4 changed files with 38 additions and 30 deletions

4
lgc.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 2.212 2016/03/31 19:02:03 roberto Exp roberto $ ** $Id: lgc.c,v 2.213 2016/10/19 12:31:42 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -467,7 +467,7 @@ static lu_mem traversetable (global_State *g, Table *h) {
else /* not weak */ else /* not weak */
traversestrongtable(g, h); traversestrongtable(g, h);
return sizeof(Table) + sizeof(TValue) * h->sizearray + return sizeof(Table) + sizeof(TValue) * h->sizearray +
sizeof(Node) * cast(size_t, sizenode(h)); sizeof(Node) * cast(size_t, allocsizenode(h));
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 2.116 2015/11/03 18:35:21 roberto Exp roberto $ ** $Id: ltable.c,v 2.117 2015/11/19 19:16:22 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -74,8 +74,6 @@
#define dummynode (&dummynode_) #define dummynode (&dummynode_)
#define isdummy(n) ((n) == dummynode)
static const Node dummynode_ = { static const Node dummynode_ = {
{NILCONSTANT}, /* value */ {NILCONSTANT}, /* value */
{{NILCONSTANT, 0}} /* key */ {{NILCONSTANT, 0}} /* key */
@ -308,14 +306,14 @@ static void setarrayvector (lua_State *L, Table *t, unsigned int size) {
static void setnodevector (lua_State *L, Table *t, unsigned int size) { static void setnodevector (lua_State *L, Table *t, unsigned int size) {
int lsize;
if (size == 0) { /* no elements to hash part? */ if (size == 0) { /* no elements to hash part? */
t->node = cast(Node *, dummynode); /* use common 'dummynode' */ t->node = cast(Node *, dummynode); /* use common 'dummynode' */
lsize = 0; t->lsizenode = 0;
t->lastfree = NULL; /* signal that it is using dummy node */
} }
else { else {
int i; int i;
lsize = luaO_ceillog2(size); int lsize = luaO_ceillog2(size);
if (lsize > MAXHBITS) if (lsize > MAXHBITS)
luaG_runerror(L, "table overflow"); luaG_runerror(L, "table overflow");
size = twoto(lsize); size = twoto(lsize);
@ -326,9 +324,9 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) {
setnilvalue(wgkey(n)); setnilvalue(wgkey(n));
setnilvalue(gval(n)); setnilvalue(gval(n));
} }
t->lsizenode = cast_byte(lsize);
t->lastfree = gnode(t, size); /* all positions are free */
} }
t->lsizenode = cast_byte(lsize);
t->lastfree = gnode(t, size); /* all positions are free */
} }
@ -337,7 +335,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
unsigned int i; unsigned int i;
int j; int j;
unsigned int oldasize = t->sizearray; unsigned int oldasize = t->sizearray;
int oldhsize = t->lsizenode; int oldhsize = allocsizenode(t);
Node *nold = t->node; /* save old hash ... */ Node *nold = t->node; /* save old hash ... */
if (nasize > oldasize) /* array part must grow? */ if (nasize > oldasize) /* array part must grow? */
setarrayvector(L, t, nasize); setarrayvector(L, t, nasize);
@ -354,7 +352,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
luaM_reallocvector(L, t->array, oldasize, nasize, TValue); luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
} }
/* re-insert elements from hash part */ /* re-insert elements from hash part */
for (j = twoto(oldhsize) - 1; j >= 0; j--) { for (j = oldhsize - 1; j >= 0; j--) {
Node *old = nold + j; Node *old = nold + j;
if (!ttisnil(gval(old))) { if (!ttisnil(gval(old))) {
/* doesn't need barrier/invalidate cache, as entry was /* doesn't need barrier/invalidate cache, as entry was
@ -362,13 +360,13 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
} }
} }
if (!isdummy(nold)) if (oldhsize > 0) /* not the dummy node? */
luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old hash */ luaM_freearray(L, nold, cast(size_t, oldhsize)); /* free old hash */
} }
void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
int nsize = isdummy(t->node) ? 0 : sizenode(t); int nsize = allocsizenode(t);
luaH_resize(L, t, nasize, nsize); luaH_resize(L, t, nasize, nsize);
} }
@ -414,7 +412,7 @@ Table *luaH_new (lua_State *L) {
void luaH_free (lua_State *L, Table *t) { void luaH_free (lua_State *L, Table *t) {
if (!isdummy(t->node)) if (!isdummy(t))
luaM_freearray(L, t->node, cast(size_t, sizenode(t))); luaM_freearray(L, t->node, cast(size_t, sizenode(t)));
luaM_freearray(L, t->array, t->sizearray); luaM_freearray(L, t->array, t->sizearray);
luaM_free(L, t); luaM_free(L, t);
@ -422,10 +420,12 @@ void luaH_free (lua_State *L, Table *t) {
static Node *getfreepos (Table *t) { static Node *getfreepos (Table *t) {
while (t->lastfree > t->node) { if (!isdummy(t)) {
t->lastfree--; while (t->lastfree > t->node) {
if (ttisnil(gkey(t->lastfree))) t->lastfree--;
return t->lastfree; if (ttisnil(gkey(t->lastfree)))
return t->lastfree;
}
} }
return NULL; /* could not find a free place */ return NULL; /* could not find a free place */
} }
@ -445,7 +445,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
if (ttisnil(key)) luaG_runerror(L, "table index is nil"); if (ttisnil(key)) luaG_runerror(L, "table index is nil");
else if (ttisfloat(key)) { else if (ttisfloat(key)) {
lua_Integer k; lua_Integer k;
if (luaV_tointeger(key, &k, 0)) { /* index is int? */ if (luaV_tointeger(key, &k, 0)) { /* does index fit in an integer? */
setivalue(&aux, k); setivalue(&aux, k);
key = &aux; /* insert it as an integer */ key = &aux; /* insert it as an integer */
} }
@ -453,7 +453,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
luaG_runerror(L, "table index is NaN"); luaG_runerror(L, "table index is NaN");
} }
mp = mainposition(t, key); mp = mainposition(t, key);
if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ if (!ttisnil(gval(mp)) || isdummy(t)) { /* main position is taken? */
Node *othern; Node *othern;
Node *f = getfreepos(t); /* get a free place */ Node *f = getfreepos(t); /* get a free place */
if (f == NULL) { /* cannot find a free place? */ if (f == NULL) { /* cannot find a free place? */
@ -461,7 +461,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
/* whatever called 'newkey' takes care of TM cache */ /* whatever called 'newkey' takes care of TM cache */
return luaH_set(L, t, key); /* insert key into grown table */ return luaH_set(L, t, key); /* insert key into grown table */
} }
lua_assert(!isdummy(f)); lua_assert(!isdummy(t));
othern = mainposition(t, gkey(mp)); othern = mainposition(t, gkey(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 */
@ -651,7 +651,7 @@ int luaH_getn (Table *t) {
return i; return i;
} }
/* else must find a boundary in hash part */ /* else must find a boundary in hash part */
else if (isdummy(t->node)) /* hash part is empty? */ else if (isdummy(t)) /* hash part is empty? */
return j; /* that is easy... */ return j; /* that is easy... */
else return unbound_search(t, j); else return unbound_search(t, j);
} }
@ -664,6 +664,6 @@ Node *luaH_mainposition (const Table *t, const TValue *key) {
return mainposition(t, key); return mainposition(t, key);
} }
int luaH_isdummy (Node *n) { return isdummy(n); } int luaH_isdummy (const Table *t) { return isdummy(t); }
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.h,v 2.20 2014/09/04 18:15:29 roberto Exp roberto $ ** $Id: ltable.h,v 2.21 2015/11/03 15:47:30 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -27,6 +27,14 @@
#define invalidateTMcache(t) ((t)->flags = 0) #define invalidateTMcache(t) ((t)->flags = 0)
/* true when 't' is using 'dummynode' as its hash part */
#define isdummy(t) ((t)->lastfree == NULL)
/* allocated size for hash nodes */
#define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t))
/* returns the key, given the value of a table entry */ /* returns the key, given the value of a table entry */
#define keyfromval(v) \ #define keyfromval(v) \
(gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val))))
@ -51,7 +59,7 @@ LUAI_FUNC int luaH_getn (Table *t);
#if defined(LUA_DEBUG) #if defined(LUA_DEBUG)
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
LUAI_FUNC int luaH_isdummy (Node *n); LUAI_FUNC int luaH_isdummy (const Table *t);
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 2.208 2015/09/08 16:55:43 roberto Exp roberto $ ** $Id: ltests.c,v 2.209 2015/10/12 16:38:19 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
*/ */
@ -687,8 +687,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, luaH_isdummy(t->node) ? 0 : sizenode(t)); lua_pushinteger(L, allocsizenode(t));
lua_pushinteger(L, t->lastfree - t->node); lua_pushinteger(L, isdummy(t) ? 0 : t->lastfree - t->node);
} }
else if ((unsigned int)i < t->sizearray) { else if ((unsigned int)i < t->sizearray) {
lua_pushinteger(L, i); lua_pushinteger(L, i);