no more reference 'memerrmsg' + new reference to "n"

(both can be retrieved by 'luaS_newliteral' without creating anything,
because they are fixed, but "n" deserves fast access while 'memerrmsg'
does not)
This commit is contained in:
Roberto Ierusalimschy
2017-07-27 10:50:16 -03:00
parent 11769b203f
commit 6d998055c8
5 changed files with 24 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/*
** $Id: lstring.c,v 2.55 2015/11/03 15:36:01 roberto Exp roberto $
** $Id: lstring.c,v 2.56 2015/11/23 11:32:51 roberto Exp roberto $
** String table (keeps all strings handled by Lua)
** See Copyright Notice in lua.h
*/
@@ -22,9 +22,6 @@
#include "lstring.h"
#define MEMERRMSG "not enough memory"
/*
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
** compute its hash
@@ -105,7 +102,7 @@ void luaS_clearcache (global_State *g) {
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 */
g->strcache[i][j] = g->nfield; /* replace it with something fixed */
}
}
@@ -116,13 +113,16 @@ void luaS_clearcache (global_State *g) {
void luaS_init (lua_State *L) {
global_State *g = G(L);
int i, j;
TString *memerrmsg;
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 */
memerrmsg = luaS_newliteral(L, MEMERRMSG);
luaC_fix(L, obj2gco(memerrmsg)); /* it should never be collected */
g->nfield = luaS_newliteral(L, "n"); /* pre-create "n" field name */
luaC_fix(L, obj2gco(g->nfield)); /* it also should never be collected */
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;
g->strcache[i][j] = g->nfield;
}