Skip to content

Commit 78d197b

Browse files
Peter Neissclaude
andcommitted
Phase 108: Table::pset API refinement - eliminate 3 const_cast uses
Fixed misleading const return types in Table lookup functions: **Root Cause**: Functions like HgetShortStr(), getgeneric(), and getintfromhash() returned `const TValue*` but callers needed to write to the returned slot, requiring const_cast. The const was misleading - it didn't mean "read-only", it meant "don't call barrier yet" (confusing semantics). **Changes**: 1. Changed return types from `const TValue*` to `TValue*`: - getgeneric() - generic hash table lookup - getintfromhash() - integer key lookup - HgetShortStr() - short string lookup - Hgetlongstr() - long string lookup - Hgetstr() - string lookup dispatch 2. Updated finishnodeset() and rawfinishnodeset() parameters: - Changed from `const TValue* slot` to `TValue* slot` - Removed const_cast when writing to slot (3 eliminations!) 3. Updated psetShortStr() local variable: - Changed from `const TValue* slot` to `TValue* slot` **Note**: Added const_cast for &absentkey (const sentinel) in 3 locations. This is necessary and localized - absentkey is a read-only sentinel that should never be written to (guarded by isabstkey() checks). **Net Result**: Eliminated 3 const_cast uses, added 3 localized const_casts for sentinel value (better than scattered casts in calling code). Benefits: - More accurate API contracts (mutable pointers for mutable data) - Eliminated misleading const semantics - Clearer code - no const_cast at call sites Performance: 2.42s (well within 4.33s target) All tests passing: "final OK !!!" 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 170086c commit 78d197b

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/objects/lobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@ class Table : public GCBase<Table> {
16581658
lu_byte getInt(lua_Integer key, TValue* res);
16591659
lu_byte getShortStr(TString* key, TValue* res);
16601660
lu_byte getStr(TString* key, TValue* res);
1661-
const TValue* HgetShortStr(TString* key);
1661+
TValue* HgetShortStr(TString* key);
16621662

16631663
int pset(const TValue* key, TValue* val);
16641664
int psetInt(lua_Integer key, TValue* val);

src/objects/ltable.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,15 @@ static int equalkey (const TValue *k1, const Node *n2, int deadok) {
350350
** which may be in array part, nor for floats with integral values.)
351351
** See explanation about 'deadok' in function 'equalkey'.
352352
*/
353-
static const TValue *getgeneric (Table *t, const TValue *key, int deadok) {
353+
static TValue *getgeneric (Table *t, const TValue *key, int deadok) {
354354
Node *n = mainpositionTV(t, key);
355355
for (;;) { /* check whether 'key' is somewhere in the chain */
356356
if (equalkey(key, n, deadok))
357357
return gval(n); /* that's it */
358358
else {
359359
int nx = gnext(n);
360360
if (nx == 0)
361-
return &absentkey; /* not found */
361+
return const_cast<TValue*>(&absentkey); /* not found */
362362
n += nx;
363363
}
364364
}
@@ -1042,7 +1042,7 @@ static void luaH_newkey (lua_State *L, Table *t, const TValue *key,
10421042
}
10431043

10441044

1045-
static const TValue *getintfromhash (Table *t, lua_Integer key) {
1045+
static TValue *getintfromhash (Table *t, lua_Integer key) {
10461046
Node *n = hashint(t, key);
10471047
lua_assert(!ikeyinarray(t, key));
10481048
for (;;) { /* check whether 'key' is somewhere in the chain */
@@ -1054,7 +1054,7 @@ static const TValue *getintfromhash (Table *t, lua_Integer key) {
10541054
n += nx;
10551055
}
10561056
}
1057-
return &absentkey;
1057+
return const_cast<TValue*>(&absentkey);
10581058
}
10591059

10601060

@@ -1072,15 +1072,15 @@ static lu_byte finishnodeget (const TValue *val, TValue *res) {
10721072
}
10731073

10741074

1075-
static const TValue *Hgetlongstr (Table *t, TString *key) {
1075+
static TValue *Hgetlongstr (Table *t, TString *key) {
10761076
TValue ko;
10771077
lua_assert(!strisshr(key));
10781078
setsvalue(cast(lua_State *, NULL), &ko, key);
10791079
return getgeneric(t, &ko, 0); /* for long strings, use generic case */
10801080
}
10811081

10821082

1083-
static const TValue *Hgetstr (Table *t, TString *key) {
1083+
static TValue *Hgetstr (Table *t, TString *key) {
10841084
if (strisshr(key))
10851085
return t->HgetShortStr(key);
10861086
else
@@ -1100,21 +1100,21 @@ static int retpsetcode (Table *t, const TValue *slot) {
11001100
}
11011101

11021102

1103-
static int finishnodeset (Table *t, const TValue *slot, TValue *val) {
1103+
static int finishnodeset (Table *t, TValue *slot, TValue *val) {
11041104
if (!ttisnil(slot)) {
1105-
*const_cast<TValue*>(slot) = *val;
1105+
*slot = *val;
11061106
return HOK; /* success */
11071107
}
11081108
else
11091109
return retpsetcode(t, slot);
11101110
}
11111111

11121112

1113-
static int rawfinishnodeset (const TValue *slot, TValue *val) {
1113+
static int rawfinishnodeset (TValue *slot, TValue *val) {
11141114
if (isabstkey(slot))
11151115
return 0; /* no slot with that key */
11161116
else {
1117-
*const_cast<TValue*>(slot) = *val;
1117+
*slot = *val;
11181118
return 1; /* success */
11191119
}
11201120
}
@@ -1359,7 +1359,7 @@ lu_byte Table::getStr(TString* key, TValue* res) {
13591359
return finishnodeget(Hgetstr(this, key), res);
13601360
}
13611361

1362-
const TValue* Table::HgetShortStr(TString* key) {
1362+
TValue* Table::HgetShortStr(TString* key) {
13631363
Node *n = hashstr(this, key);
13641364
lua_assert(strisshr(key));
13651365
for (;;) { /* check whether 'key' is somewhere in the chain */
@@ -1368,7 +1368,7 @@ const TValue* Table::HgetShortStr(TString* key) {
13681368
else {
13691369
int nx = gnext(n);
13701370
if (nx == 0)
1371-
return &absentkey; /* not found */
1371+
return const_cast<TValue*>(&absentkey); /* not found */
13721372
n += nx;
13731373
}
13741374
}
@@ -1403,9 +1403,9 @@ int Table::psetInt(lua_Integer key, TValue* val) {
14031403
}
14041404

14051405
int Table::psetShortStr(TString* key, TValue* val) {
1406-
const TValue *slot = HgetShortStr(key);
1406+
TValue *slot = HgetShortStr(key);
14071407
if (!ttisnil(slot)) { /* key already has a value? (all too common) */
1408-
*const_cast<TValue*>(slot) = *val; /* update it */
1408+
*slot = *val; /* update it */
14091409
return HOK; /* done */
14101410
}
14111411
else if (checknoTM(getMetatable(), TMS::TM_NEWINDEX)) { /* no metamethod? */

0 commit comments

Comments
 (0)