back to open hashing for the string table (but with a different

'hnext' field, to strings are still collected like all other
objects)
This commit is contained in:
Roberto Ierusalimschy 2013-09-05 16:31:49 -03:00
parent 0ad15fc100
commit d3bbb34c24
5 changed files with 61 additions and 92 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 2.80 2013/08/18 16:12:18 roberto Exp roberto $ ** $Id: lobject.h,v 2.81 2013/08/27 18:53:35 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
*/ */
@ -310,8 +310,9 @@ typedef union TString {
struct { struct {
CommonHeader; CommonHeader;
lu_byte extra; /* reserved words for short strings; "has hash" for longs */ lu_byte extra; /* reserved words for short strings; "has hash" for longs */
unsigned int hash;
size_t len; /* number of characters in string */ size_t len; /* number of characters in string */
union TString *hnext; /* linked list for hash table */
unsigned int hash;
} tsv; } tsv;
} TString; } TString;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.c,v 2.109 2013/08/30 19:14:26 roberto Exp roberto $ ** $Id: lstate.c,v 2.110 2013/09/03 15:37:10 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -289,7 +289,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
g->gcrunning = 0; /* no GC while building state */ g->gcrunning = 0; /* no GC while building state */
g->GCestimate = 0; g->GCestimate = 0;
g->GCthreshold = 10000; g->GCthreshold = 10000;
g->strt.size = g->strt.nuse = g->strt.empty = 0; g->strt.size = g->strt.nuse = 0;
g->strt.hash = NULL; g->strt.hash = NULL;
setnilvalue(&g->l_registry); setnilvalue(&g->l_registry);
luaZ_initbuffer(L, &g->buff); luaZ_initbuffer(L, &g->buff);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 2.92 2013/08/30 19:14:26 roberto Exp roberto $ ** $Id: lstate.h,v 2.93 2013/09/03 15:37:10 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -53,7 +53,6 @@ struct lua_longjmp; /* defined in ldo.c */
typedef struct stringtable { typedef struct stringtable {
TString **hash; TString **hash;
int nuse; /* number of elements */ int nuse; /* number of elements */
int empty; /* number of available empty slots */
int size; int size;
} stringtable; } stringtable;

121
lstring.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstring.c,v 2.32 2013/08/27 20:04:00 roberto Exp roberto $ ** $Id: lstring.c,v 2.33 2013/08/28 18:30:26 roberto Exp roberto $
** String table (keeps all strings handled by Lua) ** String table (keeps all strings handled by Lua)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -20,13 +20,6 @@
#include "lstring.h" #include "lstring.h"
/* mark for vacant places in hash table */
#define VACANTK cast(TString *, cast(size_t, -1))
/* second hash (for double hash) */
#define h2(h1,hash,size) lmod(h1 + ((hash % 61) | 1), size)
/* /*
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
@ -74,30 +67,32 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
void luaS_resize (lua_State *L, int newsize) { void luaS_resize (lua_State *L, int newsize) {
int i; int i;
stringtable *tb = &G(L)->strt; stringtable *tb = &G(L)->strt;
TString **oldhash = tb->hash; if (newsize > tb->size) { /* grow table if needed */
int oldsize = tb->size; luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
tb->hash = luaM_newvector(L, newsize, TString *); for (i = tb->size; i < newsize; i++)
tb->size = newsize; tb->hash[i] = NULL;
/* keep load factor below 75% */ }
tb->empty = newsize/2 + newsize/4 - tb->nuse; for (i = 0; i < tb->size; i++) { /* rehash */
for (i = 0; i < newsize; i++) tb->hash[i] = NULL; TString *p = tb->hash[i];
tb->nuse = 0; tb->hash[i] = NULL;
/* rehash */ while (p) { /* for each node in the list */
for (i = 0; i < oldsize; i++) { TString *hnext = p->tsv.hnext; /* save next */
TString *ts = oldhash[i]; unsigned int h = lmod(p->tsv.hash, newsize); /* new position */
if (ts != NULL && ts != VACANTK) { p->tsv.hnext = tb->hash[h]; /* chain it */
unsigned int hash = ts->tsv.hash; tb->hash[h] = p;
int h1 = lmod(hash, tb->size); p = hnext;
while (tb->hash[h1] != NULL)
h1 = h2(h1, hash, tb->size);
tb->hash[h1] = ts;
tb->nuse++;
} }
} }
luaM_freearray(L, oldhash, oldsize); if (newsize < tb->size) { /* shrink table if needed */
/* vanishing slice should be empty */
lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
}
tb->size = newsize;
} }
/* /*
** creates a new string object ** creates a new string object
*/ */
@ -116,32 +111,15 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l,
} }
static void rehash (lua_State *L, stringtable *tb) {
int size = tb->size;
if (tb->nuse < size / 2) { /* using less than half the size? */
if (tb->nuse < size / 4) /* using less than half of that? */
size /= 2; /* shrink table */
/* else keep size (but reorganize table) */
}
else { /* table must grow */
if (size >= MAX_INT/2) /* avoid arith. overflow */
luaD_throw(L, LUA_ERRMEM); /* regular errors need new strings... */
size *= 2;
}
luaS_resize(L, size);
}
LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) { LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) {
stringtable *tb = &G(L)->strt; stringtable *tb = &G(L)->strt;
unsigned int hash = ts->tsv.hash; TString **p = &tb->hash[lmod(ts->tsv.hash, tb->size)];
int h1 = lmod(hash, tb->size); while (*p != ts) /* find previous element */
while (tb->hash[h1] != ts) { p = &(*p)->tsv.hnext;
lua_assert(tb->hash[h1] != NULL); *p = (*p)->tsv.hnext; /* remove element from its list */
h1 = h2(h1, hash, tb->size);
}
tb->hash[h1] = VACANTK;
tb->nuse--; tb->nuse--;
if (tb->nuse < tb->size/4)
luaS_resize(L, tb->size/2);
} }
@ -150,39 +128,26 @@ LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) {
*/ */
static TString *internshrstr (lua_State *L, const char *str, size_t l) { static TString *internshrstr (lua_State *L, const char *str, size_t l) {
TString *ts; TString *ts;
unsigned int hash = luaS_hash(str, l, G(L)->seed); global_State *g = G(L);
stringtable *tb = &G(L)->strt; unsigned int h = luaS_hash(str, l, g->seed);
int vacant = -1; TString **list = &g->strt.hash[lmod(h, g->strt.size)];
int h1; for (ts = *list; ts != NULL; ts = ts->tsv.hnext) {
h1 = lmod(hash, tb->size); /* previous call can changed 'size' */ if (l == ts->tsv.len &&
while ((ts = tb->hash[h1]) != NULL) { /* search the string in hash table */ (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
if (ts == VACANTK) {
if (vacant < 0) vacant = h1; /* keep track of first vacant place */
}
else if (l == ts->tsv.len &&
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
/* found! */ /* found! */
if (isdead(G(L), obj2gco(ts))) /* dead (but was not collected yet)? */ if (isdead(g, obj2gco(ts))) /* dead (but not collected yet)? */
changewhite(obj2gco(ts)); /* resurrect it */ changewhite(obj2gco(ts)); /* resurrect it */
if (vacant >= 0) { /* is there a better place for this string? */ return ts;
tb->hash[vacant] = ts; /* move it up the line */
tb->hash[h1] = VACANTK;
}
return ts; /* found */
} }
h1 = h2(h1, hash, tb->size);
} }
if (tb->empty <= 0) { /* no more empty spaces? */ if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
rehash(L, tb); luaS_resize(L, g->strt.size * 2);
return internshrstr(L, str, l); /* recompute insertion with new size */ list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
} }
ts = createstrobj(L, str, l, LUA_TSHRSTR, hash); ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
tb->nuse++; ts->tsv.hnext = *list;
if (vacant < 0) /* found no vacant place? */ *list = ts;
tb->empty--; /* will have to use the empty place */ g->strt.nuse++;
else
h1 = vacant; /* use vacant place */
tb->hash[h1] = ts;
return ts; return ts;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 2.153 2013/09/03 15:37:10 roberto Exp roberto $ ** $Id: ltests.c,v 2.154 2013/09/04 15:34:24 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
*/ */
@ -730,17 +730,21 @@ static int table_query (lua_State *L) {
static int string_query (lua_State *L) { static int string_query (lua_State *L) {
stringtable *tb = &G(L)->strt; stringtable *tb = &G(L)->strt;
int s = luaL_optint(L, 2, 0) - 1; int s = luaL_optint(L, 1, 0) - 1;
if (s < 0) { if (s == -1) {
lua_pushinteger(L ,tb->nuse);
lua_pushinteger(L ,tb->size); lua_pushinteger(L ,tb->size);
lua_pushinteger(L ,tb->nuse);
return 2; return 2;
} }
else if (s < tb->size) { else if (s < tb->size) {
TString *ts = tb->hash[s]; TString *ts;
setsvalue2s(L, L->top, ts); int n = 0;
api_incr_top(L); for (ts = tb->hash[s]; ts != NULL; ts = ts->tsv.hnext) {
return 1; setsvalue2s(L, L->top, ts);
api_incr_top(L);
n++;
}
return n;
} }
else return 0; else return 0;
} }