Skip to content

Commit e4db34b

Browse files
Peter Neissclaude
andcommitted
Phase 122: Convert luaH_* functions to Table member methods
CHANGES: - Removed all 22 luaH_* wrapper functions from ltable.cpp - Updated ~48 call sites across 20 files to use Table member methods - Moved fastGeti/fastSeti implementations to lobject.h as Table methods - Added const overload for Table::HgetShortStr (metamethod lookup) - Converted function pointer arguments to lambdas in hot paths CALL SITE UPDATES: - lapi.cpp: 11 conversions (luaH_getn → t->getn(), etc.) - lstate.cpp: 5 conversions (registry initialization) - ltm.cpp: 3 conversions (CRITICAL metamethod lookups) - lvm.cpp: 10 conversions (bytecode interpreter hot path) - ldebug.cpp, lcode.cpp, llex.cpp: Table::create usage - ldump.cpp, lundump.cpp: serialization updates - lvm_table.cpp: metamethod chain lambdas - lvm_string.cpp, gc_core.cpp: getn/size conversions - ltests.cpp: mainPosition conversion TECHNICAL DETAILS: - fastGeti/fastSeti in lobject.h (after ltm.h) due to TMS dependency - Lambda parameter naming to avoid shadowing (tbl, k, v) - Variable renaming in tableNext/getn to avoid member shadowing - Internal rehash() now calls t->resize() instead of luaH_resize() TESTING: - All tests pass: "final OK !!!" - Performance: 2.26s avg (5 runs) - excellent! ✅ - Target: ≤4.33s (well under target) - Zero regressions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9ef5202 commit e4db34b

File tree

20 files changed

+205
-321
lines changed

20 files changed

+205
-321
lines changed

src/compiler/lcode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ int FuncState::addk(Proto *proto, TValue *v) {
337337
int FuncState::k2proto(TValue *key, TValue *v) {
338338
TValue val;
339339
Proto *proto = getProto();
340-
LuaT tag = luaH_get(getKCache(), key, &val); /* query scanner table */
340+
LuaT tag = getKCache()->get(key, &val); /* query scanner table */
341341
if (!tagisempty(tag)) { /* is there an index there? */
342342
auto k = cast_int(ivalue(&val));
343343
/* collisions can happen only for float keys */
@@ -349,7 +349,7 @@ int FuncState::k2proto(TValue *key, TValue *v) {
349349
/* cache it for reuse; numerical value does not need GC barrier;
350350
table is not a metatable, so it does not need to invalidate cache */
351351
val.setInt(k);
352-
luaH_set(getLexState()->getLuaState(), getKCache(), key, &val);
352+
getKCache()->set(getLexState()->getLuaState(), key, &val);
353353
return k;
354354
}
355355
}

src/compiler/llex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ l_noret LexState::syntaxError(const char *msg) {
119119
TString* LexState::anchorStr(TString *ts) {
120120
lua_State *luaState = getLuaState();
121121
TValue oldts;
122-
LuaT tag = luaH_getstr(getTable(), ts, &oldts);
122+
LuaT tag = getTable()->getStr(ts, &oldts);
123123
if (!tagisempty(tag)) /* string already present? */
124124
return tsvalue(&oldts); /* use stored value */
125125
else { /* create a new entry */
126126
TValue *stv = s2v(luaState->getTop().p); /* reserve stack space for string */
127127
luaState->getStackSubsystem().push();
128128
setsvalue(luaState, stv, ts); /* push (anchor) the string on the stack */
129-
luaH_set(luaState, getTable(), stv, stv); /* t[string] = string */
129+
getTable()->set(luaState, stv, stv); /* t[string] = string */
130130
/* table is not a metatable, so it does not need to invalidate cache */
131131
luaC_checkGC(luaState);
132132
luaState->getStackSubsystem().pop(); /* remove string from stack */

src/compiler/parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ void Parser::open_func(FuncState *funcstate, BlockCnt *bl) {
460460
f->setSource(ls->getSource());
461461
luaC_objbarrier(state, f, f->getSource());
462462
f->setMaxStackSize(2); /* registers 0/1 are always valid */
463-
funcstate->setKCache(luaH_new(state)); /* create table for function */
463+
funcstate->setKCache(Table::create(state)); /* create table for function */
464464
sethvalue2s(state, state->getTop().p, funcstate->getKCache()); /* anchor it */
465465
state->inctop(); /* Phase 25e */
466466
funcstate->enterblock(bl, 0);

src/compiler/parseutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
185185
LClosure *cl = LClosure::create(L, 1); /* create main closure */
186186
setclLvalue2s(L, L->getTop().p, cl); /* anchor it (to avoid being collected) */
187187
L->inctop(); /* Phase 25e */
188-
lexstate.setTable(luaH_new(L)); /* create table for scanner */
188+
lexstate.setTable(Table::create(L)); /* create table for scanner */
189189
sethvalue2s(L, L->getTop().p, lexstate.getTable()); /* anchor it */
190190
L->inctop(); /* Phase 25e */
191191
funcstate.setProto(luaF_newproto(L));

src/core/lapi.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
389389
case LuaT::TABLE: {
390390
lua_Unsigned res;
391391
lua_lock(L);
392-
res = luaH_getn(L, hvalue(o));
392+
res = hvalue(o)->getn(L);
393393
lua_unlock(L);
394394
return res;
395395
}
@@ -614,7 +614,7 @@ LUA_API int lua_pushthread (lua_State *L) {
614614
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
615615
LuaT tag;
616616
TString *str = TString::create(L, k);
617-
tag = luaV_fastget(t, str, s2v(L->getTop().p), luaH_getstr);
617+
tag = luaV_fastget(t, str, s2v(L->getTop().p), [](Table* tbl, TString* strkey, TValue* res) { return tbl->getStr(strkey, res); });
618618
if (!tagisempty(tag))
619619
api_incr_top(L);
620620
else {
@@ -634,7 +634,7 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
634634
*/
635635
static void getGlobalTable (lua_State *L, TValue *gt) {
636636
Table *registry = hvalue(G(L)->getRegistry());
637-
LuaT tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
637+
LuaT tag = registry->getInt(LUA_RIDX_GLOBALS, gt);
638638
(void)tag; /* avoid not-used warnings when checks are off */
639639
api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
640640
}
@@ -652,7 +652,7 @@ LUA_API int lua_gettable (lua_State *L, int idx) {
652652
lua_lock(L);
653653
api_checkpop(L, 1);
654654
TValue *t = L->getStackSubsystem().indexToValue(L,idx);
655-
LuaT tag = luaV_fastget(t, s2v(L->getTop().p - 1), s2v(L->getTop().p - 1), luaH_get);
655+
LuaT tag = luaV_fastget(t, s2v(L->getTop().p - 1), s2v(L->getTop().p - 1), [](Table* tbl, const TValue* key, TValue* res) { return tbl->get(key, res); });
656656
if (tagisempty(tag))
657657
tag = luaV_finishget(L, t, s2v(L->getTop().p - 1), L->getTop().p - 1, tag);
658658
lua_unlock(L);
@@ -702,7 +702,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) {
702702
lua_lock(L);
703703
api_checkpop(L, 1);
704704
Table *t = gettable(L, idx);
705-
LuaT tag = luaH_get(t, s2v(L->getTop().p - 1), s2v(L->getTop().p - 1));
705+
LuaT tag = t->get(s2v(L->getTop().p - 1), s2v(L->getTop().p - 1));
706706
L->getStackSubsystem().pop(); /* pop key */
707707
return finishrawget(L, tag);
708708
}
@@ -712,7 +712,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
712712
lua_lock(L);
713713
Table *t = gettable(L, idx);
714714
LuaT tag;
715-
luaH_fastgeti(t, n, s2v(L->getTop().p), tag);
715+
t->fastGeti(n, s2v(L->getTop().p), tag);
716716
return finishrawget(L, tag);
717717
}
718718

@@ -722,17 +722,17 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
722722
Table *t = gettable(L, idx);
723723
TValue k;
724724
setpvalue(&k, cast_voidp(p));
725-
return finishrawget(L, luaH_get(t, &k, s2v(L->getTop().p)));
725+
return finishrawget(L, t->get(&k, s2v(L->getTop().p)));
726726
}
727727

728728

729729
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
730730
lua_lock(L);
731-
Table *t = luaH_new(L);
731+
Table *t = Table::create(L);
732732
sethvalue2s(L, L->getTop().p, t);
733733
api_incr_top(L);
734734
if (narray > 0 || nrec > 0)
735-
luaH_resize(L, t, cast_uint(narray), cast_uint(nrec));
735+
t->resize(L, cast_uint(narray), cast_uint(nrec));
736736
luaC_checkGC(L);
737737
lua_unlock(L);
738738
}
@@ -796,7 +796,7 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
796796
int hres;
797797
TString *str = TString::create(L, k);
798798
api_checkpop(L, 1);
799-
hres = luaV_fastset(t, str, s2v(L->getTop().p - 1), luaH_psetstr);
799+
hres = luaV_fastset(t, str, s2v(L->getTop().p - 1), [](Table* tbl, TString* strkey, TValue* val) { return tbl->psetStr(strkey, val); });
800800
if (hres == HOK) {
801801
luaV_finishfastset(L, t, s2v(L->getTop().p - 1));
802802
L->getStackSubsystem().pop(); /* pop value */
@@ -823,7 +823,7 @@ LUA_API void lua_settable (lua_State *L, int idx) {
823823
lua_lock(L);
824824
api_checkpop(L, 2);
825825
TValue *t = L->getStackSubsystem().indexToValue(L,idx);
826-
int hres = luaV_fastset(t, s2v(L->getTop().p - 2), s2v(L->getTop().p - 1), luaH_pset);
826+
int hres = luaV_fastset(t, s2v(L->getTop().p - 2), s2v(L->getTop().p - 1), [](Table* tbl, const TValue* key, TValue* val) { return tbl->pset(key, val); });
827827
if (hres == HOK)
828828
luaV_finishfastset(L, t, s2v(L->getTop().p - 1));
829829
else
@@ -861,7 +861,7 @@ static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
861861
lua_lock(L);
862862
api_checkpop(L, n);
863863
Table *t = gettable(L, idx);
864-
luaH_set(L, t, key, s2v(L->getTop().p - 1));
864+
t->set(L, key, s2v(L->getTop().p - 1));
865865
invalidateTMcache(t);
866866
luaC_barrierback(L, obj2gco(t), s2v(L->getTop().p - 1));
867867
L->getStackSubsystem().popN(n);
@@ -885,7 +885,7 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
885885
lua_lock(L);
886886
api_checkpop(L, 1);
887887
Table *t = gettable(L, idx);
888-
luaH_setint(L, t, n, s2v(L->getTop().p - 1));
888+
t->setInt(L, n, s2v(L->getTop().p - 1));
889889
luaC_barrierback(L, obj2gco(t), s2v(L->getTop().p - 1));
890890
L->getStackSubsystem().pop();
891891
lua_unlock(L);
@@ -1197,7 +1197,7 @@ LUA_API int lua_next (lua_State *L, int idx) {
11971197
lua_lock(L);
11981198
api_checkpop(L, 1);
11991199
Table *t = gettable(L, idx);
1200-
int more = luaH_next(L, t, L->getTop().p - 1);
1200+
int more = t->tableNext(L, L->getTop().p - 1);
12011201
if (more)
12021202
api_incr_top(L);
12031203
else /* no more elements */

src/core/ldebug.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ static void collectvalidlines (lua_State *L, Closure *f) {
312312
else {
313313
const Proto *p = reinterpret_cast<LClosure*>(f)->getProto();
314314
int currentline = p->getLineDefined();
315-
Table *t = luaH_new(L); /* new table to store active lines */
315+
Table *t = Table::create(L); /* new table to store active lines */
316316
sethvalue2s(L, L->getTop().p, t); /* push it on stack */
317317
api_incr_top(L);
318318
auto lineInfoSpan = p->getDebugInfo().getLineInfoSpan();
@@ -330,7 +330,7 @@ static void collectvalidlines (lua_State *L, Closure *f) {
330330
}
331331
for (; i < lineInfoSpan.size(); i++) { /* for each instruction */
332332
currentline = nextline(p, currentline, i); /* get its line */
333-
luaH_setint(L, t, currentline, &v); /* table[line] = true */
333+
t->setInt(L, currentline, &v); /* table[line] = true */
334334
}
335335
}
336336
}

src/core/lstate.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,18 @@ static void freestack (lua_State *L) {
191191
static void init_registry (lua_State *L, global_State *g) {
192192
/* create registry */
193193
TValue aux;
194-
Table *registry = luaH_new(L);
194+
Table *registry = Table::create(L);
195195
sethvalue(L, g->getRegistry(), registry);
196-
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
196+
registry->resize(L, LUA_RIDX_LAST, 0);
197197
/* registry[1] = false */
198198
setbfvalue(&aux);
199-
luaH_setint(L, registry, 1, &aux);
199+
registry->setInt(L, 1, &aux);
200200
/* registry[LUA_RIDX_MAINTHREAD] = L */
201201
setthvalue(L, &aux, L);
202-
luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &aux);
202+
registry->setInt(L, LUA_RIDX_MAINTHREAD, &aux);
203203
/* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
204-
sethvalue(L, &aux, luaH_new(L));
205-
luaH_setint(L, registry, LUA_RIDX_GLOBALS, &aux);
204+
sethvalue(L, &aux, Table::create(L));
205+
registry->setInt(L, LUA_RIDX_GLOBALS, &aux);
206206
}
207207

208208

src/core/ltm.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void luaT_init (lua_State *L) {
5959
** tag methods
6060
*/
6161
const TValue *luaT_gettm (const Table *events, TMS event, TString *ename) {
62-
const TValue *tm = luaH_Hgetshortstr(events, ename);
62+
const TValue *tm = events->HgetShortStr(ename);
6363
lua_assert(event <= TMS::TM_EQ);
6464
if (notm(tm)) { /* no tag method? */
6565
events->setFlagBits(1 << static_cast<int>(event)); /* cache this fact (flags is mutable) */
@@ -81,7 +81,7 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
8181
default:
8282
mt = G(L)->getMetatable(ttype(o));
8383
}
84-
return (mt ? luaH_Hgetshortstr(mt, G(L)->getTMName(static_cast<int>(event))) : G(L)->getNilValue());
84+
return (mt ? mt->HgetShortStr(G(L)->getTMName(static_cast<int>(event))) : G(L)->getNilValue());
8585
}
8686

8787

@@ -93,7 +93,7 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) {
9393
Table *mt;
9494
if ((ttistable(o) && (mt = hvalue(o)->getMetatable()) != nullptr) ||
9595
(ttisfulluserdata(o) && (mt = uvalue(o)->getMetatable()) != nullptr)) {
96-
const TValue *name = luaH_Hgetshortstr(mt, TString::create(L, "__name"));
96+
const TValue *name = mt->HgetShortStr(TString::create(L, "__name"));
9797
if (ttisstring(name)) /* is '__name' a string? */
9898
return getstr(tsvalue(name)); /* use it as type name */
9999
}

src/memory/gc/gc_core.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ l_mem GCCore::objsize(GCObject* o) {
2626
lu_mem res;
2727
switch (static_cast<int>(o->getType())) {
2828
case static_cast<int>(ctb(LuaT::TABLE)): {
29-
res = luaH_size(gco2t(o));
29+
res = gco2t(o)->size();
3030
break;
3131
}
3232
case static_cast<int>(ctb(LuaT::LCL)): {

src/memory/lgc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ void freeobj (lua_State *L, GCObject *o) {
417417
}
418418
case static_cast<int>(ctb(LuaT::TABLE)): {
419419
Table *t = gco2t(o);
420-
luaH_free(L, t); // Note: luaH_free calls destroy() which should handle cleanup
420+
t->destroy(L); // Destroy table and cleanup
421421
break;
422422
}
423423
case static_cast<int>(ctb(LuaT::THREAD)):

0 commit comments

Comments
 (0)