perf: rbxmx / XML decode 2.5x: Types.object keeps the key order in the object's own metatable (no global weak map insert), XML fast paths for plain attributes / text / end tags, utf8.len validation, rbxmx components read in order

This commit is contained in:
4DBug committed 2026-11-01 19:00:12 -06:00
1 parent 158fa3d3f0
commit 0ec772ee30
5 files changed
+153 -28

No files matched your search

+21 -7
View File
@@ -652,21 +652,35 @@ Types.null = table.freeze(setmetatable({}, nullMeta))
-- Objects (maps) whose key order matters, and empty objects (which would otherwise read back as empty arrays), carry
-- an "object" metatable with their key order. Decoders mark empty objects always and every object when asked
-- (opts.ordered); encoders write an object's keys in that order (keys added later, or plain tables: sorted).
local OBJECT = { __object = true }
-- The key order is the __keys field of the object's own metatable, or (for a table that came with a metatable of its
-- own) an entry in a weak map.
local objectMeta: { [any]: { any } } = setmetatable({}, { __mode = "k" }) :: any -- object -> its key order
local function orderOf(v: any): { any }?
local mt = getmetatable(v)
if type(mt) == "table" then
local k = rawget(mt, "__keys")
if k ~= nil then
return k
end
end
return objectMeta[v]
end
function Types.object(t: { [any]: any }?, keys: { any }?): { [any]: any }
local o: { [any]: any } = t or {}
if getmetatable(o) == nil then
setmetatable(o, OBJECT)
local k = keys or {}
local mt = getmetatable(o)
if mt == nil or (type(mt) == "table" and rawget(mt, "__keys") ~= nil) then
setmetatable(o, { __object = true, __keys = k })
else
objectMeta[o] = k
end
objectMeta[o] = keys or {}
return o
end
function Types.isObject(v: any): boolean
return type(v) == "table" and objectMeta[v] ~= nil
return type(v) == "table" and orderOf(v) ~= nil
end
function Types.set(o: { [any]: any }, k: any, v: any)
local order = objectMeta[o]
local order = orderOf(o)
if order and o[k] == nil and v ~= nil then
table.insert(order, k)
end
@@ -702,7 +716,7 @@ function Types.isArray(t: { [any]: any }, emptyIsMap: boolean?): boolean
return c == n
end
function Types.keys(t: { [any]: any }): { any }
local order = objectMeta[t]
local order = orderOf(t)
local out, seen = {}, {}
if order then
for _, k in order do