diff --git a/builtin/emerge/env.lua b/builtin/emerge/env.lua index 2b32a0339..5beb6285c 100644 --- a/builtin/emerge/env.lua +++ b/builtin/emerge/env.lua @@ -23,6 +23,8 @@ core.add_node = core.set_node -- we don't deal with metadata currently core.swap_node = core.set_node +core.bulk_swap_node = core.bulk_set_node + function core.remove_node(pos) return core.vmanip:set_node_at(pos, {name="air"}) end diff --git a/doc/lua_api.md b/doc/lua_api.md index e7cc8a07f..3a5f7e128 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -6145,6 +6145,8 @@ Environment access * `minetest.swap_node(pos, node)` * Swap node at position with another. * This keeps the metadata intact and will not run con-/destructor callbacks. +* `minetest.bulk_swap_node({pos1, pos2, pos3, ...}, node)` + * Equivalent to `minetest.swap_node` but in bulk. * `minetest.remove_node(pos)`: Remove a node * Equivalent to `minetest.set_node(pos, {name="air"})`, but a bit faster. * `minetest.get_node(pos)` diff --git a/games/devtest/mods/benchmarks/init.lua b/games/devtest/mods/benchmarks/init.lua index 1f5001c69..e3a4409a5 100644 --- a/games/devtest/mods/benchmarks/init.lua +++ b/games/devtest/mods/benchmarks/init.lua @@ -154,3 +154,36 @@ minetest.register_chatcommand("bench_bulk_get_node", { return true, msg end, }) + +minetest.register_chatcommand("bench_bulk_swap_node", { + params = "", + description = "Benchmark: Bulk-swap 99×99×99 stone nodes", + func = function(name, param) + local player = minetest.get_player_by_name(name) + if not player then + return false, "No player." + end + local pos_list = get_positions_cube(player:get_pos()) + + minetest.chat_send_player(name, "Benchmarking minetest.bulk_swap_node. Warming up ...") + + -- warm up because first execution otherwise becomes + -- significantly slower + minetest.bulk_swap_node(pos_list, {name = "mapgen_stone"}) + + minetest.chat_send_player(name, "Warming up finished, now benchmarking ...") + + local start_time = minetest.get_us_time() + for i=1,#pos_list do + minetest.swap_node(pos_list[i], {name = "mapgen_stone"}) + end + local middle_time = minetest.get_us_time() + minetest.bulk_swap_node(pos_list, {name = "mapgen_stone"}) + local end_time = minetest.get_us_time() + local msg = string.format("Benchmark results: minetest.swap_node loop: %.2f ms; minetest.bulk_swap_node: %.2f ms", + ((middle_time - start_time)) / 1000, + ((end_time - middle_time)) / 1000 + ) + return true, msg + end, +}) diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index a024d7e42..125a352bc 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -253,6 +253,31 @@ int ModApiEnv::l_swap_node(lua_State *L) return 1; } +// bulk_swap_node([pos1, pos2, ...], node) +// pos = {x=num, y=num, z=num} +int ModApiEnv::l_bulk_swap_node(lua_State *L) +{ + GET_ENV_PTR; + + luaL_checktype(L, 1, LUA_TTABLE); + + s32 len = lua_objlen(L, 1); + + MapNode n = readnode(L, 2); + + // Do it + bool succeeded = true; + for (s32 i = 1; i <= len; i++) { + lua_rawgeti(L, 1, i); + if (!env->swapNode(read_v3s16(L, -1), n)) + succeeded = false; + lua_pop(L, 1); + } + + lua_pushboolean(L, succeeded); + return 1; +} + // get_node_raw(x, y, z) -> content, param1, param2, pos_ok int ModApiEnv::l_get_node_raw(lua_State *L) { @@ -1377,6 +1402,7 @@ void ModApiEnv::Initialize(lua_State *L, int top) API_FCT(bulk_set_node); API_FCT(add_node); API_FCT(swap_node); + API_FCT(bulk_swap_node); API_FCT(add_item); API_FCT(remove_node); API_FCT(get_node_raw); diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index b92a6f935..ba0f2eb61 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -65,6 +65,10 @@ private: // pos = {x=num, y=num, z=num} static int l_bulk_set_node(lua_State *L); + // bulk_swap_node([pos1, pos2, ...], node) + // pos = {x=num, y=num, z=num} + static int l_bulk_swap_node(lua_State *L); + static int l_add_node(lua_State *L); // remove_node(pos)