Skip to content

mcp: avoid double-copy of payload in writeEvent#1106

Merged
yarolegovich merged 2 commits into
modelcontextprotocol:mainfrom
incident-io:incident-io/writeevent-no-double-copy
Jul 24, 2026
Merged

mcp: avoid double-copy of payload in writeEvent#1106
yarolegovich merged 2 commits into
modelcontextprotocol:mainfrom
incident-io:incident-io/writeevent-no-double-copy

Conversation

@george1410

@george1410 george1410 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

writeEvent built the SSE frame with fmt.Fprintf(&b, "data: %s\n\n", string(evt.Data)), which copies the payload into a fresh string and then again into the buffer, growing it via repeated bytes.growSlice doublings. For large MCP responses (e.g. ~162KB tools/list) under a burst of concurrent writes this ~3x amplifies allocations and peak heap.

Write the payload directly into a pre-grown buffer instead. Output framing is byte-identical. Benchmarks (162KB payload): 517KB->172KB per op, 9->4 allocs/op, ~3x faster.

@george1410
george1410 marked this pull request as ready for review July 22, 2026 11:26
writeEvent built the SSE frame with
fmt.Fprintf(&b, "data: %s\n\n", string(evt.Data)), which copies the
payload into a fresh string and then again into the buffer, growing it
via repeated bytes.growSlice doublings. For large MCP responses
(e.g. ~162KB tools/list) under a burst of concurrent writes this ~3x
amplifies allocations and peak heap.

Write the payload directly into a pre-grown buffer instead. Output
framing is byte-identical. Benchmarks (162KB payload): 517KB->172KB per
op, 9->4 allocs/op, ~3x faster.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@george1410
george1410 force-pushed the incident-io/writeevent-no-double-copy branch from 1f1803a to 0061a33 Compare July 22, 2026 11:28

@yarolegovich yarolegovich left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the change makes sense, thanks for the contribution.

@yarolegovich
yarolegovich merged commit e761950 into modelcontextprotocol:main Jul 24, 2026
9 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