internal/json: Unmarshal allocates a 32 KiB stream buffer on every call
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
Start by reading internal/json.Unmarshal and UnmarshalUseNumber, then inspect the segmentio JSON decoder behavior described in the issue. Compare a proposed in-place parse path against the existing decoder path, preserving the listed error types and messages. The issue says a PR is ready with differential tests and a fuzz target; review those and run the relevant tests and benchmarks to verify behavior and allocation changes.
索引モデルが issue の本文から書いたものです。
説明
Is your feature request related to a problem? Please describe.
internal/json.Unmarshal (and UnmarshalUseNumber) wrap the input in a bytes.Reader and decode it with segmentio's streaming json.Decoder. The decoder copies its input into a read buffer of at least 32 KiB (minBufferSize), and doubles it once less than 4 KiB is free, so every call allocates 32–64 KiB however small the message is.
Unmarshal runs several times per request (the batch, the JSON-RPC message, request meta, applySchema, the typed arguments), so one tools/call over streamable HTTP allocates about half a megabyte, nearly all of it these buffers. BenchmarkStreamableServing on main reports 528 KiB/op.
I hit this while profiling a server built on the SDK (stateless streamable HTTP, JSON responses): about 90% of the bytes allocated per tool call came from segmentio/encoding/json.(*Decoder).readValue, and GC took about 28% of CPU.
#1395 avoids one of these calls (the method string). Fixing the shared helper covers all of them.
Describe the solution you'd like
Unmarshal already has the whole input, so it doesn't need a stream. It can find the extent of the first JSON value in place (json.Parse into a json.RawMessage with DontCopyRawMessage runs the same parseValue that Decoder.readValue runs on its buffer), then decode exactly that value with the same flags. When that fails, the input is malformed, and it can still go through a Decoder, so errors stay exactly as they are today: io.EOF for empty input, io.ErrUnexpectedEOF for truncated input, and the same SyntaxError and UnmarshalTypeError text.
With that change on main (Go 1.26.6, Apple M1 Max, -count=8):
BenchmarkStreamableServing 368.7µs → 219.3µs (-41%) 528 KiB → 60 KiB (-89%)
BenchmarkUnmarshal (new) 4.0µs → 1.3µs (-67%) 37 KiB → 5 KiB (-86%)
In my server's own tool-call benchmark, latency drops 36–50% and bytes allocated 84–93%. In containers limited to one CPU, tool-call throughput under load roughly triples.
I have a PR ready, with a differential test and fuzz target that compare the new path against the old Decoder path: the decoded value, the error type and the error text.
Describe alternatives you've considered
- Replacing segmentio with
encoding/json/v2(#1287) would also remove the buffer, but it raises the minimum Go version and swaps a dependency. This change is smaller, keeps today's behaviour exactly, and doesn't get in the way of #1287 later. - Decoding individual fields with
encoding/json(#1395) helps only that call site.
Additional context
Case-sensitive field matching (#805) and the nesting-depth limit are unchanged.
- 主要言語
- Go
- スター
- 5.3k
- フォーク
- 606
- 平均マージ
- 1日 16時間
- マージ済み PR(30日)
- 72
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modelcontextprotocol/go-sdk のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
modelcontextprotocol/go-sdk#1444 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
Session-lifecycle bookkeeping logs at info, spamming multiple log lines per request on stateless streamable HTTP対応中かも @anneheartrecord が 60 日前に担当しました。 オープンP3
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
modelcontextprotocol/go-sdk#1204 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Expose generic `SendNotification` on `ServerSession` for custom protocol extensions対応中かも @ajuijas が 226 日前に担当しました。 オープンneeds investigation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
modelcontextprotocol/go-sdk#745 · コメント 13 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
auth/extauth: ClientCredentialsHandler does not send the resource parameter対応中かも @shlee1223 が 2 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 25/100
modelcontextprotocol/go-sdk#1450 ·
メンテナーはふだん 1 日以内に返信
-
mcp: LoggingHandler drops records logged at custom slog levels対応中かも @kaankucukx が 3 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 45/100
modelcontextprotocol/go-sdk#1445 ·
メンテナーはふだん 1 日以内に返信
modelcontextprotocol/go-sdk の issue をすべて見る
似ている issue
-
Handler accepts zero/negative NNCP_MAX_RETRIES and NNCP backoff values, stalling policies or hot-looping対応中かも @NopeCharon が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
nmstate/kubernetes-nmstate#1604 ·
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
bug raised-by:desk
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
medici-finance/assay#4723 ·
メンテナーはふだん 1 日以内に返信
-
[doc-healer] DDUw improvement: retry integrity-filtered issue reads with direct single-item lookupsオープンautomation documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
flannel-io/flannel#2608 ·
メンテナーはふだん 2 日以内に返信