PlutoLang / Pluto

A superset of Lua 5.4 with a focus on general-purpose programming.
https://pluto-lang.org/docs/Introduction
MIT License
337 stars 20 forks source link

Optimize json.encode with __order #878

Closed Sainan closed 2 weeks ago

Sainan commented 3 weeks ago

I have the following code being a huge bottleneck:

io.contents("dict." .. lang .. ".json", dicts[lang] |> json.encode|true|)

I've tried to address this in #877, and while that was a huge boost, it still takes several minutes (with the amount of data I'm processing).

Using the following Lua code, it instead only takes a couple of seconds:

local t = { "{\n" }
local first = true
for ipairs(dicts[lang].__order) as key do
    if first then
        first = false
    else
        t:insert(",\n")
    end
    t:insert("    "..json.encode(key)..": "..json.encode(dicts[lang][key]))
end
t:insert("\n}")
io.contents("dict." .. lang .. ".json", t:concat(""))

This is only an issue with __order, so this should be special-cased some more, ideally to behave similarly to the above Lua code.