code for string cache generalized for "associative sets" (compiler

will optimize away or inline the extra loops)
This commit is contained in:
Roberto Ierusalimschy 2015-09-22 11:18:24 -03:00
parent 0f1f51be4b
commit 9fae7b6d3f
4 changed files with 36 additions and 23 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: llimits.h,v 1.136 2015/07/15 15:57:13 roberto Exp roberto $ ** $Id: llimits.h,v 1.137 2015/09/08 16:53:56 roberto Exp roberto $
** Limits, basic types, and some other 'installation-dependent' definitions ** Limits, basic types, and some other 'installation-dependent' definitions
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -190,10 +190,13 @@ typedef unsigned long Instruction;
/* /*
** Size of cache for strings in the API (better be a prime) ** 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.)
*/ */
#if !defined(STRCACHE_SIZE) #if !defined(STRCACHE_N)
#define STRCACHE_SIZE 127 #define STRCACHE_N 63
#define STRCACHE_M 2
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 2.123 2015/07/04 16:33:17 roberto Exp roberto $ ** $Id: lstate.h,v 2.124 2015/09/08 15:41:05 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -140,7 +140,7 @@ typedef struct global_State {
TString *memerrmsg; /* memory-error message */ TString *memerrmsg; /* memory-error message */
TString *tmname[TM_N]; /* array with tag-method names */ TString *tmname[TM_N]; /* array with tag-method names */
struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
TString *strcache[STRCACHE_SIZE][1]; /* cache for strings in API */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
} global_State; } global_State;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstring.c,v 2.51 2015/09/08 15:41:05 roberto Exp roberto $ ** $Id: lstring.c,v 2.52 2015/09/17 15:51:05 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
*/ */
@ -92,11 +92,12 @@ void luaS_resize (lua_State *L, int newsize) {
** a non-collectable string.) ** a non-collectable string.)
*/ */
void luaS_clearcache (global_State *g) { void luaS_clearcache (global_State *g) {
int i; int i, j;
for (i = 0; i < STRCACHE_SIZE; i++) { for (i = 0; i < STRCACHE_N; i++)
if (iswhite(g->strcache[i][0])) /* will entry be collected? */ for (j = 0; j < STRCACHE_M; j++) {
g->strcache[i][0] = g->memerrmsg; /* replace it with something fixed */ if (iswhite(g->strcache[i][j])) /* will entry be collected? */
} g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
}
} }
@ -105,13 +106,14 @@ void luaS_clearcache (global_State *g) {
*/ */
void luaS_init (lua_State *L) { void luaS_init (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
int i; int i, j;
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
/* pre-create memory-error message */ /* pre-create memory-error message */
g->memerrmsg = luaS_newliteral(L, MEMERRMSG); g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
for (i = 0; i < STRCACHE_SIZE; i++) /* fill cache with valid strings */ for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
g->strcache[i][0] = g->memerrmsg; for (j = 0; j < STRCACHE_M; j++)
g->strcache[i][j] = g->memerrmsg;
} }
@ -205,15 +207,19 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
** check hits. ** check hits.
*/ */
TString *luaS_new (lua_State *L, const char *str) { TString *luaS_new (lua_State *L, const char *str) {
unsigned int i = point2uint(str) % STRCACHE_SIZE; /* hash */ unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
int j;
TString **p = G(L)->strcache[i]; TString **p = G(L)->strcache[i];
if (strcmp(str, getstr(p[0])) == 0) /* hit? */ for (j = 0; j < STRCACHE_M; j++) {
return p[0]; /* that it is */ if (strcmp(str, getstr(p[j])) == 0) /* hit? */
else { /* normal route */ return p[j]; /* that is it */
TString *s = luaS_newlstr(L, str, strlen(str));
p[0] = s;
return s;
} }
/* 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];
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.h,v 2.47 2014/12/26 14:44:44 roberto Exp roberto $ ** $Id: ltests.h,v 2.48 2015/06/18 14:27:44 roberto Exp roberto $
** Internal Header for Debugging of the Lua Implementation ** Internal Header for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -118,5 +118,9 @@ LUA_API void *debug_realloc (void *ud, void *block,
#undef LUAI_USER_ALIGNMENT_T #undef LUAI_USER_ALIGNMENT_T
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; } #define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
#define STRCACHE_N 23
#define STRCACHE_M 5
#endif #endif