internal/json: Unmarshal allocates a 32 KiB stream buffer on every call
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 25/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- Go
- Star
- 5.3k
- Fork
- 606
- Merge trung bình
- 1 ngày 14 giờ
- Pull request đã merge (30 ngày)
- 77
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/go-sdk
-
Expose generic `SendNotification` on `ServerSession` for custom protocol extensionsCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởneeds investigation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
modelcontextprotocol/go-sdk#745 · 13 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
mcp: LoggingHandler drops records logged at custom slog levelsCó thể đã có người làm @kaankucukx đã nhận 7 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
modelcontextprotocol/go-sdk#1445 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
mcp: Client.Connect falls back to initialize after a 401 on server/discover, running the authorization flow twiceCó thể đã có người làm @kerthans đã nhận 10 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
modelcontextprotocol/go-sdk#1423 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
jsonrpc: decimal and exponent integer IDs still lose precision after #1401Có thể đã có người làm @skosovsky đã nhận 15 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 25/100
modelcontextprotocol/go-sdk#1404 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
mcp: rejected request IDs receive no JSON-RPC error envelopeCó thể đã có người làm @skosovsky đã nhận 19 ngày trước. Đang mở
Độ khó 4/5 1-2 ngày Mức phù hợp với người mới 68/100
modelcontextprotocol/go-sdk#1391 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của modelcontextprotocol/go-sdk
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
lognorman20/monaco#7428 ·
-
docs(prompts): IF BLOCKED workflow-scope branch says to comment on the issue on jira/local trackersĐang mởagent-butler-finding bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
jordansmall/spindrift#6238 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug: microcks test get swallows test failure details and operation results in text output modeĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
microcks/microcks-cli#608 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug from-studio
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
esengine/DeepSeek-Reasonix#14868 ·
Maintainer thường phản hồi trong vòng 1 ngày