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

+4
View File
@@ -573,6 +573,10 @@ do
if j < 0 then j = #s + j + 1 end
local n = 0
while i <= j do
local h = string.find(s, "[\128-\255]", i)
if not h or h > j then return n + j - i + 1 end
n = n + h - i
i = h
local c, l = dec(s, i)
if not c then return nil, i end
n = n + 1
+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
+68 -4
View File
@@ -18,6 +18,8 @@ export type Options = {
}
local NAME = "^[%a_:\128-\255][%w_:%.%-\128-\255]*"
local TEXT = "^([^<&%]%z\1-\8\11\12\14-\31]*)()<"
local ATTR = '^[ \t\n]+([%a_:\128-\255][%w_:%.%-\128-\255]*)[ \t\n]*=[ \t\n]*"([^"<&%c]*)"()'
local PREDEF: { [string]: string } = { lt = "<", gt = ">", amp = "&", quot = '"', apos = "'" }
local utf8char = Text.utf8char
@@ -85,7 +87,7 @@ function Parse.toUtf8(s: string): (string?, string)
if enc then
local e = lower(enc)
if e == "utf-8" or e == "utf8" or e == "us-ascii" or e == "ascii" then
if find(s, "[\128-\255]") and not utf8.len(s) then
if not utf8.len(s) then
return nil, "invalid UTF-8"
end
return s, e
@@ -100,7 +102,7 @@ function Parse.toUtf8(s: string): (string?, string)
end
return nil, "unsupported encoding " .. enc
end
if find(s, "[\128-\255]") and not utf8.len(s) then
if not utf8.len(s) then
return nil, "invalid UTF-8"
end
return s, "utf-8"
@@ -494,11 +496,33 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
return nil
end
local c = byte(s, i)
if c ~= 60 and depth > 0 and not keepWs and not preserve[depth] then
local w = match(s, "^[ \t\n]*()", i) :: number
local cw = byte(s, w)
if cw == 60 then
i = w
c = 60
elseif cw then
-- text with a non-blank at w and no ']', '&' or control characters before the next '<'
local raw, e = match(s, TEXT, i)
if raw then
i = e
return "text", raw
end
end
end
if c ~= 60 then -- text
local e = find(s, "<", i, true) or (n + 1)
local raw = sub(s, i, e - 1)
local start = i
i = e
if depth > 0 and not find(raw, "[%]&%z\1-\8\11\12\14-\31]") then
-- no ']]>', control characters or references
if keepWs or preserve[depth] or find(raw, "[^ \t\n]") then
return "text", raw
end
continue
end
local wsOnly = not find(raw, "[^ \t\n]")
if depth == 0 then
if not wsOnly then
@@ -524,6 +548,21 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
end
local c2 = byte(s, i + 1)
if c2 == 47 then -- </
local top = stack[depth]
if top then
local tl = #top
if byte(s, i + 2 + tl) == 62 and sub(s, i + 2, i + 1 + tl) == top then
i += 3 + tl
stack[depth] = nil
nsStack[depth] = nil
preserve[depth] = nil
depth -= 1
if depth == 0 then
rootDone = true
end
return "end", top
end
end
local name = match(s, NAME, i + 2)
if not name then
return fail("bad end tag", i)
@@ -617,11 +656,35 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
end
local at = i
local p = i + 1 + #name
local attrs = Types.object()
local attrs = {}
local order = {}
local na = 0
local closed = false
local nsMap: { [string]: string }? = nil
while true do
if byte(s, p) == 62 then
p += 1
break
end
-- the common attribute: name="value" with no references, controls or '<' in the value
local an, av, ae = match(s, ATTR, p)
if an then
if attrs[an] ~= nil then
return fail("duplicate attribute " .. an, match(s, "^[ \t\n]*()", p) :: number)
end
attrs[an] = av
na += 1
order[na] = an
if nsOn then
if an == "xmlns" or sub(an, 1, 6) == "xmlns:" then
local m: { [string]: string } = nsMap or {}
nsMap = m
m[if an == "xmlns" then "" else sub(an, 7)] = av
end
end
p = ae
continue
end
local q = match(s, "^[ \t\n]*()", p) :: any
local cc = byte(s, q)
if cc == 62 then
@@ -675,7 +738,8 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
return fail("duplicate attribute " .. an, q)
end
attrs[an] = v
table.insert(order, an)
na += 1
order[na] = an
if nsOn then
if an == "xmlns" or sub(an, 1, 6) == "xmlns:" then
local m: { [string]: string } = nsMap or {}
+31 -11
View File
@@ -78,7 +78,16 @@ function Xml.decode(src: string | buffer, opts: DecodeOptions?): (Node?, Err.Err
local cur: any = doc
local stack = { doc }
local depth = 1
for ev, a, b, c in Xml.events(src, o) do
local s, terr = text(src)
local u, enc = nil, ""
if s then
u, enc = Parse.toUtf8(s)
end
if not u then
return nil, terr or Err.new(if find(enc, "unsupported", 1, true) then Err.UNSUPPORTED else Err.CORRUPT, enc)
end
local str: string = u
for ev, a, b, c in Parse.events(str, o) do
if ev == "start" then
local node: any = { name = a, attrs = b, children = {}, parent = if parents then cur else nil }
if c then
@@ -118,7 +127,7 @@ function Xml.decode(src: string | buffer, opts: DecodeOptions?): (Node?, Err.Err
elseif ev == "doctype" then
doc.doctype = { raw = a, name = b.name, publicId = b.publicId, systemId = b.systemId, entities = b.entities }
elseif ev == "error" then
return nil, a
return nil, Err.new(errCode(a, b, #str), a, b)
end
end
return doc, nil
@@ -161,24 +170,27 @@ function Xml.setAttr(node: Node, name: string, value: string?)
Types.set(node.attrs, name, value)
end
-- name == nil: any element; a name without a prefix also matches prefixed names with that local part
local function matches(cn: string, name: string?, plain: boolean): boolean
return name == nil or cn == name or (plain and find(cn, ":", 1, true) ~= nil and match(cn, ":(.*)$") == name)
end
function Xml.children(node: Node, name: string?): { Node }
local out = {}
local plain = name ~= nil and not find(name, ":", 1, true)
for _, ch in node.children do
if type(ch) == "table" and string.byte(ch.name) ~= 35 then
if name == nil or ch.name == name or (not find(name, ":", 1, true) and match(ch.name, ":(.*)$") == name) then
table.insert(out, ch)
end
if type(ch) == "table" and string.byte(ch.name) ~= 35 and matches(ch.name, name, plain) then
table.insert(out, ch)
end
end
return out
end
function Xml.child(node: Node, name: string?): Node?
local plain = name ~= nil and not find(name, ":", 1, true)
for _, ch in node.children do
if type(ch) == "table" and string.byte(ch.name) ~= 35 then
if name == nil or ch.name == name or (not find(name, ":", 1, true) and match(ch.name, ":(.*)$") == name) then
return ch
end
if type(ch) == "table" and string.byte(ch.name) ~= 35 and matches(ch.name, name, plain) then
return ch
end
end
return nil
@@ -186,10 +198,11 @@ end
function Xml.descendants(node: Node, name: string?): { Node }
local out = {}
local plain = name ~= nil and not find(name, ":", 1, true)
local function walk(n: Node)
for _, ch in n.children do
if type(ch) == "table" and string.byte(ch.name) ~= 35 then
if name == nil or ch.name == name or (not find(name :: string, ":", 1, true) and match(ch.name, ":(.*)$") == name) then
if matches(ch.name, name, plain) then
table.insert(out, ch)
end
walk(ch)
@@ -207,6 +220,13 @@ function Xml.text(node: any): string
if node.name == "#cdata" then
return node.text or ""
end
local kids = node.children
local nk = #kids
if nk == 0 then
return ""
elseif nk == 1 and type(kids[1]) == "string" then
return kids[1]
end
local parts = {}
local function walk(n: any)
for _, ch in n.children do
+29 -6
View File
@@ -81,6 +81,10 @@ local function num(s: string?): number
if s == nil then
return 0
end
local v = tonumber(s)
if v then
return v
end
local t = string.lower((string.gsub(s, "^%s+", "")))
t = string.gsub(t, "%s+$", "")
if t == "inf" or t == "+inf" or t == "infinity" then
@@ -100,8 +104,27 @@ end
local function comps(node: Xml.Node?, names: { string }): { number }
local out = table.create(#names, 0)
if node then
-- the children in order, element i usually named names[i]
local kids = node.children
local k = 1
for i, n in names do
out[i] = num(childText(node, n))
local c = nil
while k <= #kids do
local ch = kids[k]
if type(ch) == "table" then
if ch.name == n then
c = ch
end
break
end
k += 1
end
if c then
k += 1
else
c = Xml.child(node, n)
end
out[i] = num(if c then Xml.text(c) else nil)
end
end
return out
@@ -117,10 +140,10 @@ end
local CF_NAMES = { "X", "Y", "Z", "R00", "R01", "R02", "R10", "R11", "R12", "R20", "R21", "R22" }
local PHYS_NAMES = { "Density", "Friction", "Elasticity", "FrictionWeight", "ElasticityWeight" }
local function readRaw(node: Xml.Node, shared: { [string]: string }, key: string): (string, any)
local function readRaw(node: Xml.Node, shared: { [string]: string }, cls: string, pname: string): (string, any)
local tag = node.name
local scalar = SCALAR_TAGS[tag]
if tag == "int" and BRICKCOLOR[key] then
if tag == "int" and BRICKCOLOR[cls .. "." .. pname] then
scalar = "BrickColor"
end
if scalar then
@@ -264,8 +287,8 @@ local function round32(v: any, which: any): any
end
return v
end
local function readValue(node: Xml.Node, shared: { [string]: string }, key: string): (string, any)
local t, v = readRaw(node, shared, key)
local function readValue(node: Xml.Node, shared: { [string]: string }, cls: string, pname: string): (string, any)
local t, v = readRaw(node, shared, cls, pname)
local which = F32[t]
if which then
v = round32(v, which)
@@ -316,7 +339,7 @@ function Rbxmx.decode(r: Bytes.Reader, opts: DecodeOptions?): (Rbx.Tree?, Err.Er
if type(pnode) == "table" and string.sub(pnode.name, 1, 1) ~= "#" then
local pname = pnode.attrs.name
if type(pname) == "string" then
local t, v = readValue(pnode, shared, cls .. "." .. pname)
local t, v = readValue(pnode, shared, cls, pname)
if pname == "Name" and (t == "String" or t == "ProtectedString") then
inst.name = v
else