new cache for interning strings

This commit is contained in:
Roberto Ierusalimschy
2015-03-04 10:31:21 -03:00
parent a00013c8d0
commit a80cada914
6 changed files with 61 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
** $Id: lstring.c,v 2.45 2014/11/02 19:19:04 roberto Exp roberto $
** $Id: lstring.c,v 2.46 2015/01/16 16:54:37 roberto Exp roberto $
** String table (keeps all strings handled by Lua)
** See Copyright Notice in lua.h
*/
@@ -22,6 +22,8 @@
#include "lstring.h"
#define MEMERRMSG "not enough memory"
/*
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
@@ -85,6 +87,21 @@ void luaS_resize (lua_State *L, int newsize) {
}
/*
** Initialize the string table and the string cache
*/
void luaS_init (lua_State *L) {
global_State *g = G(L);
int i;
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++)
g->strcache[i] = g->memerrmsg; /* fill cache with valid strings */
}
/*
** creates a new string object
@@ -163,10 +180,21 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
/*
** new zero-terminated string
** Create or reuse a zero-terminated string, first checking in the
** cache (using the string address as a key). The cache can contain
** only zero-terminated strings, so it is safe to use 'strcmp' to
** check hits.
*/
TString *luaS_new (lua_State *L, const char *str) {
return luaS_newlstr(L, str, strlen(str));
unsigned int i = point2uint(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;
}
}