From 9410a2bcd518355d1edeb16fb43ff0c5980a5cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikita=20Wi=C5=9Bniewski?= Date: Mon, 10 Feb 2025 22:34:42 +0700 Subject: [PATCH] Initial commit --- .gitignore | 3 +++ .luacheckrc | 3 +++ LICENSE | 10 ++++++++++ README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 23 +++++++++++++++++++++++ mod.conf | 3 +++ settingtypes.txt | 16 ++++++++++++++++ 7 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100644 .luacheckrc create mode 100644 LICENSE create mode 100644 README.md create mode 100644 init.lua create mode 100644 mod.conf create mode 100644 settingtypes.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c468b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.swp +*.patch +*.bak diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..d0a352c --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,3 @@ +read_globals = { + "core", "jit" +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d5710a2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,10 @@ +Permission to use, copy, modify, and/or distribute this software for any purpose +with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER +TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF +THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..110ebdb --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# Tune JIT `tune_jit` + +A Luanti mod tuning LuaJIT parameters to improve server performance by +preventing constant GC. + + +## Wait, what does it exactly do? + +At a base level, this mod implements the same thing as [VoxeLibre](https://git.minetest.land/VoxeLibre/VoxeLibre/src/commit/161dbaea03a7816db1393ca767c84a5f275ef162/mods/CORE/mcl_init/tune_jit.lua) +and [Mineclonia](https://codeberg.org/mineclonia/mineclonia/src/commit/94467d9f822ae31838e3c76d643950be2ee891f8/mods/ENTITIES/mcl_mobs/pathfinding.lua#L1627-L1636) +but for other Luanti games, with more configurability for the server operator. + +[The `jit.opt.*` API](https://luajit.org/ext_jit.html) is leveraged in order to +modify JIT compiler optimization parameters on server startup. This won't work +with LuaJIT 1.x, but if you have a LuaJIT that ancient, most of your server-side +performance issues should go away after you upgrade it. The coolest thing is +that this mod doesn't have to be in the `secure.trusted_mods` list in order to +apply the parameters. + +In [@halon](https://codeberg.org/halon)'s words: + +> Large minetest servers on stock luajit spend most of their globalsteps +> recording and jit compiling traces that are evicted for exceeding one of the +> minuscule limits on trace count or mcode size defined by default + +The specific parameters that are modified are `maxtrace`, `maxrecord`, +`minstitch` and `maxmcode`. Please refer to this mod's `settingtypes.txt` and +LuaJIT documentation for details. + + +## Will it help with client-side lag? + +Unless it was the server's fault, no. + +For singleplayer, remember that Luanti just starts up a server under the hood +and connects you to it, with the same server/client communication rules and +specifics. The only difference is that both server and client are now ran on the +same machine, though most likely in different threads. + +Mobs freezing and map generation suddenly stopping might get removed, but this +won't have any effect on rendering or audio playback. + + +## License + +The source code is licensed under 0BSD ("public domain"). See `LICENSE` file for +details. diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..38e2445 --- /dev/null +++ b/init.lua @@ -0,0 +1,23 @@ +if not core.global_exists("jit") then + core.log("warning", "[tune_jit] Couldn't find LuaJIT, nothing to tune") + return +elseif not jit.status() then + core.log("warning", "[tune_jit] LuaJIT is enabled but JIT compiler is disabled") + return +end + +local PARAM_DEFAULTS = { + maxtrace = 24000, -- 1000 for LuaJIT; 8000 for OpenResty + maxrecord = 32000, -- 4000 for LuaJIT; 16000 for OpenResty + minstitch = 3, -- 0 for LuaJIT; 3 for OpenResty + maxmcode = 163840 -- 512 for LuaJIT (O_O); 40960 for OpenResty +} + +local opt = {} +for name, default_value in pairs(PARAM_DEFAULTS) do + local value = tonumber(core.settings:get("tune_jit."..name)) or default_value + table.insert(opt, name .. "=" .. value) +end + +jit.opt.start(unpack(opt)) +core.log("action", "[tune_jit] Applied increased JIT compiler optimization parameters") diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..8c6cedd --- /dev/null +++ b/mod.conf @@ -0,0 +1,3 @@ +name = tune_jit +title = Tune JIT +description = Tunes LuaJIT parameters to improve server performance by preventing constant GC diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..f509543 --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1,16 @@ +# Maximum amount of cached traces. +# 1000 for stock, 8000 for OpenResty +tune_jit.maxtrace (Max. # of traces in cache) int 24000 0 + +# Maximum amount of IR (Intermediate Representation) instructions. +# 4000 for stock, 16000 for OpenResty +tune_jit.maxrecord (Max. # of recorded IR instructions) int 32000 0 + +# Minimum amount of IR (Intermediate Representation) instructions to be +# included in a stitched trace. +# 0 for stock, 3 for OpenResty +tune_jit.minstitch (Min. # of stitched trace IR instructions) int 3 0 + +# Maximum total size of all machine code areas, in kilobytes. +# 512 for stock, 40960 for OpenResty +tune_jit.maxmcode (Max. machine code size) int 163840 0