Repository navigation
zero-swift: deep JSON without recursion; nesting limit 1,500 - #17
Merged
Merged
Conversation
…cursion JSONParser accepts 5,000 levels, but parsing recursed and overflowed a 512 KB stack (a macOS test or cooperative thread) at about 300 levels in a debug build. The parser, JSONSerializer, the query hash's visitValue, and JSONValue's hash now keep the arrays and objects they are in on an explicit stack. JSONValue's == recurses for the first 64 levels, which is faster, and goes on without recursion past them. Freeing a deep value still recurses (Array's and JSONObject's deinit): a 5,000-deep value fits in 1 MB in a release build, not in 512 KB. SweepWireTests no longer needs an 8 MB stack to parse; deepRowValue runs on 512 KB and frees on 8 MB. JSONDepthTests parses, writes, compares, and hashes 5,000-deep arrays and objects on a 512 KB thread. Amp-Thread-ID: https://ampcode.com/threads/T-c513a037-a257-45e0-b9a9-15c15edfe859 Co-authored-by: tmm <tmm@tmm.dev>
… small stack Freeing a value still recurses in Array's and JSONObject's storage. At 1,500 levels it fits a 512 KB stack in a release build. Documents the limit as a difference from JS in JSParity.md. Amp-Thread-ID: https://ampcode.com/threads/T-c513a037-a257-45e0-b9a9-15c15edfe859 Co-authored-by: tmm <tmm@tmm.dev>
tmm
marked this pull request as ready for review
October 22, 2026 14:53
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.
Option (c), approved by tmm: iterative JSON code plus a lower nesting limit, with the limit documented as a difference from JS.
What changed:
JSONParseris one loop with an explicit stack. Key order, duplicate-key handling, and error messages and offsets are unchanged.JSONSerializer.write,JSONValue.hash,QueryHashvalue hashing andJSONValue.==don't recurse.==recurses for the first 64 levels and switches to an explicit stack below that.JSONParser.maxDepthdrops from 5,000 to 1,500. Freeing a value still recurses, inArray's andJSONObject's storage. At 1,500 levels that fits a 512 KB stack in a release build.Tests:
JSONDepthTestschecks values exactlymaxDepthdeep on a 512 KB thread. On the old code it crashes with signal 11.SweepWireTests.deepRowValuechecks a poke at the limit on 512 KB, including freeing it, and checks that one more level is rejected.Cost (release, same process, old vs new):
==on large nested values: +35–40%.Tested: 209 tests pass under
ulimit -s 512. Also lint, ClientE2ETests (2) and the zbugs-swift build.