Repository navigation
perf: replace the recursive decoder with an iterative frame-stack decoder - #121
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
tryDecodewas paying a per-level function-call cost on nested data, and that building map results via apairsarray plus an all-string scan added allocation and a linear scan per map. This rewrite targets both.Approach
tryDecodenow 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.decodeSigned,decodeExt,decodeUnsignedInt,decodeFloat) now return the value directly instead of writing to a sharedcontext.valueslot.__proto__deferred toprotoValues, and the first non-string key converts the accumulated object to aMap. This preservespreferMap/protoActionsemantics exactly while removing the pairs-array allocation and the all-string scan.tryDecodeonce per scalar) no longer allocates an empty array.decodeArray/decodeMapare removed;buildMapremains 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
Mapkeys,preferMap,protoActionand 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-largedecode: +3–5%mixed.json(primitive/map-heavy) decode: +6–9%decoder(): ~-2% (the iterative scalar path in streaming is a touch costlier; the streaming map path still usesbuildMapand 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.