From 9fae7b6d3fca02b4661aaa7c16e9fbeec8964b9b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 22 Sep 2015 11:18:24 -0300 Subject: [PATCH] code for string cache generalized for "associative sets" (compiler will optimize away or inline the extra loops) --- llimits.h | 11 +++++++---- lstate.h | 4 ++-- lstring.c | 38 ++++++++++++++++++++++---------------- ltests.h | 6 +++++- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/llimits.h b/llimits.h index 29872ca3..d1dff19f 100644 --- a/llimits.h +++ b/llimits.h @@ -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 ** 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) -#define STRCACHE_SIZE 127 +#if !defined(STRCACHE_N) +#define STRCACHE_N 63 +#define STRCACHE_M 2 #endif diff --git a/lstate.h b/lstate.h index 61dd99f9..33351ca3 100644 --- a/lstate.h +++ b/lstate.h @@ -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 ** See Copyright Notice in lua.h */ @@ -140,7 +140,7 @@ typedef struct global_State { TString *memerrmsg; /* memory-error message */ TString *tmname[TM_N]; /* array with tag-method names */ 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; diff --git a/lstring.c b/lstring.c index 9802d9a8..b1b084d2 100644 --- a/lstring.c +++ b/lstring.c @@ -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) ** See Copyright Notice in lua.h */ @@ -92,11 +92,12 @@ void luaS_resize (lua_State *L, int newsize) { ** a non-collectable string.) */ void luaS_clearcache (global_State *g) { - int i; - for (i = 0; i < STRCACHE_SIZE; i++) { - if (iswhite(g->strcache[i][0])) /* will entry be collected? */ - g->strcache[i][0] = 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 (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) { 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); luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ - for (i = 0; i < STRCACHE_SIZE; i++) /* fill cache with valid strings */ - g->strcache[i][0] = g->memerrmsg; + 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; } @@ -205,15 +207,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 = point2uint(str) % STRCACHE_SIZE; /* hash */ + unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ + int j; TString **p = G(L)->strcache[i]; - if (strcmp(str, getstr(p[0])) == 0) /* hit? */ - return p[0]; /* that it is */ - else { /* normal route */ - TString *s = luaS_newlstr(L, str, strlen(str)); - p[0] = s; - return s; + 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]; } diff --git a/ltests.h b/ltests.h index d3404aa4..2ebe1181 100644 --- a/ltests.h +++ b/ltests.h @@ -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 ** See Copyright Notice in lua.h */ @@ -118,5 +118,9 @@ LUA_API void *debug_realloc (void *ud, void *block, #undef LUAI_USER_ALIGNMENT_T #define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; } + +#define STRCACHE_N 23 +#define STRCACHE_M 5 + #endif