Skip to content

Commit c6805cb

Browse files
committed
Phase 122 (continued): Optimize VM auxiliary files with auto
Extend local variable optimization to VM arithmetic, comparison, loops, table, and string operation files: lvm_arithmetic.cpp (2 optimizations): - luaV_idiv: auto q (division result) - luaV_mod: auto r (modulus result) lvm_comparison.cpp (5 optimizations): - l_strcmp: auto for temp, zl1, zl2, s1, s2 - luaV_equalobj: auto tag (TM result) lvm_loops.cpp (8 optimizations): - forPrep: auto for pinit, plimit, pstep, init, step - forPrep: declare init/limit/step in float branch together - floatForLoop: auto for step, limit, idx lvm_table.cpp (2 optimizations): - luaV_finishget: declare loop in for-init - luaV_finishset: declare loop in for-init, auto *h lvm_string.cpp (5 optimizations): - copy2buff: auto tl, st, s - luaV_concat: auto top, n, tl, l Total: 22 optimizations across 5 VM files Benefits: - Consistent with lvm.cpp optimizations (Phase 122) - Improved code clarity and maintainability - Type-safe variable declarations Performance: 4.31s avg (5 runs) - Run 1: 4.13s - Run 2: 4.25s - Run 3: 4.69s - Run 4: 4.22s - Run 5: 4.28s Target: ≤4.33s ✓ (2.6% above baseline) Tests: All pass ✓ Files modified: - src/vm/lvm_arithmetic.cpp - src/vm/lvm_comparison.cpp - src/vm/lvm_loops.cpp - src/vm/lvm_table.cpp - src/vm/lvm_string.cpp
1 parent 98eafa5 commit c6805cb

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

src/vm/lvm_arithmetic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
3131
return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
3232
}
3333
else {
34-
lua_Integer q = m / n; /* perform C division */
34+
auto q = m / n; /* perform C division */
3535
if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */
3636
q -= 1; /* correct result for different rounding */
3737
return q;
@@ -51,7 +51,7 @@ lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
5151
return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
5252
}
5353
else {
54-
lua_Integer r = m % n;
54+
auto r = m % n;
5555
if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */
5656
r += n; /* correct result for different rounding */
5757
return r;

src/vm/lvm_comparison.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@
3333
*/
3434
[[nodiscard]] int l_strcmp (const TString *ts1, const TString *ts2) {
3535
size_t rl1; /* real length */
36-
const char *s1 = getlstr(ts1, rl1);
36+
auto *s1 = getlstr(ts1, rl1);
3737
size_t rl2;
38-
const char *s2 = getlstr(ts2, rl2);
38+
auto *s2 = getlstr(ts2, rl2);
3939
for (;;) { /* for each segment */
40-
int temp = strcoll(s1, s2);
40+
auto temp = strcoll(s1, s2);
4141
if (temp != 0) /* not equal? */
4242
return temp; /* done */
4343
else { /* strings are equal up to a '\0' */
44-
size_t zl1 = strlen(s1); /* index of first '\0' in 's1' */
45-
size_t zl2 = strlen(s2); /* index of first '\0' in 's2' */
44+
auto zl1 = strlen(s1); /* index of first '\0' in 's1' */
45+
auto zl2 = strlen(s2); /* index of first '\0' in 's2' */
4646
if (zl2 == rl2) /* 's2' is finished? */
4747
return (zl1 == rl1) ? 0 : 1; /* check 's1' */
4848
else if (zl1 == rl1) /* 's1' is finished? */
@@ -254,7 +254,7 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
254254
if (tm == nullptr) /* no TM? */
255255
return 0; /* objects are different */
256256
else {
257-
LuaT tag = luaT_callTMres(L, tm, t1, t2, L->getTop().p); /* call TM */
257+
auto tag = luaT_callTMres(L, tm, t1, t2, L->getTop().p); /* call TM */
258258
return !tagisfalse(tag);
259259
}
260260
}

src/vm/lvm_loops.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ int lua_State::forLimit(lua_Integer init, const TValue *lim,
6868
** ra + 2 : control variable
6969
*/
7070
int lua_State::forPrep(StkId ra) {
71-
TValue *pinit = s2v(ra);
72-
TValue *plimit = s2v(ra + 1);
73-
TValue *pstep = s2v(ra + 2);
71+
auto *pinit = s2v(ra);
72+
auto *plimit = s2v(ra + 1);
73+
auto *pstep = s2v(ra + 2);
7474
if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
75-
lua_Integer init = ivalue(pinit);
76-
lua_Integer step = ivalue(pstep);
75+
auto init = ivalue(pinit);
76+
auto step = ivalue(pstep);
7777
lua_Integer limit;
7878
if (step == 0)
7979
luaG_runerror(this, "'for' step is zero");
@@ -105,7 +105,7 @@ int lua_State::forPrep(StkId ra) {
105105
}
106106
}
107107
else { /* try making all values floats */
108-
lua_Number init; lua_Number limit; lua_Number step;
108+
lua_Number init, limit, step;
109109
if (l_unlikely(!tonumber(plimit, &limit)))
110110
luaG_forerror(this, plimit, "limit");
111111
if (l_unlikely(!tonumber(pstep, &step)))
@@ -134,9 +134,9 @@ int lua_State::forPrep(StkId ra) {
134134
** written online with opcode OP_FORLOOP, for performance.)
135135
*/
136136
int lua_State::floatForLoop(StkId ra) {
137-
lua_Number step = fltvalue(s2v(ra + 1));
138-
lua_Number limit = fltvalue(s2v(ra));
139-
lua_Number idx = fltvalue(s2v(ra + 2)); /* control variable */
137+
auto step = fltvalue(s2v(ra + 1));
138+
auto limit = fltvalue(s2v(ra));
139+
auto idx = fltvalue(s2v(ra + 2)); /* control variable */
140140
idx = luai_numadd(this, idx, step); /* increment index */
141141
if (luai_numlt(0, step) ? luai_numle(idx, limit)
142142
: luai_numle(limit, idx)) {

src/vm/lvm_string.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ inline bool isemptystr(const TValue* o) noexcept {
3636

3737
/* copy strings in stack from top - n up to top - 1 to buffer */
3838
static void copy2buff (StkId top, int n, char *buff) {
39-
size_t tl = 0; /* size already copied */
39+
auto tl = size_t{0}; /* size already copied */
4040
do {
41-
TString *st = tsvalue(s2v(top - n));
41+
auto *st = tsvalue(s2v(top - n));
4242
size_t l; /* length of string being copied */
43-
const char *s = getlstr(st, l);
43+
auto *s = getlstr(st, l);
4444
std::copy_n(s, l, buff + tl);
4545
tl += l;
4646
} while (--n > 0);
@@ -55,8 +55,8 @@ void luaV_concat (lua_State *L, int total) {
5555
if (total == 1)
5656
return; /* "all" values already concatenated */
5757
do {
58-
StkId top = L->getTop().p;
59-
int n = 2; /* number of elements handled in this pass (at least 2) */
58+
auto top = L->getTop().p;
59+
auto n = 2; /* number of elements handled in this pass (at least 2) */
6060
if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) ||
6161
!tostring(L, s2v(top - 1))) {
6262
luaT_tryconcatTM(L); /* may invalidate 'top' */
@@ -71,12 +71,12 @@ void luaV_concat (lua_State *L, int total) {
7171
}
7272
else {
7373
/* at least two non-empty string values; get as many as possible */
74-
size_t tl = tsslen(tsvalue(s2v(top - 1)));
74+
auto tl = tsslen(tsvalue(s2v(top - 1)));
7575
TString *ts;
7676
/* collect total length and number of strings */
7777
for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
7878
top = L->getTop().p; /* recapture after tostring() which can trigger GC */
79-
size_t l = tsslen(tsvalue(s2v(top - n - 1)));
79+
auto l = tsslen(tsvalue(s2v(top - n - 1)));
8080
if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) {
8181
L->getStackSubsystem().setTopPtr(top - total); /* pop strings to avoid wasting stack */
8282
luaG_runerror(L, "string length overflow");

src/vm/lvm_table.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ inline constexpr int MAXTAGLOOP = 2000;
4848
*/
4949
LuaT luaV_finishget (lua_State *L, const TValue *t, TValue *key,
5050
StkId val, LuaT tag) {
51-
int loop; /* counter to avoid infinite loops */
5251
const TValue *tm; /* metamethod */
53-
for (loop = 0; loop < MAXTAGLOOP; loop++) {
52+
for (int loop = 0; loop < MAXTAGLOOP; loop++) {
5453
if (tag == LuaT::NOTABLE) { /* 't' is not a table? */
5554
lua_assert(!ttistable(t));
5655
tm = luaT_gettmbyobj(L, t, TMS::TM_INDEX);
@@ -103,11 +102,10 @@ LuaT luaV_finishget (lua_State *L, const TValue *t, TValue *key,
103102
*/
104103
void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
105104
TValue *val, int hres) {
106-
int loop; /* counter to avoid infinite loops */
107-
for (loop = 0; loop < MAXTAGLOOP; loop++) {
105+
for (int loop = 0; loop < MAXTAGLOOP; loop++) {
108106
const TValue *tm; /* '__newindex' metamethod */
109107
if (hres != HNOTATABLE) { /* is 't' a table? */
110-
Table *h = hvalue(t); /* save 't' table */
108+
auto *h = hvalue(t); /* save 't' table */
111109
tm = fasttm(L, h->getMetatable(), TMS::TM_NEWINDEX); /* get metamethod */
112110
if (tm == nullptr) { /* no metamethod? */
113111
sethvalue2s(L, L->getTop().p, h); /* anchor 't' */

0 commit comments

Comments
 (0)