Skip to content

jsonrpc2: avoid streaming decoder allocation for method strings - #1395

Open
skosovsky wants to merge 1 commit into
modelcontextprotocol:mainfrom
skosovsky:perf/method-string-decode
Open

skosovsky wants to merge 1 commit into
modelcontextprotocol:mainfrom
skosovsky:perf/method-string-decode

Conversation

@skosovsky

Copy link
Copy Markdown

Decoding "method":"tools/call" currently invokes internaljson.Unmarshal a second time after decoding the envelope. Its streaming decoder allocates a minimum 32 KiB buffer even for this short scalar. Decode the method value with encoding/json.Unmarshal instead.

The envelope still uses the existing case-sensitive, depth-limited decoder. Method presence classification (including empty and null values) is preserved. Added tests cover strings, escapes, Unicode, invalid types, field-name case sensitivity, and envelope depth rejection.

BenchmarkDecodeMethodRequest before/after on upstream base b5d319b00902, Go 1.27.1, darwin/arm64 (three runs, 1000 iterations):

Method Before B/op After B/op Allocations before → after
plain tools/call 65,890–65,896 33,095–33,100 12 → 9
escaped tools\u002fcall 65,897–65,903 33,122–33,152 12 → 10

This saves approximately 32 KiB per request decode. Timing measurements were noisy and are not used to claim an end-to-end latency improvement. This does not change the shared JSON helper or ID parsing.

Validation:

  • Method regression tests pass before and after the change.
  • go test ./... -timeout=10m
  • go test -race ./internal/jsonrpc2 ./mcp -timeout=10m
  • go vet ./...
  • go build ./...

External conformance and downstream application performance gates were not run.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-12T18:51:24.347357Z 2612452 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2612452b78

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +28 to +32
// Arrange.
wire := []byte(`{"jsonrpc":"2.0","id":1,"method":` + tt.value + `}`)
// Act.
msg, err := DecodeMessage(wire)
// Assert.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove redundant test-phase comments

In each of these new tests, the Arrange, Act, and Assert labels only restate the immediately following code and add noise instead of explaining why the test is structured this way; remove these repeated labels so the tests remain self-documenting as required by the repository's comment guidance.

AGENTS.md reference: AGENTS.md:L45-L47

Useful? React with 👍 / 👎.

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.

1 participant