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:
1 parent
158fa3d3f0
commit
0ec772ee30
5 files changed
+153
-28
No files matched your search
@@ -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 {}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user