1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Optimize appending to tables in core.serialize and dump

This commit is contained in:
Lars Mueller 2025-05-02 14:52:43 +02:00 committed by Lars Müller
parent 747857bffa
commit 34e73da424
2 changed files with 16 additions and 4 deletions

View file

@ -122,8 +122,16 @@ function dump(value, indent)
local newline = indent == "" and "" or "\n"
local rope = {}
local function write(str)
table.insert(rope, str)
local write
do
-- Keeping the length of the table as a local variable is *much*
-- faster than invoking the length operator.
-- See https://gitspartv.github.io/LuaJIT-Benchmarks/#test12.
local i = 0
function write(str)
i = i + 1
rope[i] = str
end
end
local n_refs = {}

View file

@ -218,9 +218,13 @@ function core.serialize(value)
core.log("deprecated", "Support for dumping functions in `core.serialize` is deprecated.")
end
local rope = {}
-- Keeping the length of the table as a local variable is *much*
-- faster than invoking the length operator.
-- See https://gitspartv.github.io/LuaJIT-Benchmarks/#test12.
local i = 0
serialize(value, function(text)
-- Faster than table.insert(rope, text) on PUC Lua 5.1
rope[#rope + 1] = text
i = i + 1
rope[i] = text
end)
return table_concat(rope)
end