Skip to content

Commit 4bf8174

Browse files
Peter Neissclaude
andcommitted
Phase 107: const-correctness fixes - eliminate 7 const_cast uses
Removed unnecessary const_cast by fixing function signatures: 1. TString::equals const-correctness (4 casts eliminated) - Changed signature: bool equals(const TString*) const - Removed const_cast from lobject.h lines 1866, 1885, 1922 - Method only reads data, should be const 2. luaH_size const-correctness (1 cast eliminated) - Changed signature: lu_mem luaH_size(const Table*) - Also made sizehash() const-correct - Removed const_cast from Table::size() 3. global_State logical mutability (1 cast eliminated) - Marked l_G field as mutable in lua_State - GC can happen during any operation, even const ones - Updated getGlobalState() const to return non-const - Removed const_cast from G() helper function 4. Udata::getMemory const-correctness (1 cast eliminated) - getudatamem() already had const overload - Removed unnecessary const_cast by calling correct overload Benefits: - Better const-correctness throughout codebase - Eliminated 7 unnecessary const_casts - More accurate API contracts (const means read-only) - Zero performance impact Performance: 2.35s avg (44% faster than 4.20s baseline) All tests passing: "final OK !!!" 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6916f04 commit 4bf8174

5 files changed

Lines changed: 13 additions & 13 deletions

File tree

src/core/lstate.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ struct lua_State : public GCBase<lua_State> {
399399
CallInfo base_ci; /* CallInfo for first level (C host) */
400400

401401
// Step 3: GC and state management fields (encapsulated)
402-
global_State *l_G;
402+
mutable global_State *l_G; /* mutable: GC can happen during any operation */
403403
UpVal *openupval; /* list of open upvalues in this stack */
404404
GCObject *gclist;
405405
lua_State *twups; /* list of threads with open upvalues */
@@ -507,7 +507,7 @@ struct lua_State : public GCBase<lua_State> {
507507

508508
// Step 3: GC and state management field accessors
509509
global_State* getGlobalState() noexcept { return l_G; }
510-
const global_State* getGlobalState() const noexcept { return l_G; }
510+
global_State* getGlobalState() const noexcept { return l_G; } // mutable field
511511
void setGlobalState(global_State* g) noexcept { l_G = g; }
512512
global_State*& getGlobalStateRef() noexcept { return l_G; } // For G() macro
513513

@@ -1199,7 +1199,7 @@ class global_State {
11991199

12001200
/* Get global state from lua_State (returns reference to allow assignment) */
12011201
inline global_State*& G(lua_State* L) noexcept { return L->getGlobalStateRef(); }
1202-
inline global_State* G(const lua_State* L) noexcept { return const_cast<global_State*>(L->getGlobalState()); }
1202+
inline global_State* G(const lua_State* L) noexcept { return L->getGlobalState(); }
12031203

12041204
/* Get main thread from global_State */
12051205
inline lua_State* mainthread(global_State* g) noexcept { return &g->getMainThread()->l; }

src/objects/lobject.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ class TString : public GCBase<TString> {
539539

540540
// Method declarations (implemented in lstring.cpp)
541541
unsigned hashLongStr();
542-
bool equals(TString* other);
542+
bool equals(const TString* other) const;
543543
void remove(lua_State* L); // Phase 25a: from luaS_remove
544544
TString* normalize(lua_State* L); // Phase 25a: from luaS_normstr
545545

@@ -772,7 +772,7 @@ inline void* Udata::getMemory() noexcept {
772772
return getudatamem(this);
773773
}
774774
inline const void* Udata::getMemory() const noexcept {
775-
return getudatamem(const_cast<Udata*>(this));
775+
return getudatamem(this); // Calls const overload
776776
}
777777

778778
/* }================================================================== */
@@ -1863,7 +1863,7 @@ inline bool operator==(const TValue& l, const TValue& r) noexcept {
18631863
}
18641864
case LUA_VSHRSTR: case LUA_VLNGSTR: {
18651865
/* Compare strings with different variants */
1866-
return const_cast<TString*>(tsvalue(&l))->equals(const_cast<TString*>(tsvalue(&r)));
1866+
return tsvalue(&l)->equals(tsvalue(&r));
18671867
}
18681868
default:
18691869
return false;
@@ -1882,7 +1882,7 @@ inline bool operator==(const TValue& l, const TValue& r) noexcept {
18821882
case LUA_VSHRSTR:
18831883
return eqshrstr(tsvalue(&l), tsvalue(&r));
18841884
case LUA_VLNGSTR:
1885-
return const_cast<TString*>(tsvalue(&l))->equals(const_cast<TString*>(tsvalue(&r)));
1885+
return tsvalue(&l)->equals(tsvalue(&r));
18861886
case LUA_VUSERDATA:
18871887
return uvalue(&l) == uvalue(&r);
18881888
case LUA_VLCF:
@@ -1919,7 +1919,7 @@ inline bool operator<=(const TString& l, const TString& r) noexcept {
19191919
/* operator== for TString - equality check using existing equals() method */
19201920
inline bool operator==(const TString& l, const TString& r) noexcept {
19211921
// Use equals() method which handles short vs long string optimization
1922-
return const_cast<TString&>(l).equals(const_cast<TString*>(&r));
1922+
return l.equals(&r);
19231923
}
19241924

19251925
/* operator!= for TString - inequality check */

src/objects/lstring.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ unsigned TString::hashLongStr() {
405405
return getHash();
406406
}
407407

408-
bool TString::equals(TString* other) {
408+
bool TString::equals(const TString* other) const {
409409
size_t len1, len2;
410410
const char *s1 = getlstr(this, len1);
411411
const char *s2 = getlstr(other, len2);

src/objects/ltable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ inline size_t extraLastfree(const Table* t) noexcept {
453453
}
454454

455455
/* 'node' size in bytes */
456-
static size_t sizehash (Table *t) {
456+
static size_t sizehash (const Table *t) {
457457
return cast_sizet(t->nodeSize()) * sizeof(Node) + extraLastfree(t);
458458
}
459459

@@ -925,7 +925,7 @@ Table *luaH_new (lua_State *L) {
925925
}
926926

927927

928-
lu_mem luaH_size (Table *t) {
928+
lu_mem luaH_size (const Table *t) {
929929
lu_mem sz = static_cast<lu_mem>(sizeof(Table)) + concretesize(t->arraySize());
930930
if (!t->isDummy())
931931
sz += sizehash(t);
@@ -1499,7 +1499,7 @@ void Table::resizeArray(lua_State* L, unsigned nasize) {
14991499
}
15001500

15011501
lu_mem Table::size() const {
1502-
return luaH_size(const_cast<Table*>(this));
1502+
return luaH_size(this);
15031503
}
15041504

15051505
int Table::tableNext(lua_State* L, StkId key) {

src/objects/ltable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ LUAI_FUNC Table *luaH_new (lua_State *L);
159159
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned nasize,
160160
unsigned nhsize);
161161
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned nasize);
162-
LUAI_FUNC lu_mem luaH_size (Table *t);
162+
LUAI_FUNC lu_mem luaH_size (const Table *t);
163163
LUAI_FUNC void luaH_free (lua_State *L, Table *t);
164164
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
165165
LUAI_FUNC lua_Unsigned luaH_getn (lua_State *L, Table *t);

0 commit comments

Comments
 (0)