From 7bdbd833b550bc4b7e6a5c1618845fce567c6af4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 8 Feb 1999 14:28:48 -0200 Subject: userdata and strings are kept in separate stringtables --- lstring.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lstring.c b/lstring.c index 9893d5d4..aaddd649 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 1.16 1999/01/04 13:37:29 roberto Exp roberto $ +** $Id: lstring.c,v 1.17 1999/01/25 17:38:04 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -14,7 +14,9 @@ #include "lua.h" -#define NUM_HASHS 61 +#define NUM_HASHSTR 31 +#define NUM_HASHUDATA 31 +#define NUM_HASHS (NUM_HASHSTR+NUM_HASHUDATA) #define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */ @@ -121,10 +123,8 @@ static TaggedString *insert_s (char *str, long l, stringtable *tb) { while ((ts = tb->hash[h1]) != NULL) { if (ts == &EMPTY) j = h1; - else if (ts->constindex >= 0 && - ts->u.s.len == l && - (memcmp(str, ts->str, l) == 0)) - return ts; + else if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0)) + return ts; h1 += (h&(size-2)) + 1; /* double hashing */ if (h1 >= size) h1 -= size; } @@ -137,6 +137,7 @@ static TaggedString *insert_s (char *str, long l, stringtable *tb) { return ts; } + static TaggedString *insert_u (void *buff, int tag, stringtable *tb) { TaggedString *ts; unsigned long h = (unsigned long)buff; @@ -151,10 +152,8 @@ static TaggedString *insert_u (void *buff, int tag, stringtable *tb) { while ((ts = tb->hash[h1]) != NULL) { if (ts == &EMPTY) j = h1; - else if (ts->constindex < 0 && /* is a udata? */ - (tag == ts->u.d.tag || tag == LUA_ANYTAG) && - buff == ts->u.d.v) - return ts; + else if ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v) + return ts; h1 += (h&(size-2)) + 1; /* double hashing */ if (h1 >= size) h1 -= size; } @@ -169,12 +168,13 @@ static TaggedString *insert_u (void *buff, int tag, stringtable *tb) { TaggedString *luaS_createudata (void *udata, int tag) { - return insert_u(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]); + int t = ((unsigned)udata%NUM_HASHUDATA)+NUM_HASHSTR; + return insert_u(udata, tag, &L->string_root[t]); } TaggedString *luaS_newlstr (char *str, long l) { - int i = (l==0)?0:(unsigned char)str[0]; - return insert_s(str, l, &L->string_root[i%NUM_HASHS]); + int t = (l==0) ? 0 : ((unsigned char)str[0]*l)%NUM_HASHSTR; + return insert_s(str, l, &L->string_root[t]); } TaggedString *luaS_new (char *str) { @@ -240,13 +240,14 @@ TaggedString *luaS_collectudata (void) { TaggedString *frees = NULL; int i; L->rootglobal.next = NULL; /* empty list of globals */ - for (i=0; istring_root[i]; int j; for (j=0; jsize; j++) { TaggedString *t = tb->hash[j]; - if (t == NULL || t == &EMPTY || t->constindex != -1) - continue; /* get only user data */ + if (t == NULL || t == &EMPTY) + continue; + LUA_ASSERT(t->constindex == -1, "must be userdata"); t->head.next = (GCnode *)frees; frees = t; tb->hash[j] = &EMPTY; -- cgit v1.2.3-55-g6feb