Skip to content

perf: replace the recursive decoder with an iterative frame-stack decoder - #121

Merged
mcollina merged 2 commits into
masterfrom
perf/iterative-decoder
Oct 17, 2026
Merged

mcollina merged 2 commits into
masterfrom
perf/iterative-decoder

Conversation

@mcollina

Copy link
Copy Markdown
Owner

This replaces the recursive decoder with an iterative, frame-stack one and builds map results directly instead of through an intermediate pairs array.

Motivation

Profiling showed the recursive tryDecode was paying a per-level function-call cost on nested data, and that building map results via a pairs array plus an all-string scan added allocation and a linear scan per map. This rewrite targets both.

Approach

  • tryDecode now decodes a single value using an explicit frame stack instead of recursion, so deeply nested collections no longer grow the call stack and there is no per-value dispatch/return overhead.
  • Scalar helpers (decodeSigned, decodeExt, decodeUnsignedInt, decodeFloat) now return the value directly instead of writing to a shared context.value slot.
  • Map frames build the result directly as keys and values are fed: string keys land on the plain object with __proto__ deferred to protoValues, and the first non-string key converts the accumulated object to a Map. This preserves preferMap/protoAction semantics exactly while removing the pairs-array allocation and the all-string scan.
  • The frame stack is allocated lazily on the first container frame, so decoding a bare scalar (common in the streaming path, which calls tryDecode once per scalar) no longer allocates an empty array.

decodeArray/decodeMap are removed; buildMap remains for the streaming path and empty maps.

Correctness

The full suite (4391 tests) passes on Node 22, 24 and 26, plus 6k randomized round-trips covering Map keys, preferMap, protoAction and mixed string/non-string keys decode byte-identically to the previous recursive decoder.

Benchmark (vs master)

On this machine (interleaved A/B, median of per-outer ratios):

  • sample-large decode: +3–5%
  • mixed.json (primitive/map-heavy) decode: +6–9%
  • encode: unchanged
  • streaming decoder(): ~-2% (the iterative scalar path in streaming is a touch costlier; the streaming map path still uses buildMap and is a separate opportunity)

As before, decode remains dominated by native UTF-8 work, so this is a modest but real improvement rather than another step-change.

tryDecode now decodes a single value using an explicit frame stack instead of
recursion, removing per-value function-call overhead and the deep call stack for
nested data. Collection frames accumulate child values into a flat array; array
frames yield the array, map frames yield the [k0,v0,...] pairs handed to
buildMap(), which preserves preferMap/protoAction semantics exactly. Scalar
helpers (decodeSigned/decodeExt/decodeUnsignedInt/decodeFloat) now return the
value directly instead of writing to context.value, and decodeArray/decodeMap
are gone.

All 4391 tests pass on Node 22/24/26. Benchmarked ~+2.5% decode on
sample-large; streaming is unaffected.
… alloc for scalars

The iterative decoder fed map frames as an intermediate [k0,v0,...] pairs array
that buildMap() then scanned. Instead, build the object/Map directly as keys and
values are fed: string keys go onto the plain object with __proto__ deferred to
protoValues, and the first non-string key converts the accumulated object to a
Map (preserving preferMap/protoAction semantics exactly). This removes the
pairs-array allocation and the all-string scan for every map.

The frame stack is now allocated lazily on the first container frame instead of
per tryDecode call, so decoding a bare scalar (common in the streaming path,
which calls tryDecode once per scalar) no longer allocates an empty array.

6115 random round-trips (incl. Map/protoAction/mixed-key edge cases) are
byte-identical to the previous recursive decoder; all 4391 tests pass on Node
22/24/26. Benchmark vs master: sample-large decode ~+3-5%, mixed (primitive/map)
decode ~+6-9%; streaming ~-2%, encode unchanged.
@mcollina
mcollina merged commit f1b406b into master Oct 17, 2026
20 checks passed
@mcollina
mcollina deleted the perf/iterative-decoder branch October 17, 2026 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant