From 310b12b5edaaa415e664a7766b9226fc95ce89e4 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Sun, 18 Sep 2022 11:46:48 -0400 Subject: Content ID caching in Lua (#12444) * Cache content IDs in Lua Co-authored-by: sfan5 --- builtin/async/game.lua | 7 +- builtin/common/item_s.lua | 225 +++++++++++++++++++++++++++ builtin/game/init.lua | 4 +- builtin/game/item_s.lua | 179 --------------------- games/devtest/mods/experimental/commands.lua | 73 +++++++++ games/devtest/mods/unittests/content_ids.lua | 37 +++++ games/devtest/mods/unittests/init.lua | 1 + 7 files changed, 345 insertions(+), 181 deletions(-) create mode 100644 builtin/common/item_s.lua delete mode 100644 builtin/game/item_s.lua create mode 100644 games/devtest/mods/unittests/content_ids.lua diff --git a/builtin/async/game.lua b/builtin/async/game.lua index 6512f0706..0b7a7ef0e 100644 --- a/builtin/async/game.lua +++ b/builtin/async/game.lua @@ -13,9 +13,12 @@ end -- Import a bunch of individual files from builtin/game/ local gamepath = core.get_builtin_path() .. "game" .. DIR_DELIM +local commonpath = core.get_builtin_path() .. "common" .. DIR_DELIM + +local builtin_shared = {} dofile(gamepath .. "constants.lua") -dofile(gamepath .. "item_s.lua") +assert(loadfile(commonpath .. "item_s.lua"))(builtin_shared) dofile(gamepath .. "misc_s.lua") dofile(gamepath .. "features.lua") dofile(gamepath .. "voxelarea.lua") @@ -57,3 +60,5 @@ setmetatable(core.registered_items, alias_metatable) setmetatable(core.registered_nodes, alias_metatable) setmetatable(core.registered_craftitems, alias_metatable) setmetatable(core.registered_tools, alias_metatable) + +builtin_shared.cache_content_ids() diff --git a/builtin/common/item_s.lua b/builtin/common/item_s.lua new file mode 100644 index 000000000..6e4269b7d --- /dev/null +++ b/builtin/common/item_s.lua @@ -0,0 +1,225 @@ +-- Minetest: builtin/item_s.lua +-- The distinction of what goes here is a bit tricky, basically it's everything +-- that does not (directly or indirectly) need access to ServerEnvironment, +-- Server or writable access to IGameDef on the engine side. +-- (The '_s' stands for standalone.) + +local builtin_shared = ... + +-- +-- Item definition helpers +-- + +function core.inventorycube(img1, img2, img3) + img2 = img2 or img1 + img3 = img3 or img1 + return "[inventorycube" + .. "{" .. img1:gsub("%^", "&") + .. "{" .. img2:gsub("%^", "&") + .. "{" .. img3:gsub("%^", "&") +end + +function core.dir_to_facedir(dir, is6d) + --account for y if requested + if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then + + --from above + if dir.y < 0 then + if math.abs(dir.x) > math.abs(dir.z) then + if dir.x < 0 then + return 19 + else + return 13 + end + else + if dir.z < 0 then + return 10 + else + return 4 + end + end + + --from below + else + if math.abs(dir.x) > math.abs(dir.z) then + if dir.x < 0 then + return 15 + else + return 17 + end + else + if dir.z < 0 then + return 6 + else + return 8 + end + end + end + + --otherwise, place horizontally + elseif math.abs(dir.x) > math.abs(dir.z) then + if dir.x < 0 then + return 3 + else + return 1 + end + else + if dir.z < 0 then + return 2 + else + return 0 + end + end +end + +-- Table of possible dirs +local facedir_to_dir = { + vector.new( 0, 0, 1), + vector.new( 1, 0, 0), + vector.new( 0, 0, -1), + vector.new(-1, 0, 0), + vector.new( 0, -1, 0), + vector.new( 0, 1, 0), +} +-- Mapping from facedir value to index in facedir_to_dir. +local facedir_to_dir_map = { + [0]=1, 2, 3, 4, + 5, 2, 6, 4, + 6, 2, 5, 4, + 1, 5, 3, 6, + 1, 6, 3, 5, + 1, 4, 3, 2, +} +function core.facedir_to_dir(facedir) + return facedir_to_dir[facedir_to_dir_map[facedir % 32]] +end + +function core.dir_to_fourdir(dir) + if math.abs(dir.x) > math.abs(dir.z) then + if dir.x < 0 then + return 3 + else + return 1 + end + else + if dir.z < 0 then + return 2 + else + return 0 + end + end +end + +function core.fourdir_to_dir(fourdir) + return facedir_to_dir[facedir_to_dir_map[fourdir % 4]] +end + +function core.dir_to_wallmounted(dir) + if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then + if dir.y < 0 then + return 1 + else + return 0 + end + elseif math.abs(dir.x) > math.abs(dir.z) then + if dir.x < 0 then + return 3 + else + return 2 + end + else + if dir.z < 0 then + return 5 + else + return 4 + end + end +end + +-- table of dirs in wallmounted order +local wallmounted_to_dir = { + [0] = vector.new( 0, 1, 0), + vector.new( 0, -1, 0), + vector.new( 1, 0, 0), + vector.new(-1, 0, 0), + vector.new( 0, 0, 1), + vector.new( 0, 0, -1), +} +function core.wallmounted_to_dir(wallmounted) + return wallmounted_to_dir[wallmounted % 8] +end + +function core.dir_to_yaw(dir) + return -math.atan2(dir.x, dir.z) +end + +function core.yaw_to_dir(yaw) + return vector.new(-math.sin(yaw), 0, math.cos(yaw)) +end + +function core.is_colored_paramtype(ptype) + return (ptype == "color") or (ptype == "colorfacedir") or + (ptype == "color4dir") or (ptype == "colorwallmounted") or + (ptype == "colordegrotate") +end + +function core.strip_param2_color(param2, paramtype2) + if not core.is_colored_paramtype(paramtype2) then + return nil + end + if paramtype2 == "colorfacedir" then + param2 = math.floor(param2 / 32) * 32 + elseif paramtype2 == "color4dir" then + param2 = math.floor(param2 / 4) * 4 + elseif paramtype2 == "colorwallmounted" then + param2 = math.floor(param2 / 8) * 8 + elseif paramtype2 == "colordegrotate" then + param2 = math.floor(param2 / 32) * 32 + end + -- paramtype2 == "color" requires no modification. + return param2 +end + +-- Content ID caching + +local old_get_content_id = core.get_content_id +local old_get_name_from_content_id = core.get_name_from_content_id + +local name2content = setmetatable({}, { + __index = function(self, name) + return old_get_content_id(name) + end, +}) + +local content2name = setmetatable({}, { + __index = function(self, id) + return old_get_name_from_content_id(id) + end, +}) + +function core.get_content_id(name) + return name2content[name] +end + +function core.get_name_from_content_id(id) + return content2name[id] +end + +-- Cache content IDs after they have stopped changing. +function builtin_shared.cache_content_ids() + for name in pairs(core.registered_nodes) do + local id = old_get_content_id(name) + name2content[name] = id + content2name[id] = name + end + -- unknown is not in the registered node list. + local unknown_name = old_get_name_from_content_id(core.CONTENT_UNKNOWN) + name2content[unknown_name] = core.CONTENT_UNKNOWN + content2name[core.CONTENT_UNKNOWN] = unknown_name + + for name in pairs(core.registered_aliases) do + if core.registered_nodes[name] then + name2content[name] = old_get_content_id(name) + end + end +end diff --git a/builtin/game/init.lua b/builtin/game/init.lua index d7606f357..b9d2c0baf 100644 --- a/builtin/game/init.lua +++ b/builtin/game/init.lua @@ -8,7 +8,7 @@ local gamepath = scriptpath .. "game".. DIR_DELIM local builtin_shared = {} dofile(gamepath .. "constants.lua") -dofile(gamepath .. "item_s.lua") +assert(loadfile(commonpath .. "item_s.lua"))(builtin_shared) assert(loadfile(gamepath .. "item.lua"))(builtin_shared) dofile(gamepath .. "register.lua") @@ -37,4 +37,6 @@ dofile(gamepath .. "statbars.lua") dofile(gamepath .. "knockback.lua") dofile(gamepath .. "async.lua") +core.after(0, builtin_shared.cache_content_ids) + profiler = nil diff --git a/builtin/game/item_s.lua b/builtin/game/item_s.lua deleted file mode 100644 index ac1090d63..000000000 --- a/builtin/game/item_s.lua +++ /dev/null @@ -1,179 +0,0 @@ --- Minetest: builtin/item_s.lua --- The distinction of what goes here is a bit tricky, basically it's everything --- that does not (directly or indirectly) need access to ServerEnvironment, --- Server or writable access to IGameDef on the engine side. --- (The '_s' stands for standalone.) - --- --- Item definition helpers --- - -function core.inventorycube(img1, img2, img3) - img2 = img2 or img1 - img3 = img3 or img1 - return "[inventorycube" - .. "{" .. img1:gsub("%^", "&") - .. "{" .. img2:gsub("%^", "&") - .. "{" .. img3:gsub("%^", "&") -end - -function core.dir_to_facedir(dir, is6d) - --account for y if requested - if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then - - --from above - if dir.y < 0 then - if math.abs(dir.x) > math.abs(dir.z) then - if dir.x < 0 then - return 19 - else - return 13 - end - else - if dir.z < 0 then - return 10 - else - return 4 - end - end - - --from below - else - if math.abs(dir.x) > math.abs(dir.z) then - if dir.x < 0 then - return 15 - else - return 17 - end - else - if dir.z < 0 then - return 6 - else - return 8 - end - end - end - - --otherwise, place horizontally - elseif math.abs(dir.x) > math.abs(dir.z) then - if dir.x < 0 then - return 3 - else - return 1 - end - else - if dir.z < 0 then - return 2 - else - return 0 - end - end -end - --- Table of possible dirs -local facedir_to_dir = { - vector.new( 0, 0, 1), - vector.new( 1, 0, 0), - vector.new( 0, 0, -1), - vector.new(-1, 0, 0), - vector.new( 0, -1, 0), - vector.new( 0, 1, 0), -} --- Mapping from facedir value to index in facedir_to_dir. -local facedir_to_dir_map = { - [0]=1, 2, 3, 4, - 5, 2, 6, 4, - 6, 2, 5, 4, - 1, 5, 3, 6, - 1, 6, 3, 5, - 1, 4, 3, 2, -} -function core.facedir_to_dir(facedir) - return facedir_to_dir[facedir_to_dir_map[facedir % 32]] -end - -function core.dir_to_fourdir(dir) - if math.abs(dir.x) > math.abs(dir.z) then - if dir.x < 0 then - return 3 - else - return 1 - end - else - if dir.z < 0 then - return 2 - else - return 0 - end - end -end - -function core.fourdir_to_dir(fourdir) - return facedir_to_dir[facedir_to_dir_map[fourdir % 4]] -end - -function core.dir_to_wallmounted(dir) - if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then - if dir.y < 0 then - return 1 - else - return 0 - end - elseif math.abs(dir.x) > math.abs(dir.z) then - if dir.x < 0 then - return 3 - else - return 2 - end - else - if dir.z < 0 then - return 5 - else - return 4 - end - end -end - --- table of dirs in wallmounted order -local wallmounted_to_dir = { - [0] = vector.new( 0, 1, 0), - vector.new( 0, -1, 0), - vector.new( 1, 0, 0), - vector.new(-1, 0, 0), - vector.new( 0, 0, 1), - vector.new( 0, 0, -1), -} -function core.wallmounted_to_dir(wallmounted) - return wallmounted_to_dir[wallmounted % 8] -end - -function core.dir_to_yaw(dir) - return -math.atan2(dir.x, dir.z) -end - -function core.yaw_to_dir(yaw) - return vector.new(-math.sin(yaw), 0, math.cos(yaw)) -end - -function core.is_colored_paramtype(ptype) - return (ptype == "color") or (ptype == "colorfacedir") or - (ptype == "color4dir") or (ptype == "colorwallmounted") or - (ptype == "colordegrotate") -end - -function core.strip_param2_color(param2, paramtype2) - if not core.is_colored_paramtype(paramtype2) then - return nil - end - if paramtype2 == "colorfacedir" then - param2 = math.floor(param2 / 32) * 32 - elseif paramtype2 == "color4dir" then - param2 = math.floor(param2 / 4) * 4 - elseif paramtype2 == "colorwallmounted" then - param2 = math.floor(param2 / 8) * 8 - elseif paramtype2 == "colordegrotate" then - param2 = math.floor(param2 / 32) * 32 - end - -- paramtype2 == "color" requires no modification. - return param2 -end diff --git a/games/devtest/mods/experimental/commands.lua b/games/devtest/mods/experimental/commands.lua index e42ae954d..4cec2e687 100644 --- a/games/devtest/mods/experimental/commands.lua +++ b/games/devtest/mods/experimental/commands.lua @@ -43,6 +43,79 @@ minetest.register_chatcommand("test_bulk_set_node", { end, }) +-- Safeguard against too much optimization. This way the results cannot be optimized +-- away, but they can be garbage collected (due to __mode = "k"). +_G._bench_content_ids_data = setmetatable({}, {__mode = "k"}) + +local function bench_name2content() + local t = {} + _G._bench_content_ids_data[t] = true + + local get_content_id = minetest.get_content_id + + local start = minetest.get_us_time() + + for i = 1, 200 do + for name in pairs(minetest.registered_nodes) do + t[#t + 1] = get_content_id(name) + end + end + + local finish = minetest.get_us_time() + + return (finish - start) / 1000 +end + +local function bench_content2name() + local t = {} + _G._bench_content_ids_data[t] = true + + -- Try to estimate the highest content ID that's used + -- (not accurate but good enough for this test) + local n = 0 + for _ in pairs(minetest.registered_nodes) do + n = n + 1 + end + + local get_name_from_content_id = minetest.get_name_from_content_id + + local start = minetest.get_us_time() + + for i = 1, 200 do + for j = 0, n do + t[#t + 1] = get_name_from_content_id(j) + end + end + + local finish = minetest.get_us_time() + + return (finish - start) / 1000 +end + +minetest.register_chatcommand("bench_name2content", { + params = "", + description = "Benchmark: Conversion from node names to content IDs", + func = function(name, param) + minetest.chat_send_player(name, "Benchmarking minetest.get_content_id. Warming up ...") + bench_name2content() + minetest.chat_send_player(name, "Warming up finished, now benchmarking ...") + local time = bench_name2content() + return true, ("Time: %.2f ms"):format(time) + end, +}) + +minetest.register_chatcommand("bench_content2name", { + params = "", + description = "Benchmark: Conversion from content IDs to node names", + func = function(name, param) + minetest.chat_send_player(name, "Benchmarking minetest.get_name_from_content_id. Warming up ...") + bench_content2name() + minetest.chat_send_player(name, "Warming up finished, now benchmarking ...") + local time = bench_content2name() + return true, ("Time: %.2f ms"):format(time) + end, +}) + minetest.register_chatcommand("bench_bulk_set_node", { params = "", description = "Benchmark: Bulk-set 99×99×99 stone nodes", diff --git a/games/devtest/mods/unittests/content_ids.lua b/games/devtest/mods/unittests/content_ids.lua new file mode 100644 index 000000000..d2f1e0c35 --- /dev/null +++ b/games/devtest/mods/unittests/content_ids.lua @@ -0,0 +1,37 @@ +core.register_alias("unittests:test_content_ids_alias1", "air") +core.register_alias("unittests:test_content_ids_alias2", "~") + +local function test_content_ids() + assert(core.get_content_id("air") == core.CONTENT_AIR) + assert(core.get_content_id("unittests:test_content_ids_alias1") == core.CONTENT_AIR) + assert(core.get_content_id("unknown") == core.CONTENT_UNKNOWN) + assert(core.get_content_id("ignore") == core.CONTENT_IGNORE) + + assert(core.get_name_from_content_id(core.CONTENT_AIR) == "air") + assert(core.get_name_from_content_id(core.CONTENT_UNKNOWN) == "unknown") + assert(core.get_name_from_content_id(core.CONTENT_IGNORE) == "ignore") + + assert(pcall(core.get_content_id, "~") == false) + assert(pcall(core.get_content_id, "unittests:test_content_ids_alias2") == false) + assert(pcall(core.get_content_id) == false) + assert(core.get_name_from_content_id(0xFFFF) == "unknown") + assert(pcall(core.get_name_from_content_id) == false) +end + +-- Run while mod is loading. +test_content_ids() + +-- Run after mods have loaded. +unittests.register("test_content_ids", test_content_ids) + +-- Run in async environment. +local function test_content_ids_async(cb) + local function func(test_func) + local ok, err = pcall(test_func) + if not ok then + return err + end + end + core.handle_async(func, cb, test_content_ids) +end +unittests.register("test_content_ids_async", test_content_ids_async, {async=true}) diff --git a/games/devtest/mods/unittests/init.lua b/games/devtest/mods/unittests/init.lua index 0e041be76..f93d516d7 100644 --- a/games/devtest/mods/unittests/init.lua +++ b/games/devtest/mods/unittests/init.lua @@ -178,6 +178,7 @@ dofile(modpath .. "/crafting.lua") dofile(modpath .. "/itemdescription.lua") dofile(modpath .. "/async_env.lua") dofile(modpath .. "/entity.lua") +dofile(modpath .. "/content_ids.lua") -------------- -- cgit v1.2.3