Skip to content

Commit 2d5bd3b

Browse files
authoredApr 14, 2020
scriptapi: Some small optimizations to value pushing (#9669)
1 parent 7c43cf4 commit 2d5bd3b

File tree

7 files changed

+59
-67
lines changed

7 files changed

+59
-67
lines changed
 

‎src/script/common/c_content.cpp

+17-17
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,15 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
350350
push_v3f(L, prop->visual_size);
351351
lua_setfield(L, -2, "visual_size");
352352

353-
lua_newtable(L);
353+
lua_createtable(L, prop->textures.size(), 0);
354354
u16 i = 1;
355355
for (const std::string &texture : prop->textures) {
356356
lua_pushlstring(L, texture.c_str(), texture.size());
357357
lua_rawseti(L, -2, i++);
358358
}
359359
lua_setfield(L, -2, "textures");
360360

361-
lua_newtable(L);
361+
lua_createtable(L, prop->colors.size(), 0);
362362
i = 1;
363363
for (const video::SColor &color : prop->colors) {
364364
push_ARGB8(L, color);
@@ -840,7 +840,7 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
840840
lua_pushnumber(L, c.connect_sides);
841841
lua_setfield(L, -2, "connect_sides");
842842

843-
lua_newtable(L);
843+
lua_createtable(L, c.connects_to.size(), 0);
844844
u16 i = 1;
845845
for (const std::string &it : c.connects_to) {
846846
lua_pushlstring(L, it.c_str(), it.size());
@@ -963,7 +963,7 @@ void push_nodebox(lua_State *L, const NodeBox &box)
963963

964964
void push_box(lua_State *L, const std::vector<aabb3f> &box)
965965
{
966-
lua_newtable(L);
966+
lua_createtable(L, box.size(), 0);
967967
u8 i = 1;
968968
for (const aabb3f &it : box) {
969969
push_aabb3f(L, it);
@@ -1040,7 +1040,7 @@ void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
10401040

10411041
void push_soundspec(lua_State *L, const SimpleSoundSpec &spec)
10421042
{
1043-
lua_newtable(L);
1043+
lua_createtable(L, 0, 3);
10441044
lua_pushstring(L, spec.name.c_str());
10451045
lua_setfield(L, -2, "name");
10461046
lua_pushnumber(L, spec.gain);
@@ -1125,12 +1125,12 @@ MapNode readnode(lua_State *L, int index, const NodeDefManager *ndef)
11251125
/******************************************************************************/
11261126
void pushnode(lua_State *L, const MapNode &n, const NodeDefManager *ndef)
11271127
{
1128-
lua_newtable(L);
1128+
lua_createtable(L, 0, 3);
11291129
lua_pushstring(L, ndef->get(n).name.c_str());
11301130
lua_setfield(L, -2, "name");
1131-
lua_pushnumber(L, n.getParam1());
1131+
lua_pushinteger(L, n.getParam1());
11321132
lua_setfield(L, -2, "param1");
1133-
lua_pushnumber(L, n.getParam2());
1133+
lua_pushinteger(L, n.getParam2());
11341134
lua_setfield(L, -2, "param2");
11351135
}
11361136

@@ -1163,7 +1163,7 @@ bool string_to_enum(const EnumString *spec, int &result,
11631163
{
11641164
const EnumString *esp = spec;
11651165
while(esp->str){
1166-
if(str == std::string(esp->str)){
1166+
if (!strcmp(str.c_str(), esp->str)) {
11671167
result = esp->num;
11681168
return true;
11691169
}
@@ -1438,7 +1438,7 @@ ToolCapabilities read_tool_capabilities(
14381438
/******************************************************************************/
14391439
void push_dig_params(lua_State *L,const DigParams &params)
14401440
{
1441-
lua_newtable(L);
1441+
lua_createtable(L, 0, 3);
14421442
setboolfield(L, -1, "diggable", params.diggable);
14431443
setfloatfield(L, -1, "time", params.time);
14441444
setintfield(L, -1, "wear", params.wear);
@@ -1447,7 +1447,7 @@ void push_dig_params(lua_State *L,const DigParams &params)
14471447
/******************************************************************************/
14481448
void push_hit_params(lua_State *L,const HitParams &params)
14491449
{
1450-
lua_newtable(L);
1450+
lua_createtable(L, 0, 3);
14511451
setintfield(L, -1, "hp", params.hp);
14521452
setintfield(L, -1, "wear", params.wear);
14531453
}
@@ -1540,9 +1540,9 @@ void read_groups(lua_State *L, int index, ItemGroupList &result)
15401540
/******************************************************************************/
15411541
void push_groups(lua_State *L, const ItemGroupList &groups)
15421542
{
1543-
lua_newtable(L);
1543+
lua_createtable(L, 0, groups.size());
15441544
for (const auto &group : groups) {
1545-
lua_pushnumber(L, group.second);
1545+
lua_pushinteger(L, group.second);
15461546
lua_setfield(L, -2, group.first.c_str());
15471547
}
15481548
}
@@ -1587,7 +1587,7 @@ void luaentity_get(lua_State *L, u16 id)
15871587
lua_getglobal(L, "core");
15881588
lua_getfield(L, -1, "luaentities");
15891589
luaL_checktype(L, -1, LUA_TTABLE);
1590-
lua_pushnumber(L, id);
1590+
lua_pushinteger(L, id);
15911591
lua_gettable(L, -2);
15921592
lua_remove(L, -2); // Remove luaentities
15931593
lua_remove(L, -2); // Remove core
@@ -1689,15 +1689,15 @@ static bool push_json_value_helper(lua_State *L, const Json::Value &value,
16891689
lua_pushboolean(L, value.asInt());
16901690
break;
16911691
case Json::arrayValue:
1692-
lua_newtable(L);
1692+
lua_createtable(L, value.size(), 0);
16931693
for (Json::Value::const_iterator it = value.begin();
16941694
it != value.end(); ++it) {
16951695
push_json_value_helper(L, *it, nullindex);
16961696
lua_rawseti(L, -2, it.index() + 1);
16971697
}
16981698
break;
16991699
case Json::objectValue:
1700-
lua_newtable(L);
1700+
lua_createtable(L, 0, value.size());
17011701
for (Json::Value::const_iterator it = value.begin();
17021702
it != value.end(); ++it) {
17031703
#ifndef JSONCPP_STRING
@@ -1824,7 +1824,7 @@ void push_objectRef(lua_State *L, const u16 id)
18241824
lua_getglobal(L, "core");
18251825
lua_getfield(L, -1, "object_refs");
18261826
luaL_checktype(L, -1, LUA_TTABLE);
1827-
lua_pushnumber(L, id);
1827+
lua_pushinteger(L, id);
18281828
lua_gettable(L, -2);
18291829
lua_remove(L, -2); // object_refs
18301830
lua_remove(L, -2); // core

‎src/script/common/c_converter.cpp

+20-20
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void push_float_string(lua_State *L, float value)
6262

6363
void push_v3f(lua_State *L, v3f p)
6464
{
65-
lua_newtable(L);
65+
lua_createtable(L, 0, 3);
6666
lua_pushnumber(L, p.X);
6767
lua_setfield(L, -2, "x");
6868
lua_pushnumber(L, p.Y);
@@ -73,7 +73,7 @@ void push_v3f(lua_State *L, v3f p)
7373

7474
void push_v2f(lua_State *L, v2f p)
7575
{
76-
lua_newtable(L);
76+
lua_createtable(L, 0, 2);
7777
lua_pushnumber(L, p.X);
7878
lua_setfield(L, -2, "x");
7979
lua_pushnumber(L, p.Y);
@@ -82,7 +82,7 @@ void push_v2f(lua_State *L, v2f p)
8282

8383
void push_v3_float_string(lua_State *L, v3f p)
8484
{
85-
lua_newtable(L);
85+
lua_createtable(L, 0, 3);
8686
push_float_string(L, p.X);
8787
lua_setfield(L, -2, "x");
8888
push_float_string(L, p.Y);
@@ -93,7 +93,7 @@ void push_v3_float_string(lua_State *L, v3f p)
9393

9494
void push_v2_float_string(lua_State *L, v2f p)
9595
{
96-
lua_newtable(L);
96+
lua_createtable(L, 0, 2);
9797
push_float_string(L, p.X);
9898
lua_setfield(L, -2, "x");
9999
push_float_string(L, p.Y);
@@ -115,19 +115,19 @@ v2s16 read_v2s16(lua_State *L, int index)
115115

116116
void push_v2s16(lua_State *L, v2s16 p)
117117
{
118-
lua_newtable(L);
119-
lua_pushnumber(L, p.X);
118+
lua_createtable(L, 0, 2);
119+
lua_pushinteger(L, p.X);
120120
lua_setfield(L, -2, "x");
121-
lua_pushnumber(L, p.Y);
121+
lua_pushinteger(L, p.Y);
122122
lua_setfield(L, -2, "y");
123123
}
124124

125125
void push_v2s32(lua_State *L, v2s32 p)
126126
{
127-
lua_newtable(L);
128-
lua_pushnumber(L, p.X);
127+
lua_createtable(L, 0, 2);
128+
lua_pushinteger(L, p.X);
129129
lua_setfield(L, -2, "x");
130-
lua_pushnumber(L, p.Y);
130+
lua_pushinteger(L, p.Y);
131131
lua_setfield(L, -2, "y");
132132
}
133133

@@ -250,14 +250,14 @@ v3d check_v3d(lua_State *L, int index)
250250

251251
void push_ARGB8(lua_State *L, video::SColor color)
252252
{
253-
lua_newtable(L);
254-
lua_pushnumber(L, color.getAlpha());
253+
lua_createtable(L, 0, 4);
254+
lua_pushinteger(L, color.getAlpha());
255255
lua_setfield(L, -2, "a");
256-
lua_pushnumber(L, color.getRed());
256+
lua_pushinteger(L, color.getRed());
257257
lua_setfield(L, -2, "r");
258-
lua_pushnumber(L, color.getGreen());
258+
lua_pushinteger(L, color.getGreen());
259259
lua_setfield(L, -2, "g");
260-
lua_pushnumber(L, color.getBlue());
260+
lua_pushinteger(L, color.getBlue());
261261
lua_setfield(L, -2, "b");
262262
}
263263

@@ -274,12 +274,12 @@ v3f checkFloatPos(lua_State *L, int index)
274274

275275
void push_v3s16(lua_State *L, v3s16 p)
276276
{
277-
lua_newtable(L);
278-
lua_pushnumber(L, p.X);
277+
lua_createtable(L, 0, 3);
278+
lua_pushinteger(L, p.X);
279279
lua_setfield(L, -2, "x");
280-
lua_pushnumber(L, p.Y);
280+
lua_pushinteger(L, p.Y);
281281
lua_setfield(L, -2, "y");
282-
lua_pushnumber(L, p.Z);
282+
lua_pushinteger(L, p.Z);
283283
lua_setfield(L, -2, "z");
284284
}
285285

@@ -386,7 +386,7 @@ aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
386386

387387
void push_aabb3f(lua_State *L, aabb3f box)
388388
{
389-
lua_newtable(L);
389+
lua_createtable(L, 6, 0);
390390
lua_pushnumber(L, box.MinEdge.X);
391391
lua_rawseti(L, -2, 1);
392392
lua_pushnumber(L, box.MinEdge.Y);

‎src/script/lua_api/l_env.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
7373
lua_remove(L, -2); // Remove core
7474

7575
// Get registered_abms[m_id]
76-
lua_pushnumber(L, m_id);
76+
lua_pushinteger(L, m_id);
7777
lua_gettable(L, -2);
7878
if(lua_isnil(L, -1))
7979
FATAL_ERROR("");
@@ -116,7 +116,7 @@ void LuaLBM::trigger(ServerEnvironment *env, v3s16 p, MapNode n)
116116
lua_remove(L, -2); // Remove core
117117

118118
// Get registered_lbms[m_id]
119-
lua_pushnumber(L, m_id);
119+
lua_pushinteger(L, m_id);
120120
lua_gettable(L, -2);
121121
FATAL_ERROR_IF(lua_isnil(L, -1), "Entry with given id not found in registered_lbms table");
122122
lua_remove(L, -2); // Remove registered_lbms
@@ -550,7 +550,7 @@ int ModApiEnvMod::l_find_nodes_with_meta(lua_State *L)
550550
std::vector<v3s16> positions = env->getMap().findNodesWithMetadata(
551551
check_v3s16(L, 1), check_v3s16(L, 2));
552552

553-
lua_newtable(L);
553+
lua_createtable(L, positions.size(), 0);
554554
for (size_t i = 0; i != positions.size(); i++) {
555555
push_v3s16(L, positions[i]);
556556
lua_rawseti(L, -2, i + 1);
@@ -1197,7 +1197,7 @@ int ModApiEnvMod::l_find_path(lua_State *L)
11971197
searchdistance, max_jump, max_drop, algo);
11981198

11991199
if (!path.empty()) {
1200-
lua_newtable(L);
1200+
lua_createtable(L, path.size(), 0);
12011201
int top = lua_gettop(L);
12021202
unsigned int index = 1;
12031203
for (const v3s16 &i : path) {

‎src/script/lua_api/l_mapgen.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
710710
if (!mg->heightmap)
711711
return 0;
712712

713-
lua_newtable(L);
713+
lua_createtable(L, maplen, 0);
714714
for (size_t i = 0; i != maplen; i++) {
715715
lua_pushinteger(L, mg->heightmap[i]);
716716
lua_rawseti(L, -2, i + 1);
@@ -722,7 +722,7 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
722722
if (!mg->biomegen)
723723
return 0;
724724

725-
lua_newtable(L);
725+
lua_createtable(L, maplen, 0);
726726
for (size_t i = 0; i != maplen; i++) {
727727
lua_pushinteger(L, mg->biomegen->biomemap[i]);
728728
lua_rawseti(L, -2, i + 1);
@@ -736,7 +736,7 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
736736

737737
BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen;
738738

739-
lua_newtable(L);
739+
lua_createtable(L, maplen, 0);
740740
for (size_t i = 0; i != maplen; i++) {
741741
lua_pushnumber(L, bg->heatmap[i]);
742742
lua_rawseti(L, -2, i + 1);
@@ -751,7 +751,7 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
751751

752752
BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen;
753753

754-
lua_newtable(L);
754+
lua_createtable(L, maplen, 0);
755755
for (size_t i = 0; i != maplen; i++) {
756756
lua_pushnumber(L, bg->humidmap[i]);
757757
lua_rawseti(L, -2, i + 1);
@@ -761,13 +761,12 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
761761
}
762762
case MGOBJ_GENNOTIFY: {
763763
std::map<std::string, std::vector<v3s16> >event_map;
764-
std::map<std::string, std::vector<v3s16> >::iterator it;
765764

766765
mg->gennotify.getEvents(event_map);
767766

768-
lua_newtable(L);
769-
for (it = event_map.begin(); it != event_map.end(); ++it) {
770-
lua_newtable(L);
767+
lua_createtable(L, 0, event_map.size());
768+
for (auto it = event_map.begin(); it != event_map.end(); ++it) {
769+
lua_createtable(L, it->second.size(), 0);
771770

772771
for (size_t j = 0; j != it->second.size(); j++) {
773772
push_v3s16(L, it->second[j]);

‎src/script/lua_api/l_noise.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ int LuaPerlinNoiseMap::l_get_2d_map(lua_State *L)
171171
Noise *n = o->noise;
172172
n->perlinMap2D(p.X, p.Y);
173173

174-
lua_newtable(L);
174+
lua_createtable(L, n->sy, 0);
175175
for (u32 y = 0; y != n->sy; y++) {
176-
lua_newtable(L);
176+
lua_createtable(L, n->sx, 0);
177177
for (u32 x = 0; x != n->sx; x++) {
178178
lua_pushnumber(L, n->result[i++]);
179179
lua_rawseti(L, -2, x + 1);
@@ -200,7 +200,7 @@ int LuaPerlinNoiseMap::l_get_2d_map_flat(lua_State *L)
200200
if (use_buffer)
201201
lua_pushvalue(L, 3);
202202
else
203-
lua_newtable(L);
203+
lua_createtable(L, maplen, 0);
204204

205205
for (size_t i = 0; i != maplen; i++) {
206206
lua_pushnumber(L, n->result[i]);
@@ -224,11 +224,11 @@ int LuaPerlinNoiseMap::l_get_3d_map(lua_State *L)
224224
Noise *n = o->noise;
225225
n->perlinMap3D(p.X, p.Y, p.Z);
226226

227-
lua_newtable(L);
227+
lua_createtable(L, n->sz, 0);
228228
for (u32 z = 0; z != n->sz; z++) {
229-
lua_newtable(L);
229+
lua_createtable(L, n->sy, 0);
230230
for (u32 y = 0; y != n->sy; y++) {
231-
lua_newtable(L);
231+
lua_createtable(L, n->sx, 0);
232232
for (u32 x = 0; x != n->sx; x++) {
233233
lua_pushnumber(L, n->result[i++]);
234234
lua_rawseti(L, -2, x + 1);
@@ -260,7 +260,7 @@ int LuaPerlinNoiseMap::l_get_3d_map_flat(lua_State *L)
260260
if (use_buffer)
261261
lua_pushvalue(L, 3);
262262
else
263-
lua_newtable(L);
263+
lua_createtable(L, maplen, 0);
264264

265265
for (size_t i = 0; i != maplen; i++) {
266266
lua_pushnumber(L, n->result[i]);

‎src/script/lua_api/l_object.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,7 @@ int ObjectRef::l_get_pos(lua_State *L)
123123
ObjectRef *ref = checkobject(L, 1);
124124
ServerActiveObject *co = getobject(ref);
125125
if (co == NULL) return 0;
126-
v3f pos = co->getBasePosition() / BS;
127-
lua_newtable(L);
128-
lua_pushnumber(L, pos.X);
129-
lua_setfield(L, -2, "x");
130-
lua_pushnumber(L, pos.Y);
131-
lua_setfield(L, -2, "y");
132-
lua_pushnumber(L, pos.Z);
133-
lua_setfield(L, -2, "z");
126+
push_v3f(L, co->getBasePosition() / BS);
134127
return 1;
135128
}
136129

‎src/script/lua_api/l_vmanip.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int LuaVoxelManip::l_get_data(lua_State *L)
7272
if (use_buffer)
7373
lua_pushvalue(L, 2);
7474
else
75-
lua_newtable(L);
75+
lua_createtable(L, volume, 0);
7676

7777
for (u32 i = 0; i != volume; i++) {
7878
lua_Integer cid = vm->m_data[i].getContent();
@@ -261,7 +261,7 @@ int LuaVoxelManip::l_get_light_data(lua_State *L)
261261

262262
u32 volume = vm->m_area.getVolume();
263263

264-
lua_newtable(L);
264+
lua_createtable(L, volume, 0);
265265
for (u32 i = 0; i != volume; i++) {
266266
lua_Integer light = vm->m_data[i].param1;
267267
lua_pushinteger(L, light);
@@ -309,7 +309,7 @@ int LuaVoxelManip::l_get_param2_data(lua_State *L)
309309
if (use_buffer)
310310
lua_pushvalue(L, 2);
311311
else
312-
lua_newtable(L);
312+
lua_createtable(L, volume, 0);
313313

314314
for (u32 i = 0; i != volume; i++) {
315315
lua_Integer param2 = vm->m_data[i].param2;

0 commit comments

Comments
 (0)
Please sign in to comment.