1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

Scanner and parser use different tables for constants

Moreover, each function being parsed has its own table.

The code is cleaner when each table is used for one specific purpose:
The scanner uses its table to anchor and unify strings, mapping strings
to themselves; the parser uses it to reuse constants in the code,
mapping constants to their indices in the constant table. A different
table for each task avoids false collisions.
This commit is contained in:
Roberto Ierusalimschy 2024-12-17 11:23:22 -03:00
parent 7538f3886d
commit 1c40ff9faa
6 changed files with 20 additions and 28 deletions

12
lcode.c
View File

@ -563,13 +563,13 @@ static int addk (FuncState *fs, Proto *f, TValue *v) {
static int k2proto (FuncState *fs, TValue *key, TValue *v) { static int k2proto (FuncState *fs, TValue *key, TValue *v) {
TValue val; TValue val;
Proto *f = fs->f; Proto *f = fs->f;
int tag = luaH_get(fs->ls->h, key, &val); /* query scanner table */ int tag = luaH_get(fs->kcache, key, &val); /* query scanner table */
int k; int k;
if (tag == LUA_VNUMINT) { /* is there an index there? */ if (!tagisempty(tag)) { /* is there an index there? */
k = cast_int(ivalue(&val)); k = cast_int(ivalue(&val));
lua_assert(k < fs->nk);
/* correct value? (warning: must distinguish floats from integers!) */ /* correct value? (warning: must distinguish floats from integers!) */
if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && if (ttypetag(&f->k[k]) == ttypetag(v) && luaV_rawequalobj(&f->k[k], v))
luaV_rawequalobj(&f->k[k], v))
return k; /* reuse index */ return k; /* reuse index */
} }
/* constant not found; create a new entry */ /* constant not found; create a new entry */
@ -577,7 +577,7 @@ static int k2proto (FuncState *fs, TValue *key, TValue *v) {
/* cache for reuse; numerical value does not need GC barrier; /* cache for reuse; numerical value does not need GC barrier;
table has no metatable, so it does not need to invalidate cache */ table has no metatable, so it does not need to invalidate cache */
setivalue(&val, k); setivalue(&val, k);
luaH_set(fs->ls->L, fs->ls->h, key, &val); luaH_set(fs->ls->L, fs->kcache, key, &val);
return k; return k;
} }
@ -659,7 +659,7 @@ static int nilK (FuncState *fs) {
TValue k, v; TValue k, v;
setnilvalue(&v); setnilvalue(&v);
/* cannot use nil as key; instead use table itself to represent nil */ /* cannot use nil as key; instead use table itself to represent nil */
sethvalue(fs->ls->L, &k, fs->ls->h); sethvalue(fs->ls->L, &k, fs->kcache);
return k2proto(fs, &k, &v); return k2proto(fs, &k, &v);
} }

15
llex.c
View File

@ -130,18 +130,15 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
** Creates a new string and anchors it in scanner's table so that it ** Creates a new string and anchors it in scanner's table so that it
** will not be collected until the end of the compilation; by that time ** will not be collected until the end of the compilation; by that time
** it should be anchored somewhere. It also internalizes long strings, ** it should be anchored somewhere. It also internalizes long strings,
** ensuring there is only one copy of each unique string. The table ** ensuring there is only one copy of each unique string.
** here is used as a set: the string enters as the key, while its value
** is irrelevant. We use the string itself as the value only because it
** is a TValue readily available. Later, the code generation can change
** this value.
*/ */
TString *luaX_newstring (LexState *ls, const char *str, size_t l) { TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
lua_State *L = ls->L; lua_State *L = ls->L;
TString *ts = luaS_newlstr(L, str, l); /* create new string */ TString *ts = luaS_newlstr(L, str, l); /* create new string */
TString *oldts = luaH_getstrkey(ls->h, ts); TValue oldts;
if (oldts != NULL) /* string already present? */ int tag = luaH_getstr(ls->h, ts, &oldts);
return oldts; /* use it */ if (!tagisempty(tag)) /* string already present? */
return tsvalue(&oldts); /* use stored value */
else { /* create a new entry */ else { /* create a new entry */
TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ TValue *stv = s2v(L->top.p++); /* reserve stack space for string */
setsvalue(L, stv, ts); /* temporarily anchor the string */ setsvalue(L, stv, ts); /* temporarily anchor the string */
@ -149,8 +146,8 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
/* table is not a metatable, so it does not need to invalidate cache */ /* table is not a metatable, so it does not need to invalidate cache */
luaC_checkGC(L); luaC_checkGC(L);
L->top.p--; /* remove string from stack */ L->top.p--; /* remove string from stack */
return ts;
} }
return ts;
} }

View File

@ -737,6 +737,7 @@ static void codeclosure (LexState *ls, expdesc *v) {
static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
lua_State *L = ls->L;
Proto *f = fs->f; Proto *f = fs->f;
fs->prev = ls->fs; /* linked list of funcstates */ fs->prev = ls->fs; /* linked list of funcstates */
fs->ls = ls; fs->ls = ls;
@ -757,8 +758,11 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
fs->firstlabel = ls->dyd->label.n; fs->firstlabel = ls->dyd->label.n;
fs->bl = NULL; fs->bl = NULL;
f->source = ls->source; f->source = ls->source;
luaC_objbarrier(ls->L, f, f->source); luaC_objbarrier(L, f, f->source);
f->maxstacksize = 2; /* registers 0/1 are always valid */ f->maxstacksize = 2; /* registers 0/1 are always valid */
fs->kcache = luaH_new(L); /* create table for function */
sethvalue2s(L, L->top.p, fs->kcache); /* anchor it */
luaD_inctop(L);
enterblock(fs, bl, 0); enterblock(fs, bl, 0);
} }
@ -780,6 +784,7 @@ static void close_func (LexState *ls) {
luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar);
luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc);
ls->fs = fs->prev; ls->fs = fs->prev;
L->top.p--; /* pop kcache table */
luaC_checkGC(L); luaC_checkGC(L);
} }

View File

@ -146,6 +146,7 @@ typedef struct FuncState {
struct FuncState *prev; /* enclosing function */ struct FuncState *prev; /* enclosing function */
struct LexState *ls; /* lexical state */ struct LexState *ls; /* lexical state */
struct BlockCnt *bl; /* chain of current blocks */ struct BlockCnt *bl; /* chain of current blocks */
Table *kcache; /* cache for reusing constants */
int pc; /* next position to code (equivalent to 'ncode') */ int pc; /* next position to code (equivalent to 'ncode') */
int lasttarget; /* 'label' of last 'jump label' */ int lasttarget; /* 'label' of last 'jump label' */
int previousline; /* last line that was saved in 'lineinfo' */ int previousline; /* last line that was saved in 'lineinfo' */

View File

@ -962,7 +962,7 @@ lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res) {
*/ */
const TValue *luaH_Hgetshortstr (Table *t, TString *key) { const TValue *luaH_Hgetshortstr (Table *t, TString *key) {
Node *n = hashstr(t, key); Node *n = hashstr(t, key);
lua_assert(key->tt == LUA_VSHRSTR); lua_assert(strisshr(key));
for (;;) { /* check whether 'key' is somewhere in the chain */ for (;;) { /* check whether 'key' is somewhere in the chain */
if (keyisshrstr(n) && eqshrstr(keystrval(n), key)) if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
return gval(n); /* that's it */ return gval(n); /* that's it */
@ -997,15 +997,6 @@ lu_byte luaH_getstr (Table *t, TString *key, TValue *res) {
} }
TString *luaH_getstrkey (Table *t, TString *key) {
const TValue *o = Hgetstr(t, key);
if (!isabstkey(o)) /* string already present? */
return keystrval(nodefromval(o)); /* get saved copy */
else
return NULL;
}
/* /*
** main search function ** main search function
*/ */

View File

@ -154,8 +154,6 @@ LUAI_FUNC lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res);
/* Special get for metamethods */ /* Special get for metamethods */
LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key); LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key);
LUAI_FUNC TString *luaH_getstrkey (Table *t, TString *key);
LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val); LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val);
LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val); LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val);
LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val); LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val);