diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-09-22 11:18:24 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-09-22 11:18:24 -0300 |
| commit | 9fae7b6d3fca02b4661aaa7c16e9fbeec8964b9b (patch) | |
| tree | 03898a73c1aa5c7ae600e1ba11a514b1c50edeef /lstring.c | |
| parent | 0f1f51be4b1e2fb561b2ec6dce7fe292d5b3764d (diff) | |
| download | lua-9fae7b6d3fca02b4661aaa7c16e9fbeec8964b9b.tar.gz lua-9fae7b6d3fca02b4661aaa7c16e9fbeec8964b9b.tar.bz2 lua-9fae7b6d3fca02b4661aaa7c16e9fbeec8964b9b.zip | |
code for string cache generalized for "associative sets" (compiler
will optimize away or inline the extra loops)
Diffstat (limited to 'lstring.c')
| -rw-r--r-- | lstring.c | 38 |
1 files changed, 22 insertions, 16 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstring.c,v 2.51 2015/09/08 15:41:05 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 2.52 2015/09/17 15:51:05 roberto Exp roberto $ |
| 3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -92,11 +92,12 @@ void luaS_resize (lua_State *L, int newsize) { | |||
| 92 | ** a non-collectable string.) | 92 | ** a non-collectable string.) |
| 93 | */ | 93 | */ |
| 94 | void luaS_clearcache (global_State *g) { | 94 | void luaS_clearcache (global_State *g) { |
| 95 | int i; | 95 | int i, j; |
| 96 | for (i = 0; i < STRCACHE_SIZE; i++) { | 96 | for (i = 0; i < STRCACHE_N; i++) |
| 97 | if (iswhite(g->strcache[i][0])) /* will entry be collected? */ | 97 | for (j = 0; j < STRCACHE_M; j++) { |
| 98 | g->strcache[i][0] = g->memerrmsg; /* replace it with something fixed */ | 98 | if (iswhite(g->strcache[i][j])) /* will entry be collected? */ |
| 99 | } | 99 | g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ |
| 100 | } | ||
| 100 | } | 101 | } |
| 101 | 102 | ||
| 102 | 103 | ||
| @@ -105,13 +106,14 @@ void luaS_clearcache (global_State *g) { | |||
| 105 | */ | 106 | */ |
| 106 | void luaS_init (lua_State *L) { | 107 | void luaS_init (lua_State *L) { |
| 107 | global_State *g = G(L); | 108 | global_State *g = G(L); |
| 108 | int i; | 109 | int i, j; |
| 109 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ | 110 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ |
| 110 | /* pre-create memory-error message */ | 111 | /* pre-create memory-error message */ |
| 111 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); | 112 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); |
| 112 | luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ | 113 | luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ |
| 113 | for (i = 0; i < STRCACHE_SIZE; i++) /* fill cache with valid strings */ | 114 | for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ |
| 114 | g->strcache[i][0] = g->memerrmsg; | 115 | for (j = 0; j < STRCACHE_M; j++) |
| 116 | g->strcache[i][j] = g->memerrmsg; | ||
| 115 | } | 117 | } |
| 116 | 118 | ||
| 117 | 119 | ||
| @@ -205,15 +207,19 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
| 205 | ** check hits. | 207 | ** check hits. |
| 206 | */ | 208 | */ |
| 207 | TString *luaS_new (lua_State *L, const char *str) { | 209 | TString *luaS_new (lua_State *L, const char *str) { |
| 208 | unsigned int i = point2uint(str) % STRCACHE_SIZE; /* hash */ | 210 | unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ |
| 211 | int j; | ||
| 209 | TString **p = G(L)->strcache[i]; | 212 | TString **p = G(L)->strcache[i]; |
| 210 | if (strcmp(str, getstr(p[0])) == 0) /* hit? */ | 213 | for (j = 0; j < STRCACHE_M; j++) { |
| 211 | return p[0]; /* that it is */ | 214 | if (strcmp(str, getstr(p[j])) == 0) /* hit? */ |
| 212 | else { /* normal route */ | 215 | return p[j]; /* that is it */ |
| 213 | TString *s = luaS_newlstr(L, str, strlen(str)); | ||
| 214 | p[0] = s; | ||
| 215 | return s; | ||
| 216 | } | 216 | } |
| 217 | /* normal route */ | ||
| 218 | for (j = STRCACHE_M - 1; j > 0; j--) | ||
| 219 | p[j] = p[j - 1]; /* move out last element */ | ||
| 220 | /* new element is first in the list */ | ||
| 221 | p[0] = luaS_newlstr(L, str, strlen(str)); | ||
| 222 | return p[0]; | ||
| 217 | } | 223 | } |
| 218 | 224 | ||
| 219 | 225 | ||
