Skip to content

perf: build JSON-RPC responses with typed structs instead of maps#1

Merged
jkyberneees merged 2 commits into
mainfrom
claude/performance-optimizations-review-77TN3
May 30, 2026
Merged

perf: build JSON-RPC responses with typed structs instead of maps#1
jkyberneees merged 2 commits into
mainfrom
claude/performance-optimizations-review-77TN3

Conversation

@jkyberneees
Copy link
Copy Markdown
Contributor

Every request handler built its result payload from ad-hoc
map[string]any literals. Replace them with typed structs
(initializeResult, toolCallResult, toolsListResult, resourcesReadResult,
etc.). Encoding a struct avoids per-request map allocation and the
runtime key-sorting that encoding/json performs for maps, while
producing byte-for-byte equivalent JSON.

Also adds benchmarks for the initialize, tools/list and tools/call paths.

Measured improvement (median of repeated runs, same machine):
initialize ~7.4us -> ~4.0us 48 -> 18 allocs 4553 -> 2824 B
tools/call ~7.7us -> ~6.3us 48 -> 35 allocs 4464 -> 3584 B
tools/list flat ns 72 -> 67 allocs 5766 -> 5350 B
(tools/list time is dominated by encoding the Tool schemas themselves,
so the wrapper change shows mainly as fewer allocations.)

https://claude.ai/code/session_015dqx2yBNNyyfgznEV36bHe

claude added 2 commits May 30, 2026 14:29
Every request handler built its result payload from ad-hoc
map[string]any literals. Replace them with typed structs
(initializeResult, toolCallResult, toolsListResult, resourcesReadResult,
etc.). Encoding a struct avoids per-request map allocation and the
runtime key-sorting that encoding/json performs for maps, while
producing byte-for-byte equivalent JSON.

Also adds benchmarks for the initialize, tools/list and tools/call paths.

Measured improvement (median of repeated runs, same machine):
  initialize  ~7.4us -> ~4.0us   48 -> 18 allocs   4553 -> 2824 B
  tools/call  ~7.7us -> ~6.3us   48 -> 35 allocs   4464 -> 3584 B
  tools/list  flat ns            72 -> 67 allocs   5766 -> 5350 B
(tools/list time is dominated by encoding the Tool schemas themselves,
so the wrapper change shows mainly as fewer allocations.)

https://claude.ai/code/session_015dqx2yBNNyyfgznEV36bHe
Add a CI workflow that runs on every push and pull request to main:
gofmt verification, go vet, race-enabled tests with coverage, library
and example builds, and a benchmark smoke run. The job matrix covers the
module's minimum Go version (1.24.3) and the latest stable release.

Also gofmt-format examples/db-explorer/main.go so the new gofmt gate
passes against the existing tree.

https://claude.ai/code/session_015dqx2yBNNyyfgznEV36bHe
@jkyberneees jkyberneees merged commit d71a60b into main May 30, 2026
5 checks passed
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.

2 participants