Skip to content

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

Open
george1410 wants to merge 1 commit into
modelcontextprotocol:mainfrom
incident-io:incident-io/writeevent-no-double-copy
Open

mcp: avoid double-copy of payload in writeEvent#1106
george1410 wants to merge 1 commit into
modelcontextprotocol:mainfrom
incident-io:incident-io/writeevent-no-double-copy

Conversation

@george1410

@george1410 george1410 commented Jul 22, 2026

Copy link
Copy Markdown

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
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