Repository navigation
Reduce allocations when serializing large content blocks and SSE JSON-RPC responses - #2079
Open
flipcyde101 wants to merge 1 commit into
Open
flipcyde101 wants to merge 1 commit into
flipcyde101 wants to merge 1 commit into
Conversation
…-RPC responses Signed-off-by: Flipcyde <169501444+flipcyde101@users.noreply.github.com>
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.
Implements proposed optimizations (1) and (2) from #1971 to significantly reduce LOH/buffer allocations when serializing large tool/resource content payloads and JSON-RPC SSE responses, with no public API changes.
Addresses #1971
What Changed
McpJsonUtilities.WriteStringSegmentedhelpers (string?andReadOnlySpan<byte>overloads) that useUtf8JsonWriter.WriteStringValueSegmentin 16 KiB chunks when payloads exceed 64 KiB onnet10.0andnetstandard2.0(which referencesSystem.Text.Json10.x), falling back toWriteStringonnet8.0/net9.0and for small payloads.ContentBlock.Converter.Write(TextContentBlock.Text,ImageContentBlock.Data,AudioContentBlock.Data) andResourceContents.Converter.Write(TextResourceContents.Text,BlobResourceContents.Blob) to useWriteStringSegmented, bounding the escape scratch buffer to the chunk size instead of growing proportionally to the full payload.SseEventWriter:PooledByteBufferWriter.WrittenSpanand addedSseEventWriter.WriteSingleLineJsonAsync, which writes thedata:prefix and serializes JSON directly into_bufferWriter, eliminating the intermediate_userDataBufferWritercopy.\r/\nbytes emitted by custom converters (WriteRawValue) in place to spaces using vectorizedIndexOfAny, preserving single-line SSE framing without re-copying._userDataBufferWriterso JSON-only SSE writers avoid allocating a second pooled buffer, and wrapped writes intry/finallyto guarantee buffer resets on cancellation/error.McpSseEventWriterExtensions.WriteAsync(this SseEventWriter, SseItem<JsonRpcMessage?>, ...)throughWriteSingleLineJsonAsync.tests/ModelContextProtocol.Tests/Protocol/LargeContentSerializationTests.cs):TextContentBlockandTextResourceContentswith surrogate pairs placed across 16 KiB segment boundaries (verifying byte-for-byte identity with unsegmentedUtf8JsonWriter.WriteStringand exact round-trip), >64 KiBImageContentBlock/AudioContentBlock/BlobResourceContentsbase64 payloads, end-to-endSseResponseStreamTransportframing of largeJsonRpcResponses, and a.NET 10per-thread allocation bound check (< 1 MiB scratch allocation for a 1M-char escaping payload).How Has This Been Tested?
dotnet build -c Release src/ModelContextProtocol.Core/ModelContextProtocol.Core.csproj(netstandard2.0,net8.0,net9.0,net10.0): 0 warnings, 0 errors.dotnet test tests/ModelContextProtocol.Tests/ModelContextProtocol.Tests.csproj -f net10.0 --filter FullyQualifiedName~LargeContentSerializationTests|FullyQualifiedName~ContentBlock|FullyQualifiedName~ResourceContents|FullyQualifiedName~Sse: 448 passed, 0 failed, 2 skipped (Docker-only).