Repository navigation
Conversation
checkMaxDepth stepped through every byte of its input, including string
contents, and Unmarshal runs it before every decode. A stateless
tools/call runs it 11 times, over 9x the body for a 64 KiB argument, so
the scan cost more than the decode itself.
Inside a string only an unescaped quote matters. stringEnd steps through
the first 32 bytes of a string as before, then jumps to the next quote
with bytes.IndexByte and treats it as escaped when an odd number of
backslashes precedes it. Short strings stay on the byte loop because each
IndexByte call has a fixed cost that keys and escaped JSON, with a quote
every few bytes, would pay on every quote.
The result is unchanged: a test and a fuzz target compare it with the
byte-at-a-time scan.
On an Apple M2 Pro (benchstat, n=10, medians):
CheckMaxDepth/text_64KiB 80.5µs -> 1.05µs (-98.7%)
CheckMaxDepth/json_in_string_64KiB 79.1µs -> 40.9µs (-48.3%)
CheckMaxDepth/quotes_64KiB 50.3µs -> 36.6µs (-27.2%)
UnmarshalLarge/text_64KiB 103.4µs -> 25.2µs (-75.6%)
No input in the benchmarks gets slower. Allocations are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
krus210
force-pushed
the
perf/json-depth-check
branch
from
November 21, 2026 07:19
90cb610 to
c0649bb
Compare
Author
|
Rebased onto current |
This branch has not been deployed
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.
checkMaxDepthstepped through every byte of its input, including string contents, andUnmarshalruns it before every decode. A statelesstools/callruns it 11 times, over 9x the body for a 64 KiB argument, so the scan cost more than the decode itself.Inside a string only an unescaped quote matters. The check now steps through the first 32 bytes of a string as before, then jumps to the next quote with
bytes.IndexByteand treats it as escaped when an odd number of backslashes precedes it. Short strings stay on the byte loop: callingIndexByteon every quote would make keys, empty strings and escaped JSON slower than the old scan. The result is unchanged:TestCheckMaxDepthSameAsBytewiseandFuzzCheckMaxDepthcompare it with the old byte-at-a-time scan, kept in the test as the reference.New benchmarks
BenchmarkCheckMaxDepthandBenchmarkUnmarshalLargecover a smalltools/calland ~64 KiB of text, escaped code, small objects, JSON inside a string, empty strings, and nothing but\".benchstat, Apple M2 Pro, n=10:End to end, a stateless
StreamableHTTPHandlerwith JSON responses serving onetools/call(benchmark in #1531, not part of this change):No input gets slower. Allocations are unchanged.
Fixes #1531
I used an AI assistant (Claude) to profile and draft this change; I reviewed it and can explain it.