aboutsummaryrefslogtreecommitdiff
path: root/games/devtest
diff options
context:
space:
mode:
authorJude Melton-Houghton <jwmhjwmh@gmail.com>2022-09-18 11:46:48 -0400
committerGitHub <noreply@github.com>2022-09-18 17:46:48 +0200
commit310b12b5edaaa415e664a7766b9226fc95ce89e4 (patch)
treefcea11433f6407d692cdcef7e2f934335b0d2d62 /games/devtest
parent006d974c584933dc4482c4a1b05f6bc0c249c8e0 (diff)
downloadminetest-310b12b5edaaa415e664a7766b9226fc95ce89e4.tar.xz
Content ID caching in Lua (#12444)
* Cache content IDs in Lua Co-authored-by: sfan5 <sfan5@live.de>
Diffstat (limited to 'games/devtest')
-rw-r--r--games/devtest/mods/experimental/commands.lua73
-rw-r--r--games/devtest/mods/unittests/content_ids.lua37
-rw-r--r--games/devtest/mods/unittests/init.lua1
3 files changed, 111 insertions, 0 deletions
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")
--------------