Skip to content

internal/json: skip long string contents in the depth check - #1532

Open
krus210 wants to merge 1 commit into
modelcontextprotocol:mainfrom
krus210:perf/json-depth-check
Open

krus210 wants to merge 1 commit into
modelcontextprotocol:mainfrom
krus210:perf/json-depth-check

Conversation

@krus210

@krus210 krus210 commented Nov 15, 2026

Copy link
Copy Markdown

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. The check now 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: calling IndexByte on every quote would make keys, empty strings and escaped JSON slower than the old scan. The result is unchanged: TestCheckMaxDepthSameAsBytewise and FuzzCheckMaxDepth compare it with the old byte-at-a-time scan, kept in the test as the reference.

New benchmarks BenchmarkCheckMaxDepth and BenchmarkUnmarshalLarge cover a small tools/call and ~64 KiB of text, escaped code, small objects, JSON inside a string, empty strings, and nothing but \".

benchstat, Apple M2 Pro, n=10:

                                       │    before    │                after                │
                                       │    sec/op    │   sec/op     vs base                │
CheckMaxDepth/small-12                   157.95n ± 4%   78.67n ± 1%  -50.19% (p=0.000 n=10)
CheckMaxDepth/text_64KiB-12              77.822µ ± 3%   1.016µ ± 1%  -98.69% (p=0.000 n=10)
CheckMaxDepth/escaped_64KiB-12            78.93µ ± 3%   35.17µ ± 3%  -55.45% (p=0.000 n=10)
CheckMaxDepth/objects_64KiB-12           118.38µ ± 2%   58.56µ ± 3%  -50.53% (p=0.000 n=10)
CheckMaxDepth/json_in_string_64KiB-12     76.59µ ± 2%   39.27µ ± 4%  -48.73% (p=0.000 n=10)
CheckMaxDepth/empty_strings_64KiB-12      90.36µ ± 3%   65.40µ ± 2%  -27.62% (p=0.000 n=10)
CheckMaxDepth/quotes_64KiB-12             48.38µ ± 3%   35.04µ ± 3%  -27.57% (p=0.000 n=10)
UnmarshalLarge/small-12                   971.9n ± 5%   900.3n ± 4%   -7.37% (p=0.000 n=10)
UnmarshalLarge/text_64KiB-12             100.12µ ± 2%   23.29µ ± 3%  -76.73% (p=0.000 n=10)
UnmarshalLarge/escaped_64KiB-12           666.5µ ± 2%   617.8µ ± 2%   -7.31% (p=0.000 n=10)
UnmarshalLarge/objects_64KiB-12           952.6µ ± 2%   884.4µ ± 2%   -7.16% (p=0.000 n=10)
UnmarshalLarge/json_in_string_64KiB-12    696.5µ ± 3%   659.0µ ± 1%   -5.39% (p=0.000 n=10)
UnmarshalLarge/empty_strings_64KiB-12     1.170m ± 2%   1.142m ± 2%   -2.33% (p=0.011 n=10)
UnmarshalLarge/quotes_64KiB-12            652.7µ ± 2%   637.9µ ± 1%   -2.26% (p=0.000 n=10)
Unmarshal-12                              1.218µ ± 3%   1.146µ ± 3%   -5.87% (p=0.004 n=10)
geomean                                   64.93µ        33.99µ       -47.65%

End to end, a stateless StreamableHTTPHandler with JSON responses serving one tools/call (benchmark in #1531, not part of this change):

                            │    before    │                after                │
                            │    sec/op    │   sec/op     vs base                │
StatelessToolCall/small-12     31.70µ ± 3%   31.02µ ± 2%   -2.14% (p=0.015 n=10)
StatelessToolCall/64KiB-12    1024.2µ ± 1%   330.4µ ± 2%  -67.74% (p=0.000 n=10)

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.

@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 15, 2026
@github-actions github-actions Bot closed this Nov 15, 2026
@github-actions github-actions Bot reopened this Nov 16, 2026
@github-actions github-actions Bot removed the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 16, 2026
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
krus210 force-pushed the perf/json-depth-check branch from 90cb610 to c0649bb Compare November 21, 2026 07:19
@krus210

krus210 commented Nov 21, 2026

Copy link
Copy Markdown
Author

Rebased onto current main (no conflicts, go test ./... passes). Whenever you have a moment, I'd appreciate a review. Happy to adjust the approach if you'd prefer a different trade-off for short strings.

This branch has not been deployed

No deployments
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.

internal/json: the nesting depth check costs more than the decode on large messages

1 participant