diff --git a/src/blua/lgc.c b/src/blua/lgc.c index 1d1087e5b..cb7a878f9 100644 --- a/src/blua/lgc.c +++ b/src/blua/lgc.c @@ -522,6 +522,19 @@ static void remarkupvals (global_State *g) { } +/* +** Clear API string cache. (Entries cannot be empty, so fill them with +** a non-collectable string.) +*/ +static void clearapihash (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 */ + } +} + + static void atomic (lua_State *L) { global_State *g = G(L); size_t udsize; /* total size of userdata to be finalized */ @@ -544,6 +557,7 @@ static void atomic (lua_State *L) { marktmu(g); /* mark `preserved' userdata */ udsize += propagateall(g); /* remark, to propagate `preserveness' */ cleartable(g->weak); /* remove collected objects from weak tables */ + clearapihash(g); /* flip current white */ g->currentwhite = cast_byte(otherwhite(g)); g->sweepstrgc = 0; diff --git a/src/blua/llimits.h b/src/blua/llimits.h index 20475d41f..a884ce592 100644 --- a/src/blua/llimits.h +++ b/src/blua/llimits.h @@ -100,6 +100,12 @@ typedef lu_int32 Instruction; #endif +/* Size of cache for strings in the API (better be a prime) */ +#ifndef STRCACHE_SIZE +#define STRCACHE_SIZE 127 +#endif + + /* minimum size for string buffer */ #ifndef LUA_MINBUFFER #define LUA_MINBUFFER 32 diff --git a/src/blua/lstate.c b/src/blua/lstate.c index b3482c6d6..633724ed7 100644 --- a/src/blua/lstate.c +++ b/src/blua/lstate.c @@ -64,9 +64,6 @@ static void freestack (lua_State *L, lua_State *L1) { } -#define MEMERRMSG "not enough memory" - - /* ** open parts that may cause memory-allocation errors */ @@ -76,12 +73,9 @@ static void f_luaopen (lua_State *L, void *ud) { stack_init(L, L); /* init stack */ sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ - luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ + luaS_init(L); luaT_init(L); luaX_init(L); - /* pre-create memory-error message */ - g->memerrmsg = luaS_newliteral(L, MEMERRMSG); - luaS_fix(g->memerrmsg); /* it should never be collected */ g->GCthreshold = 4*g->totalbytes; } diff --git a/src/blua/lstate.h b/src/blua/lstate.h index 7cfafdb94..21fb4920c 100644 --- a/src/blua/lstate.h +++ b/src/blua/lstate.h @@ -92,6 +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 */ } global_State; diff --git a/src/blua/lstring.c b/src/blua/lstring.c index b1249238b..4cec90597 100644 --- a/src/blua/lstring.c +++ b/src/blua/lstring.c @@ -18,6 +18,9 @@ #include "lstring.h" +#define MEMERRMSG "not enough memory" + + void luaS_resize (lua_State *L, int newsize) { GCObject **newhash; @@ -72,6 +75,21 @@ static TString *newlstr (lua_State *L, const char *str, size_t l, } +/* +** 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); + 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 */ +} + + // string hash taken from luaJIT /* Unaligned load of uint32_t. */ @@ -128,6 +146,10 @@ static inline __attribute__((always_inline)) unsigned luaS_hash (const char *str #endif } + +/* +** new string (with explicit length) +*/ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { GCObject *o; unsigned int h = luaS_hash(str, l); @@ -147,6 +169,25 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { } +/* +** 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) { + 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; + } +} + + Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { Udata *u; if (l_unlikely(s > MAX_SIZET - sizeof(Udata))) diff --git a/src/blua/lstring.h b/src/blua/lstring.h index 73a2ff8b3..d794e158a 100644 --- a/src/blua/lstring.h +++ b/src/blua/lstring.h @@ -17,15 +17,18 @@ #define sizeudata(u) (sizeof(union Udata)+(u)->len) -#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) +//#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) -#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) +#define luaS_iswhite(x) test2bits((x)->tsv.marked, WHITE0BIT, WHITE1BIT) +#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) LUAI_FUNC void luaS_resize (lua_State *L, int newsize); +LUAI_FUNC void luaS_init (lua_State *L); LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); +LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); #endif