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:
parent
747857bffa
commit
34e73da424
2 changed files with 16 additions and 4 deletions
|
@ -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 = {}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue