Skip to content

Commit c062b29

Browse files
authored
Merge pull request PathOfBuildingCommunity#99 from Helyos96/performance-improvements
Performance improvements of some lua APIs
2 parents db84d8d + 3616b23 commit c062b29

3 files changed

Lines changed: 100 additions & 40 deletions

File tree

engine/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ T clamp(T &v, T l, T u)
481481

482482
int IsColorEscape(char const* str);
483483
int IsColorEscape(std::u32string_view str);
484-
void ReadColorEscape(char const* str, col3_t out);
484+
void ReadColorEscape(char const* str, int len, col3_t out);
485485
std::u32string_view ReadColorEscape(std::u32string_view str, col3_t out);
486486

487487
char* _AllocString(const char* str, const char* file, int line);

engine/common/common.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,22 +247,29 @@ int IsColorEscape(std::u32string_view str)
247247
return 0;
248248
}
249249

250-
void ReadColorEscape(const char* str, col3_t out)
250+
static int HexCharToInt(char c) {
251+
if (c >= 'a') return c - 'a' + 10;
252+
if (c >= 'A') return c - 'A' + 10;
253+
return c - '0';
254+
}
255+
256+
void ReadColorEscape(const char* str, int len, col3_t out)
251257
{
252-
int len = IsColorEscape(str);
253258
switch (len) {
254259
case 2:
255260
VectorCopy(colorEscape[str[1] - '0'], out);
256261
break;
257262
case 8:
258-
{
259-
int xr, xg, xb;
260-
sscanf(str + 2, "%2x%2x%2x", &xr, &xg, &xb);
261-
out[0] = xr / 255.0f;
262-
out[1] = xg / 255.0f;
263-
out[2] = xb / 255.0f;
264-
}
265-
break;
263+
{
264+
int xr = (HexCharToInt(str[2]) << 4) | HexCharToInt(str[3]);
265+
int xg = (HexCharToInt(str[4]) << 4) | HexCharToInt(str[5]);
266+
int xb = (HexCharToInt(str[6]) << 4) | HexCharToInt(str[7]);
267+
268+
out[0] = xr / 255.0f;
269+
out[1] = xg / 255.0f;
270+
out[2] = xb / 255.0f;
271+
}
272+
break;
266273
}
267274
}
268275

ui_api.cpp

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static int l_##Name(lua_State* L) { \
162162
lua_pushfstring(L, "C++ exception:\n%s", e.what()); \
163163
return -1; \
164164
} \
165-
}; \
165+
}; \
166166
int rc = fun(L); \
167167
if (rc < 0) { LuaErrorWrapper(L); } \
168168
return rc; }
@@ -832,19 +832,29 @@ static int l_SetDrawColor(lua_State* L)
832832
ui->LAssert(L, n >= 1, "Usage: SetDrawColor(red, green, blue[, alpha]) or SetDrawColor(escapeStr)");
833833
col4_t color;
834834
if (lua_type(L, 1) == LUA_TSTRING) {
835-
ui->LAssert(L, IsColorEscape(lua_tostring(L, 1)), "SetDrawColor() argument 1: invalid color escape sequence");
836-
ReadColorEscape(lua_tostring(L, 1), color);
835+
const char *str = lua_tostring(L, 1);
836+
int len = IsColorEscape(str);
837+
ui->LAssert(L, len, "SetDrawColor() argument 1: invalid color escape sequence");
838+
ReadColorEscape(str, len, color);
837839
color[3] = 1.0;
838840
}
839841
else {
840842
ui->LAssert(L, n >= 3, "Usage: SetDrawColor(red, green, blue[, alpha]) or SetDrawColor(escapeStr)");
841843
for (int i = 1; i <= 3; i++) {
842-
ui->LAssert(L, lua_isnumber(L, i), "SetDrawColor() argument %d: expected number, got %s", i, luaL_typename(L, i));
843-
color[i - 1] = (float)lua_tonumber(L, i);
844+
int isnum;
845+
lua_Number val = lua_tonumberx(L, i, &isnum);
846+
if (!isnum) {
847+
ui->LAssert(L, false, "SetDrawColor() argument %d: expected number, got %s", i, luaL_typename(L, i));
848+
}
849+
color[i - 1] = (float)val;
844850
}
845851
if (n >= 4 && !lua_isnil(L, 4)) {
846-
ui->LAssert(L, lua_isnumber(L, 4), "SetDrawColor() argument 4: expected number or nil, got %s", luaL_typename(L, 4));
847-
color[3] = (float)lua_tonumber(L, 4);
852+
int isnum;
853+
lua_Number val = lua_tonumberx(L, 4, &isnum);
854+
if (!isnum) {
855+
ui->LAssert(L, false, "SetDrawColor() argument 4: expected number or nil, got %s", luaL_typename(L, 4));
856+
}
857+
color[3] = (float)val;
848858
}
849859
else {
850860
color[3] = 1.0;
@@ -884,7 +894,11 @@ static int l_DrawImage(lua_State* L)
884894
int n = lua_gettop(L);
885895
const char* usage = "Usage: DrawImage({imgHandle|nil}, left, top, width, height[, tcLeft, tcTop, tcRight, tcBottom][, stackIdx[, mask]])";
886896
ui->LAssert(L, n >= 5, usage);
887-
ui->LAssert(L, lua_isnil(L, 1) || ui->IsUserData(L, 1, "uiimghandlemeta"), "DrawImage() argument 1: expected image handle or nil, got %s", luaL_typename(L, 1));
897+
898+
if (!lua_isnil(L, 1) && !ui->IsUserData(L, 1, "uiimghandlemeta")) {
899+
ui->LAssert(L, false, "DrawImage() argument 1: expected image handle or nil, got %s", luaL_typename(L, 1));
900+
}
901+
888902
r_shaderHnd_c* hnd = NULL;
889903
glm::vec2 xys[2]{}, uvs[2]{};
890904
int stackLayer = 0;
@@ -897,7 +911,7 @@ static int l_DrawImage(lua_State* L)
897911
// | 9 | X | X | X | | |
898912
// | 10 | X | X | X | X | |
899913
// | 11 | X | X | X | X | X |
900-
914+
901915
enum ArgFlag : uint8_t { AF_IMG = 0x1, AF_XY = 0x2, AF_UV = 0x4, AF_STACK = 0x8, AF_MASK = 0x10 };
902916
ArgFlag af{};
903917
switch (n) {
@@ -919,22 +933,32 @@ static int l_DrawImage(lua_State* L)
919933
}
920934
k += 1;
921935
}
922-
936+
923937
if (af & AF_XY) {
924938
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
925939
for (int i = k; i < k + 4; i++) {
926-
ui->LAssert(L, lua_isnumber(L, i), "DrawImage() argument %d: expected number, got %s", i, luaL_typename(L, i));
940+
int isNum;
941+
lua_Number val = lua_tonumberx(L, i, &isNum);
942+
943+
if (!isNum) {
944+
ui->LAssert(L, false, "DrawImage() argument %d: expected number, got %s", i, luaL_typename(L, i));
945+
}
927946
const int idx = i - k;
928-
xys[idx/2][idx%2] = (float)lua_tonumber(L, i) * dpiScale;
947+
xys[idx/2][idx%2] = (float)val * dpiScale;
929948
}
930949
k += 4;
931950
}
932951

933952
if (af & AF_UV) {
934953
for (int i = k; i < k + 4; i++) {
935-
ui->LAssert(L, lua_isnumber(L, i), "DrawImage() argument %d: expected number, got %s", i, luaL_typename(L, i));
954+
int isNum;
955+
lua_Number val = lua_tonumberx(L, i, &isNum);
956+
957+
if (!isNum) {
958+
ui->LAssert(L, false, "DrawImage() argument %d: expected number, got %s", i, luaL_typename(L, i));
959+
}
936960
int idx = i - k;
937-
uvs[idx/2][idx%2] = (float)lua_tonumber(L, i);
961+
uvs[idx/2][idx%2] = (float)val;
938962
}
939963
k += 4;
940964
}
@@ -948,8 +972,12 @@ static int l_DrawImage(lua_State* L)
948972
maxStackValue = hnd->StackCount();
949973

950974
if (af & AF_STACK) {
951-
ui->LAssert(L, lua_isinteger(L, k), "DrawImage() argument %d: expected integer, got %s", k, luaL_typename(L, k));
952-
const int val = (int)lua_tointeger(L, k);
975+
int isInt;
976+
int val = (int)lua_tointegerx(L, k, &isInt);
977+
978+
if (!isInt) {
979+
ui->LAssert(L, false, "DrawImage() argument %d: expected integer, got %s", k, luaL_typename(L, k));
980+
}
953981
ui->LAssert(L, val > 0, "DrawImage() argument %d: expected positive integer, got %d", k, val);
954982
if (maxStackValue.has_value())
955983
ui->LAssert(L, val <= *maxStackValue, "DrawImage() argument %d: expected valid stack index <= %d, got %d", k, *maxStackValue, val);
@@ -958,9 +986,13 @@ static int l_DrawImage(lua_State* L)
958986
}
959987

960988
if (af & AF_MASK) {
961-
ui->LAssert(L, lua_isnil(L, k) || lua_isinteger(L, k), "DrawImage() argument %d: expected integer or nil, got %s", k, luaL_typename(L, k));
962-
if (lua_isinteger(L, k)) {
963-
const int val = (int)lua_tointeger(L, k);
989+
if (!lua_isnil(L, k)) {
990+
int isInt;
991+
int val = (int)lua_tointegerx(L, k, &isInt);
992+
993+
if (!isInt) {
994+
ui->LAssert(L, false, "DrawImage() argument %d: expected integer or nil, got %s", k, luaL_typename(L, k));
995+
}
964996
ui->LAssert(L, val > 0, "DrawImage() argument %d: expected positive integer, got %d", k, val);
965997
if (maxStackValue.has_value())
966998
ui->LAssert(L, val <= *maxStackValue, "DrawImage() argument %d: expected valid stack index <= %d, got %d", k, *maxStackValue, val);
@@ -982,7 +1014,9 @@ static int l_DrawImageQuad(lua_State* L)
9821014
int n = lua_gettop(L);
9831015
const char* usage = "Usage: DrawImageQuad({imgHandle|nil}, x1, y1, x2, y2, x3, y3, x4, y4[, s1, t1, s2, t2, s3, t3, s4, t4][, stackIdx[, mask]])";
9841016
ui->LAssert(L, n >= 9, usage);
985-
ui->LAssert(L, lua_isnil(L, 1) || ui->IsUserData(L, 1, "uiimghandlemeta"), "DrawImageQuad() argument 1: expected image handle or nil, got %s", luaL_typename(L, 1));
1017+
if (!lua_isnil(L, 1) && ! ui->IsUserData(L, 1, "uiimghandlemeta")) {
1018+
ui->LAssert(L, false, "DrawImageQuad() argument 1: expected image handle or nil, got %s", luaL_typename(L, 1));
1019+
}
9861020

9871021
r_shaderHnd_c* hnd = NULL;
9881022
glm::vec2 xys[4]{}, uvs[4]{};
@@ -1022,18 +1056,28 @@ static int l_DrawImageQuad(lua_State* L)
10221056
if (af & AF_XY) {
10231057
const float dpiScale = ui->renderer->VirtualScreenScaleFactor();
10241058
for (int i = k; i < k + 8; i++) {
1025-
ui->LAssert(L, lua_isnumber(L, i), "DrawImageQuad() argument %d: expected number, got %s", i, luaL_typename(L, i));
1059+
int isNum;
1060+
lua_Number val = lua_tonumberx(L, i, &isNum);
1061+
1062+
if (!isNum) {
1063+
ui->LAssert(L, false, "DrawImageQuad() argument %d: expected number, got %s", i, luaL_typename(L, i));
1064+
}
10261065
const int idx = i - k;
1027-
xys[idx / 2][idx % 2] = (float)lua_tonumber(L, i) * dpiScale;
1066+
xys[idx / 2][idx % 2] = (float)val * dpiScale;
10281067
}
10291068
k += 8;
10301069
}
10311070

10321071
if (af & AF_UV) {
10331072
for (int i = k; i < k + 8; i++) {
1034-
ui->LAssert(L, lua_isnumber(L, i), "DrawImageQuad() argument %d: expected number, got %s", i, luaL_typename(L, i));
1073+
int isNum;
1074+
lua_Number val = lua_tonumberx(L, i, &isNum);
1075+
1076+
if (!isNum) {
1077+
ui->LAssert(L, false, "DrawImageQuad() argument %d: expected number, got %s", i, luaL_typename(L, i));
1078+
}
10351079
int idx = i - k;
1036-
uvs[idx / 2][idx % 2] = (float)lua_tonumber(L, i);
1080+
uvs[idx / 2][idx % 2] = (float)val;
10371081
}
10381082
k += 8;
10391083
}
@@ -1049,8 +1093,12 @@ static int l_DrawImageQuad(lua_State* L)
10491093
maxStackValue = hnd->StackCount();
10501094

10511095
if (af & AF_STACK) {
1052-
ui->LAssert(L, lua_isinteger(L, k), "DrawImageQuad() argument %d: expected integer, got %s", k, luaL_typename(L, k));
1053-
const int val = (int)lua_tointeger(L, k);
1096+
int isInt;
1097+
const int val = (int)lua_tointegerx(L, k, &isInt);
1098+
1099+
if (!isInt) {
1100+
ui->LAssert(L, false, "DrawImageQuad() argument %d: expected integer, got %s", k, luaL_typename(L, k));
1101+
}
10541102
ui->LAssert(L, val > 0, "DrawImageQuad() argument %d: expected positive integer, got %d", k, val);
10551103
if (maxStackValue.has_value())
10561104
ui->LAssert(L, val <= *maxStackValue, "DrawImageQuad() argument %d: expected valid stack index <= %d, got %d", k, *maxStackValue, val);
@@ -1059,12 +1107,17 @@ static int l_DrawImageQuad(lua_State* L)
10591107
}
10601108

10611109
if (af & AF_MASK) {
1062-
ui->LAssert(L, lua_isnil(L, k) || lua_isinteger(L, k), "DrawImageQuad() argument %d: expected integer or nil, got %s", k, luaL_typename(L, k));
1063-
if (lua_isinteger(L, k)) {
1064-
const int val = (int)lua_tointeger(L, k);
1110+
if (!lua_isnil(L, k)) {
1111+
int isInt;
1112+
const int val = (int)lua_tointegerx(L, k, &isInt);
1113+
1114+
if (!isInt) {
1115+
ui->LAssert(L, false, "DrawImageQuad() argument %d: expected integer or nil, got %s", k, luaL_typename(L, k));
1116+
}
10651117
ui->LAssert(L, val > 0, "DrawImageQuad() argument %d: expected positive integer, got %d", k, val);
10661118
if (maxStackValue.has_value())
10671119
ui->LAssert(L, val <= *maxStackValue, "DrawImageQuad() argument %d: expected valid stack index <= %d, got %d", k, *maxStackValue, val);
1120+
10681121
maskLayer = val - 1;
10691122
}
10701123
k += 1;

0 commit comments

Comments
 (0)