diff --git a/src/blua/llimits.h b/src/blua/llimits.h index a884ce592..f20f5fa85 100644 --- a/src/blua/llimits.h +++ b/src/blua/llimits.h @@ -100,9 +100,14 @@ typedef lu_int32 Instruction; #endif -/* Size of cache for strings in the API (better be a prime) */ -#ifndef STRCACHE_SIZE -#define STRCACHE_SIZE 127 +/* +** Size of cache for strings in the API. 'N' is the number of +** sets (better be a prime) and "M" is the size of each set (M == 1 +** makes a direct cache.) +*/ +#ifndef STRCACHE_N +#define STRCACHE_N 63 +#define STRCACHE_M 2 #endif diff --git a/src/blua/lstate.h b/src/blua/lstate.h index 21fb4920c..718bfab99 100644 --- a/src/blua/lstate.h +++ b/src/blua/lstate.h @@ -92,7 +92,7 @@ typedef struct global_State { TString *memerrmsg; /* memory-error message */ struct Table *mt[NUM_TAGS]; /* metatables for basic types */ TString *tmname[TM_N]; /* array with tag-method names */ - TString *strcache[STRCACHE_SIZE]; /* cache for strings in API */ + TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ } global_State; diff --git a/src/blua/lstring.c b/src/blua/lstring.c index 238e3ddcc..83a0a3721 100644 --- a/src/blua/lstring.c +++ b/src/blua/lstring.c @@ -80,11 +80,12 @@ static TString *newlstr (lua_State *L, const char *str, size_t l, ** a non-collectable string.) */ void luaS_clearcache (global_State *g) { - int i; - for (i = 0; i < STRCACHE_SIZE; i++) { - if (luaS_iswhite(g->strcache[i])) /* will entry be collected? */ - g->strcache[i] = g->memerrmsg; /* replace it with something fixed */ - } + int i, j; + for (i = 0; i < STRCACHE_N; i++) + for (j = 0; j < STRCACHE_M; j++) { + if (luaS_iswhite(g->strcache[i][j])) /* will entry be collected? */ + g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ + } } @@ -93,13 +94,14 @@ void luaS_clearcache (global_State *g) { */ void luaS_init (lua_State *L) { global_State *g = G(L); - int i; + int i, j; luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ /* pre-create memory-error message */ g->memerrmsg = luaS_newliteral(L, MEMERRMSG); luaS_fix(g->memerrmsg); /* it should never be collected */ - for (i = 0; i < STRCACHE_SIZE; i++) - g->strcache[i] = g->memerrmsg; /* fill cache with valid strings */ + for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ + for (j = 0; j < STRCACHE_M; j++) + g->strcache[i][j] = g->memerrmsg; } @@ -189,15 +191,19 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { ** check hits. */ TString *luaS_new (lua_State *L, const char *str) { - unsigned int i = IntPoint(str) % STRCACHE_SIZE; /* hash */ - TString **p = &G(L)->strcache[i]; - if (strcmp(str, getstr(*p)) == 0) /* hit? */ - return *p; /* that it is */ - else { /* normal route */ - TString *s = luaS_newlstr(L, str, strlen(str)); - *p = s; - return s; + unsigned int i = IntPoint(str) % STRCACHE_N; /* hash */ + int j; + TString **p = G(L)->strcache[i]; + for (j = 0; j < STRCACHE_M; j++) { + if (strcmp(str, getstr(p[j])) == 0) /* hit? */ + return p[j]; /* that is it */ } + /* normal route */ + for (j = STRCACHE_M - 1; j > 0; j--) + p[j] = p[j - 1]; /* move out last element */ + /* new element is first in the list */ + p[0] = luaS_newlstr(L, str, strlen(str)); + return p[0]; }