perf: build JSON-RPC responses with typed structs instead of maps#1
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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