perf: JSON decode skips whitespace without a pattern call when there is none; JSON encode inlines output appends and caches indent strings

This commit is contained in:
4DBug committed 2026-11-01 19:11:09 -06:00
1 parent 7947dd7f3b
commit 11b7f89dab
2 files changed
+99 -35

No files matched your search

+28 -7
View File
@@ -29,6 +29,8 @@ local ESC: { [number]: string } = {
[116] = "\t", [116] = "\t",
} }
local WS: { [number]: boolean } = { [32] = true, [9] = true, [10] = true, [13] = true }
local utf8char = Text.utf8char local utf8char = Text.utf8char
Decode.utf8char = utf8char Decode.utf8char = utf8char
@@ -164,7 +166,9 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
local value: any local value: any
while true do while true do
i = match(s, "^[ \t\n\r]*()", i) :: any if WS[byte(s, i) or 0] then
i = match(s, "^[ \t\n\r]*()", i) :: any
end
local c = byte(s, i) local c = byte(s, i)
local haveValue = true local haveValue = true
if c == 123 then -- { if c == 123 then -- {
@@ -172,7 +176,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
if depth > maxDepth then if depth > maxDepth then
return nil, "nested deeper than " .. maxDepth, i - 1 return nil, "nested deeper than " .. maxDepth, i - 1
end end
i = match(s, "^[ \t\n\r]*()", i + 1) :: any i = i + 1
if WS[byte(s, i) or 0] then
i = match(s, "^[ \t\n\r]*()", i) :: any
end
local t = {} local t = {}
c = byte(s, i) c = byte(s, i)
if c == 125 then if c == 125 then
@@ -184,7 +191,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
if k == nil then if k == nil then
return nil, ni, at return nil, ni, at
end end
i = match(s, "^[ \t\n\r]*()", ni) :: any i = ni
if WS[byte(s, i) or 0] then
i = match(s, "^[ \t\n\r]*()", i) :: any
end
if byte(s, i) ~= 58 then if byte(s, i) ~= 58 then
return nil, if i > n then "truncated object" else "expected ':'", i - 1 return nil, if i > n then "truncated object" else "expected ':'", i - 1
end end
@@ -200,7 +210,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
if depth > maxDepth then if depth > maxDepth then
return nil, "nested deeper than " .. maxDepth, i - 1 return nil, "nested deeper than " .. maxDepth, i - 1
end end
i = match(s, "^[ \t\n\r]*()", i + 1) :: any i = i + 1
if WS[byte(s, i) or 0] then
i = match(s, "^[ \t\n\r]*()", i) :: any
end
if byte(s, i) == 93 then if byte(s, i) == 93 then
i += 1 i += 1
depth -= 1 depth -= 1
@@ -256,10 +269,15 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
t[cnt] = value t[cnt] = value
stackK[depth] = cnt stackK[depth] = cnt
end end
i = match(s, "^[ \t\n\r]*()", i) :: any if WS[byte(s, i) or 0] then
i = match(s, "^[ \t\n\r]*()", i) :: any
end
c = byte(s, i) c = byte(s, i)
if c == 44 then -- , if c == 44 then -- ,
i = match(s, "^[ \t\n\r]*()", i + 1) :: any i = i + 1
if WS[byte(s, i) or 0] then
i = match(s, "^[ \t\n\r]*()", i) :: any
end
if ord then if ord then
if byte(s, i) ~= 34 then if byte(s, i) ~= 34 then
return nil, if i > n then "truncated object" else "expected a key", i - 1 return nil, if i > n then "truncated object" else "expected a key", i - 1
@@ -268,7 +286,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?)
if k == nil then if k == nil then
return nil, ni, at return nil, ni, at
end end
i = match(s, "^[ \t\n\r]*()", ni) :: any i = ni
if WS[byte(s, i) or 0] then
i = match(s, "^[ \t\n\r]*()", i) :: any
end
if byte(s, i) ~= 58 then if byte(s, i) ~= 58 then
return nil, if i > n then "truncated object" else "expected ':'", i - 1 return nil, if i > n then "truncated object" else "expected ':'", i - 1
end end
+71 -28
View File
@@ -140,18 +140,23 @@ type State = {
maxDepth: number, maxDepth: number,
null: any, null: any,
err: string?, err: string?,
nls: { string }, -- "\n" .. indent * (i - 1)
} }
local function put(st: State, s: string) local function newline(st: State, depth: number): string
local n = st.n + 1 local r = "\n" .. string.rep(st.indent :: string, depth)
st.out[n] = s st.nls[depth + 1] = r
st.n = n return r
end end
local function value(st: State, v: any, depth: number): boolean local function value(st: State, v: any, depth: number): boolean
local tv = type(v) local tv = type(v)
local out = st.out
local n: number
if tv == "string" then if tv == "string" then
put(st, quote(v, st.ascii, st.slash)) n = st.n + 1
out[n] = quote(v, st.ascii, st.slash)
st.n = n
elseif tv == "number" then elseif tv == "number" then
local s = number(v) local s = number(v)
if not s then if not s then
@@ -161,14 +166,22 @@ local function value(st: State, v: any, depth: number): boolean
end end
s = "null" s = "null"
end end
put(st, s) n = st.n + 1
out[n] = s
st.n = n
elseif tv == "boolean" then elseif tv == "boolean" then
put(st, if v then "true" else "false") n = st.n + 1
out[n] = if v then "true" else "false"
st.n = n
elseif v == nil or v == st.null or v == Types.null then elseif v == nil or v == st.null or v == Types.null then
put(st, "null") n = st.n + 1
out[n] = "null"
st.n = n
elseif tv == "table" then elseif tv == "table" then
if Types.Date.is(v) then if Types.Date.is(v) then
put(st, '"' .. tostring(v) .. '"') n = st.n + 1
out[n] = '"' .. tostring(v) .. '"'
st.n = n
return true return true
end end
if depth >= st.maxDepth then if depth >= st.maxDepth then
@@ -178,31 +191,44 @@ local function value(st: State, v: any, depth: number): boolean
local ind = st.indent local ind = st.indent
local nl, nl2 = "", "" local nl, nl2 = "", ""
if ind then if ind then
nl = "\n" .. string.rep(ind, depth + 1) local nls = st.nls
nl2 = "\n" .. string.rep(ind, depth) nl = nls[depth + 2] or newline(st, depth + 1)
nl2 = nls[depth + 1] or newline(st, depth)
end end
if kind(v, st.emptyObject) == "array" then if kind(v, st.emptyObject) == "array" then
local n = #v local len = #v
if n == 0 then if len == 0 then
put(st, "[]") n = st.n + 1
out[n] = "[]"
st.n = n
return true return true
end end
put(st, "[") n = st.n + 1
for i = 1, n do out[n] = "["
st.n = n
for i = 1, len do
if i > 1 then if i > 1 then
put(st, ",") n = st.n + 1
out[n] = ","
st.n = n
end end
if ind then if ind then
put(st, nl) n = st.n + 1
out[n] = nl
st.n = n
end end
if not value(st, v[i], depth + 1) then if not value(st, v[i], depth + 1) then
return false return false
end end
end end
if ind then if ind then
put(st, nl2) n = st.n + 1
out[n] = nl2
st.n = n
end end
put(st, "]") n = st.n + 1
out[n] = "]"
st.n = n
else else
local keys: { any } local keys: { any }
if Types.isObject(v) then if Types.isObject(v) then
@@ -222,17 +248,25 @@ local function value(st: State, v: any, depth: number): boolean
end end
end end
if #keys == 0 then if #keys == 0 then
put(st, "{}") n = st.n + 1
out[n] = "{}"
st.n = n
return true return true
end end
put(st, "{") n = st.n + 1
out[n] = "{"
st.n = n
local sep = if ind then ": " else ":" local sep = if ind then ": " else ":"
for i, k in keys do for i, k in keys do
if i > 1 then if i > 1 then
put(st, ",") n = st.n + 1
out[n] = ","
st.n = n
end end
if ind then if ind then
put(st, nl) n = st.n + 1
out[n] = nl
st.n = n
end end
local ks: string local ks: string
if type(k) == "string" then if type(k) == "string" then
@@ -243,16 +277,24 @@ local function value(st: State, v: any, depth: number): boolean
st.err = "object key of type " .. type(k) st.err = "object key of type " .. type(k)
return false return false
end end
put(st, quote(ks, st.ascii, st.slash)) n = st.n + 1
put(st, sep) out[n] = quote(ks, st.ascii, st.slash)
st.n = n
n = st.n + 1
out[n] = sep
st.n = n
if not value(st, v[k], depth + 1) then if not value(st, v[k], depth + 1) then
return false return false
end end
end end
if ind then if ind then
put(st, nl2) n = st.n + 1
out[n] = nl2
st.n = n
end end
put(st, "}") n = st.n + 1
out[n] = "}"
st.n = n
end end
elseif tv == "buffer" then elseif tv == "buffer" then
st.err = "cannot encode a buffer (encode it as base64 or an array first)" st.err = "cannot encode a buffer (encode it as base64 or an array first)"
@@ -284,6 +326,7 @@ local function state(opts: Options?, nullv: any?): State
maxDepth = o.maxDepth or 1000, maxDepth = o.maxDepth or 1000,
null = nullv, null = nullv,
err = nil, err = nil,
nls = {},
} }
end end
Encode.state = state Encode.state = state