perf: XML encode (name cache, escape fast path, indent cache, no key list for attribute-less elements), Types.object shares one empty key list until set, rbxm column type check
This commit is contained in:
1 parent
0ec772ee30
commit
7947dd7f3b
3 files changed
+63
-19
No files matched your search
+23
-7
@@ -665,9 +665,10 @@ local function orderOf(v: any): { any }?
|
||||
end
|
||||
return objectMeta[v]
|
||||
end
|
||||
local NOKEYS = table.freeze({})
|
||||
function Types.object(t: { [any]: any }?, keys: { any }?): { [any]: any }
|
||||
local o: { [any]: any } = t or {}
|
||||
local k = keys or {}
|
||||
local k = keys or NOKEYS
|
||||
local mt = getmetatable(o)
|
||||
if mt == nil or (type(mt) == "table" and rawget(mt, "__keys") ~= nil) then
|
||||
setmetatable(o, { __object = true, __keys = k })
|
||||
@@ -682,7 +683,16 @@ end
|
||||
function Types.set(o: { [any]: any }, k: any, v: any)
|
||||
local order = orderOf(o)
|
||||
if order and o[k] == nil and v ~= nil then
|
||||
table.insert(order, k)
|
||||
if order == NOKEYS then
|
||||
local mt = getmetatable(o)
|
||||
if type(mt) == "table" and rawget(mt, "__keys") == NOKEYS then
|
||||
mt.__keys = { k }
|
||||
else
|
||||
objectMeta[o] = { k }
|
||||
end
|
||||
else
|
||||
table.insert(order, k)
|
||||
end
|
||||
end
|
||||
o[k] = v
|
||||
end
|
||||
@@ -718,23 +728,29 @@ end
|
||||
function Types.keys(t: { [any]: any }): { any }
|
||||
local order = orderOf(t)
|
||||
local out, seen = {}, {}
|
||||
local n = 0
|
||||
if order then
|
||||
for _, k in order do
|
||||
if t[k] ~= nil and not seen[k] then
|
||||
seen[k] = true
|
||||
table.insert(out, k)
|
||||
n += 1
|
||||
out[n] = k
|
||||
end
|
||||
end
|
||||
end
|
||||
local rest = {}
|
||||
local rest: { any }? = nil
|
||||
for k in next, t do
|
||||
if not seen[k] then
|
||||
table.insert(rest, k)
|
||||
if rest then
|
||||
table.insert(rest, k)
|
||||
else
|
||||
rest = { k }
|
||||
end
|
||||
end
|
||||
end
|
||||
if #rest > 0 then
|
||||
if rest then
|
||||
table.sort(rest, keyLess)
|
||||
table.move(rest, 1, #rest, #out + 1, out)
|
||||
table.move(rest, 1, #rest, n + 1, out)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
@@ -135,7 +135,7 @@ end
|
||||
|
||||
function Xml.element(name: string, attrs: { [string]: string }?, children: { any }?): Node
|
||||
local a = attrs or Types.object()
|
||||
if not Types.isObject(a) then
|
||||
if attrs and not Types.isObject(a) then
|
||||
local keys = {}
|
||||
for k in next, a do
|
||||
table.insert(keys, k)
|
||||
@@ -271,14 +271,32 @@ local TEXT_ESC = { ["&"] = "&", ["<"] = "<", [">"] = ">", ["\r"] = "&#
|
||||
local ATTR_ESC = { ["&"] = "&", ["<"] = "<", [">"] = ">", ['"'] = """, ["\t"] = "	", ["\n"] = " ", ["\r"] = " " }
|
||||
|
||||
function Xml.escape(s: string): string
|
||||
if not find(s, "[&<>\r]") then
|
||||
return s
|
||||
end
|
||||
return (gsub(s, "[&<>\r]", TEXT_ESC))
|
||||
end
|
||||
function Xml.escapeAttr(s: string): string
|
||||
if not find(s, '[&<>"\t\n\r]') then
|
||||
return s
|
||||
end
|
||||
return (gsub(s, '[&<>"\t\n\r]', ATTR_ESC))
|
||||
end
|
||||
|
||||
local goodNames: { [string]: boolean } = {}
|
||||
local nGood = 0
|
||||
local function badName(name: any): boolean
|
||||
return type(name) ~= "string" or not match(name, "^[%a_:\128-\255][%w_:%.%-\128-\255]*$")
|
||||
if goodNames[name] then
|
||||
return false
|
||||
end
|
||||
if type(name) ~= "string" or not match(name, "^[%a_:\128-\255][%w_:%.%-\128-\255]*$") then
|
||||
return true
|
||||
end
|
||||
if nGood < 4096 then
|
||||
goodNames[name] = true
|
||||
nGood += 1
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
@@ -290,6 +308,15 @@ function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
ind = o.indent
|
||||
end
|
||||
local selfClose = o.selfClose ~= false
|
||||
local indents: { string } = {}
|
||||
local function indent(depth: number): string
|
||||
local r = indents[depth + 1]
|
||||
if not r then
|
||||
r = "\n" .. string.rep(ind :: string, depth)
|
||||
indents[depth + 1] = r
|
||||
end
|
||||
return r
|
||||
end
|
||||
local out: { string } = {}
|
||||
local err: string? = nil
|
||||
local seen: { [any]: boolean } = {}
|
||||
@@ -344,7 +371,7 @@ function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
end
|
||||
table.insert(out, "<" .. name)
|
||||
local attrs = n.attrs or {}
|
||||
for _, k in Types.keys(attrs) do
|
||||
for _, k in (if next(attrs) == nil then attrs else Types.keys(attrs)) do
|
||||
local v = attrs[k]
|
||||
if badName(k) then
|
||||
err = "bad attribute name " .. tostring(k)
|
||||
@@ -375,12 +402,12 @@ function Xml.encode(node: Node, opts: EncodeOptions?): (string?, Err.Err?)
|
||||
end
|
||||
for _, c in ch do
|
||||
if pretty then
|
||||
table.insert(out, "\n" .. string.rep(ind :: string, depth + 1))
|
||||
table.insert(out, indent(depth + 1))
|
||||
end
|
||||
write(c, depth + 1)
|
||||
end
|
||||
if pretty then
|
||||
table.insert(out, "\n" .. string.rep(ind :: string, depth))
|
||||
table.insert(out, indent(depth))
|
||||
end
|
||||
table.insert(out, "</" .. name .. ">")
|
||||
end
|
||||
|
||||
@@ -244,12 +244,13 @@ function Rbxm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Rbx.Tree?, Err.Err
|
||||
local vals, msg = Binary.decode(t, body, q + 1, be, n, ctx)
|
||||
if vals then
|
||||
local tname = Rbx.TYPE_NAMES[t]
|
||||
for i, ref in cls.refs do
|
||||
local inst = byRef[ref]
|
||||
if pname == "Name" and t == 0x01 then
|
||||
inst.name = vals[i]
|
||||
else
|
||||
inst.properties[pname] = { type = tname, value = vals[i] }
|
||||
if pname == "Name" and t == 0x01 then
|
||||
for i, ref in cls.refs do
|
||||
byRef[ref].name = vals[i]
|
||||
end
|
||||
else
|
||||
for i, ref in cls.refs do
|
||||
byRef[ref].properties[pname] = { type = tname, value = vals[i] }
|
||||
end
|
||||
end
|
||||
elseif msg == "unknown" then
|
||||
@@ -322,7 +323,7 @@ local function columnType(insts: { Rbx.Instance }, pname: string): (number?, str
|
||||
local id, tname = nil, nil
|
||||
for _, inst in insts do
|
||||
local v = inst.properties[pname]
|
||||
if v then
|
||||
if v and (type(v) ~= "table" or tname == nil or v.type ~= tname) then
|
||||
if type(v) ~= "table" or type(v.type) ~= "string" then
|
||||
return nil, "property " .. pname .. " of " .. inst.className .. " is not a { type, value } table"
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user