Skip to content

Commit b4772ca

Browse files
committed
Phase 119: C++ Standard Library Integration - std::array conversion
Converted 4 fixed-size C arrays to std::array for improved type safety: Part A - Local/Header Arrays: - luaT_eventname (ltm.cpp) - 25 tag method names - opnames (lopnames.h) - 84 opcode names Part B - Global Arrays: - luaT_typenames_ (ltm.cpp/ltm.h) - 12 type names - luaP_opmodes (lopcodes.cpp/lopcodes.h) - 83 opcode modes Technical Details: - Used type aliases (TypeNamesArray, OpModesArray) to work around LUAI_DDEC macro limitations with template commas - All arrays are constexpr where possible for compile-time evaluation - Zero-cost abstraction with better bounds checking in debug builds Performance Results: - Baseline: 4.20s avg - Current: 3.97s avg (5-run benchmark) - Change: -5.5% (improvement!) - Target: ≤4.33s ✅ PASS Benefits: - Better type safety (no array decay) - Compile-time size information - Improved compiler optimizations - Modern C++23 best practices - Debug-mode bounds checking All tests passing with "final OK !!!"
1 parent 346bf5b commit b4772ca

File tree

5 files changed

+13
-6
lines changed

5 files changed

+13
-6
lines changed

src/compiler/lopcodes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "lprefix.h"
1111

1212

13+
#include <array>
1314
#include "lopcodes.h"
1415

1516

@@ -19,7 +20,7 @@
1920

2021
/* ORDER OP */
2122

22-
LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
23+
LUAI_DDEF const OpModesArray luaP_opmodes = {
2324
/* MM OT IT T A mode opcode */
2425
opmode(0, 0, 0, 0, 1, OpMode::iABC) /* OP_MOVE */
2526
,opmode(0, 0, 0, 0, 1, OpMode::iAsBx) /* OP_LOADI */

src/compiler/lopcodes.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef lopcodes_h
88
#define lopcodes_h
99

10+
#include <array>
1011
#include "llimits.h"
1112
#include "lobject.h"
1213

@@ -548,7 +549,8 @@ inline constexpr int NUM_OPCODES = ((int)(OP_EXTRAARG) + 1);
548549
** bit 7: instruction is an MM instruction (call a metamethod)
549550
*/
550551

551-
LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
552+
using OpModesArray = std::array<lu_byte, NUM_OPCODES>;
553+
LUAI_DDEC(const OpModesArray luaP_opmodes;)
552554

553555
inline OpMode getOpMode(int m) noexcept {
554556
return static_cast<OpMode>(luaP_opmodes[m] & 7);

src/core/ltm.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "lprefix.h"
1111

1212

13+
#include <array>
1314
#include <cstring>
1415

1516
#include "lua.h"
@@ -27,7 +28,7 @@
2728

2829
static const char udatatypename[] = "userdata";
2930

30-
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
31+
LUAI_DDEF const TypeNamesArray luaT_typenames_ = {
3132
"no value",
3233
"nil", "boolean", udatatypename, "number",
3334
"string", "table", "function", udatatypename, "thread",
@@ -36,7 +37,7 @@ LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
3637

3738

3839
void luaT_init (lua_State *L) {
39-
static const char *const luaT_eventname[] = { /* ORDER TM */
40+
static constexpr std::array<const char*, 25> luaT_eventname = { /* ORDER TM */
4041
"__index", "__newindex",
4142
"__gc", "__mode", "__len", "__eq",
4243
"__add", "__sub", "__mul", "__mod", "__pow",

src/core/ltm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define ltm_h
99

1010

11+
#include <array>
1112
#include "lobject.h"
1213

1314

@@ -78,7 +79,8 @@ struct lua_State;
7879
inline const TValue* gfasttm(global_State* g, const Table* mt, TMS e) noexcept;
7980
inline const TValue* fasttm(lua_State* l, const Table* mt, TMS e) noexcept;
8081

81-
LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];)
82+
using TypeNamesArray = std::array<const char*, LUA_TOTALTYPES>;
83+
LUAI_DDEC(const TypeNamesArray luaT_typenames_;)
8284

8385
inline const char* ttypename(int x) noexcept {
8486
return luaT_typenames_[x + 1];

src/vm/lopnames.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
#if !defined(lopnames_h)
88
#define lopnames_h
99

10+
#include <array>
1011
#include <cstddef>
1112

1213

1314
/* ORDER OP */
1415

15-
static const char *const opnames[] = {
16+
static constexpr std::array<const char*, 84> opnames = {
1617
"MOVE",
1718
"LOADI",
1819
"LOADF",

0 commit comments

Comments
 (0)