From 1c40ff9faafed620aa0458b397bcbfbe19e0f663 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 17 Dec 2024 11:23:22 -0300 Subject: 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. --- lcode.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lcode.c') diff --git a/lcode.c b/lcode.c index e6a98bb6..8f08302e 100644 --- a/lcode.c +++ b/lcode.c @@ -563,13 +563,13 @@ static int addk (FuncState *fs, Proto *f, TValue *v) { static int k2proto (FuncState *fs, TValue *key, TValue *v) { TValue val; 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; - if (tag == LUA_VNUMINT) { /* is there an index there? */ + if (!tagisempty(tag)) { /* is there an index there? */ k = cast_int(ivalue(&val)); + lua_assert(k < fs->nk); /* correct value? (warning: must distinguish floats from integers!) */ - if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && - luaV_rawequalobj(&f->k[k], v)) + if (ttypetag(&f->k[k]) == ttypetag(v) && luaV_rawequalobj(&f->k[k], v)) return k; /* reuse index */ } /* 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; table has no metatable, so it does not need to invalidate cache */ setivalue(&val, k); - luaH_set(fs->ls->L, fs->ls->h, key, &val); + luaH_set(fs->ls->L, fs->kcache, key, &val); return k; } @@ -659,7 +659,7 @@ static int nilK (FuncState *fs) { TValue k, v; setnilvalue(&v); /* 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); } -- cgit v1.2.3-55-g6feb