Repository navigation
fix(core): avoid quadratic copying and scanning in ReadBuffer (#2961) - #2965
Open
SuparvaCode wants to merge 1 commit into
Open
SuparvaCode wants to merge 1 commit into
SuparvaCode wants to merge 1 commit into
Conversation
…ontextprotocol#2961) - Buffer chunks in an array instead of copying the whole accumulated buffer with Buffer.concat on every append. - Inspect newly appended chunks for delimiters instead of repeatedly rescanning the entire buffer from offset 0. - Slice and concatenate only when a complete newline-delimited message is ready. - Add regression tests covering chunked message streaming, multi-message chunks, and uneven split boundaries. Co-authored-by: Claude <noreply@anthropic.com> Signed-off-by: Suparva <suparva@outlook.in>
🦋 Changeset detectedLatest commit: ec31eab The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@modelcontextprotocol/client
@modelcontextprotocol/codemod
@modelcontextprotocol/core
@modelcontextprotocol/server
@modelcontextprotocol/server-legacy
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
This branch has not been deployed
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.
Summary
Fixes #2961
ReadBufferpreviously spent time quadratic in single message size when receiving large messages delivered across chunks:append(chunk)– repeatedly invokedBuffer.concat([this._buffer, chunk]), copying the entire accumulated buffer on every incoming chunk.readMessage()– repeatedly scanned from byte 0 withthis._buffer.indexOf('\n')on every iteration before a complete delimiter was received.For a 100 MB message in 64 KiB chunks, this resulted in ~80 GB of intermediate copying and hundreds of millions of redundant byte comparisons, taking ~38s on the event loop (and timing out clients in production).
Changes
_chunks: Buffer[]) without intermediate copying.6x0a) only within newly appended chunks as they arrive.maxBufferSizeguard and bounds checking behavior.packages/core-internal/test/shared/stdio.test.tscovering:@modelcontextprotocol/core-internal.Verification
@modelcontextprotocol/core-internalunit test suite (71 test files, 1,529 tests passed).@mcp-examples/server-quickstart(tsc) with strictnoUncheckedIndexedAccess.