Skip to content

Commit c0777f2

Browse files
committed
Phase 122-123: std::span callsite conversion (Parts 2-3 - Compiler & Runtime)
Converted 9 additional patterns to use existing std::span accessors: **Phase 122 - Compiler Code (7 conversions):** funcstate.cpp: 1. registerlocalvar() - locVarsSpan initialization loop 2. searchupvalue() - upvaluesSpan search loop 3. allocupvalue() - upvaluesSpan initialization loop lcode.cpp: 4. addk() - constantsSpan initialization loop 5. finaltarget() - codeSpan jump chain following 6. finish() - codeSpan instruction loop parser.cpp: 7. addprototype() - protosSpan initialization loop **Phase 123 - Runtime Code (2 conversions):** lfunc.cpp: 8. Proto::getLocalName() - locVarsSpan range-based for loop lvm.cpp: 9. lua_State::pushClosure() - upvaluesSpan range-based for loop All conversions use existing span accessors from Phase 115-116: - Proto::getCodeSpan() - Proto::getConstantsSpan() - Proto::getProtosSpan() - Proto::getUpvaluesSpan() - ProtoDebugInfo::getLocVarsSpan() Benefits: - Type-safe array access throughout compiler and runtime - Cleaner range-based for loops where applicable - Better bounds safety with std::span - More idiomatic modern C++23 code Testing: - All tests pass (final OK !!!) - Performance: 4.61s avg (within acceptable bounds) - Total std::span conversions: 15 (Phase 121: 6 + Phase 122: 7 + Phase 123: 2) This completes the std::span expansion effort (Option 2) with 15 new call sites converted beyond the original Phase 115-116 accessor additions.
1 parent 4969379 commit c0777f2

File tree

5 files changed

+38
-31
lines changed

5 files changed

+38
-31
lines changed

src/compiler/funcstate.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ short FuncState::registerlocalvar(TString *varname) {
9191
int oldsize = proto->getLocVarsSize();
9292
luaM_growvector(getLexState()->getLuaState(), proto->getLocVarsRef(), getNumDebugVars(), proto->getLocVarsSizeRef(),
9393
LocVar, SHRT_MAX, "local variables");
94-
while (oldsize < proto->getLocVarsSize())
95-
proto->getLocVars()[oldsize++].setVarName(nullptr);
96-
proto->getLocVars()[getNumDebugVars()].setVarName(varname);
97-
proto->getLocVars()[getNumDebugVars()].setStartPC(getPC());
94+
auto locVarsSpan = proto->getDebugInfo().getLocVarsSpan();
95+
while (oldsize < static_cast<int>(locVarsSpan.size()))
96+
locVarsSpan[oldsize++].setVarName(nullptr);
97+
locVarsSpan[getNumDebugVars()].setVarName(varname);
98+
locVarsSpan[getNumDebugVars()].setStartPC(getPC());
9899
luaC_objbarrier(getLexState()->getLuaState(), proto, varname);
99100
return postIncrementNumDebugVars();
100101
}
@@ -181,10 +182,9 @@ void FuncState::removevars(int tolevel) {
181182
** with the given 'name'.
182183
*/
183184
int FuncState::searchupvalue(TString *name) {
184-
int i;
185-
Upvaldesc *up = getProto()->getUpvalues();
186-
for (i = 0; i < getNumUpvalues(); i++) {
187-
if (eqstr(up[i].getName(), name)) return i;
185+
auto upvaluesSpan = getProto()->getUpvaluesSpan();
186+
for (size_t i = 0; i < static_cast<size_t>(getNumUpvalues()); i++) {
187+
if (eqstr(upvaluesSpan[i].getName(), name)) return static_cast<int>(i);
188188
}
189189
return -1; /* not found */
190190
}
@@ -196,9 +196,10 @@ Upvaldesc *FuncState::allocupvalue() {
196196
checklimit(getNumUpvalues() + 1, MAXUPVAL, "upvalues");
197197
luaM_growvector(getLexState()->getLuaState(), proto->getUpvaluesRef(), getNumUpvalues(), proto->getUpvaluesSizeRef(),
198198
Upvaldesc, MAXUPVAL, "upvalues");
199-
while (oldsize < proto->getUpvaluesSize())
200-
proto->getUpvalues()[oldsize++].setName(nullptr);
201-
return &proto->getUpvalues()[getNumUpvaluesRef()++];
199+
auto upvaluesSpan = proto->getUpvaluesSpan();
200+
while (oldsize < static_cast<int>(upvaluesSpan.size()))
201+
upvaluesSpan[oldsize++].setName(nullptr);
202+
return &upvaluesSpan[getNumUpvaluesRef()++];
202203
}
203204

204205

src/compiler/lcode.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,10 @@ int FuncState::addk(Proto *proto, TValue *v) {
319319
int oldsize = proto->getConstantsSize();
320320
int k = getNK();
321321
luaM_growvector(L, proto->getConstantsRef(), k, proto->getConstantsSizeRef(), TValue, MAXARG_Ax, "constants");
322-
while (oldsize < proto->getConstantsSize())
323-
setnilvalue(&proto->getConstants()[oldsize++]);
324-
proto->getConstants()[k] = *v;
322+
auto constantsSpan = proto->getConstantsSpan();
323+
while (oldsize < static_cast<int>(constantsSpan.size()))
324+
setnilvalue(&constantsSpan[oldsize++]);
325+
constantsSpan[k] = *v;
325326
incrementNK();
326327
luaC_barrier(L, proto, v);
327328
return k;
@@ -1071,10 +1072,10 @@ void FuncState::codeconcat(expdesc *e1, expdesc *e2, int line) {
10711072
** return the final target of a jump (skipping jumps to jumps)
10721073
*/
10731074
int FuncState::finaltarget(int i) {
1074-
Instruction *code = getProto()->getCode();
1075+
auto codeSpan = getProto()->getCodeSpan();
10751076
int count;
10761077
for (count = 0; count < 100; count++) { /* avoid infinite loops */
1077-
Instruction instr = code[i];
1078+
Instruction instr = codeSpan[i];
10781079
if (InstructionView(instr).opcode() != OP_JMP)
10791080
break;
10801081
else
@@ -1654,8 +1655,9 @@ void FuncState::setlist(int base, int nelems, int tostore) {
16541655
void FuncState::finish() {
16551656
int i;
16561657
Proto *p = getProto();
1658+
auto codeSpan = p->getCodeSpan();
16571659
for (i = 0; i < getPC(); i++) {
1658-
Instruction *instr = &p->getCode()[i];
1660+
Instruction *instr = &codeSpan[i];
16591661
/* avoid "not used" warnings when assert is off (for 'onelua.c') */
16601662
(void)luaP_isOT; (void)luaP_isIT;
16611663
lua_assert(i == 0 || luaP_isOT(*(instr - 1)) == luaP_isIT(*instr));

src/compiler/parser.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,11 @@ Proto *Parser::addprototype() {
412412
if (funcstate->getNP() >= proto->getProtosSize()) {
413413
int oldsize = proto->getProtosSize();
414414
luaM_growvector(state, proto->getProtosRef(), funcstate->getNP(), proto->getProtosSizeRef(), Proto *, MAXARG_Bx, "functions");
415-
while (oldsize < proto->getProtosSize())
416-
proto->getProtos()[oldsize++] = nullptr;
415+
auto protosSpan = proto->getProtosSpan();
416+
while (oldsize < static_cast<int>(protosSpan.size()))
417+
protosSpan[oldsize++] = nullptr;
417418
}
418-
proto->getProtos()[funcstate->getNPRef()++] = clp = luaF_newproto(state);
419+
proto->getProtosSpan()[funcstate->getNPRef()++] = clp = luaF_newproto(state);
419420
luaC_objbarrier(state, proto, clp);
420421
return clp;
421422
}

src/objects/lfunc.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,14 @@ void luaF_freeproto (lua_State *L, Proto *f) {
319319
** Returns nullptr if not found.
320320
*/
321321
const char* Proto::getLocalName(int local_number, int pc) const {
322-
int i;
323-
for (i = 0; i<getLocVarsSize() && getLocVars()[i].getStartPC() <= pc; i++) {
324-
if (pc < getLocVars()[i].getEndPC()) { /* is variable active? */
322+
auto locVarsSpan = getDebugInfo().getLocVarsSpan();
323+
for (const auto& locvar : locVarsSpan) {
324+
if (locvar.getStartPC() > pc)
325+
break;
326+
if (pc < locvar.getEndPC()) { /* is variable active? */
325327
local_number--;
326328
if (local_number == 0)
327-
return getstr(getLocVars()[i].getVarName());
329+
return getstr(locvar.getVarName());
328330
}
329331
}
330332
return nullptr; /* not found */

src/vm/lvm.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,19 @@ inline constexpr int MAXTAGLOOP = 2000;
135135
** its upvalues.
136136
*/
137137
void lua_State::pushClosure(Proto *p, UpVal **encup, StkId base, StkId ra) {
138-
int nup = p->getUpvaluesSize();
139-
Upvaldesc *uv = p->getUpvalues();
140-
int i;
138+
auto upvaluesSpan = p->getUpvaluesSpan();
139+
int nup = static_cast<int>(upvaluesSpan.size());
140+
int i = 0;
141141
LClosure *ncl = LClosure::create(this, nup);
142142
ncl->setProto(p);
143143
setclLvalue2s(this, ra, ncl); /* anchor new closure in stack */
144-
for (i = 0; i < nup; i++) { /* fill in its upvalues */
145-
if (uv[i].isInStack()) /* upvalue refers to local variable? */
146-
ncl->setUpval(i, luaF_findupval(this, base + uv[i].getIndex()));
144+
for (const auto& uv : upvaluesSpan) { /* fill in its upvalues */
145+
if (uv.isInStack()) /* upvalue refers to local variable? */
146+
ncl->setUpval(i, luaF_findupval(this, base + uv.getIndex()));
147147
else /* get upvalue from enclosing function */
148-
ncl->setUpval(i, encup[uv[i].getIndex()]);
148+
ncl->setUpval(i, encup[uv.getIndex()]);
149149
luaC_objbarrier(this, ncl, ncl->getUpval(i));
150+
i++;
150151
}
151152
}
152153

0 commit comments

Comments
 (0)