Repository navigation
Serialize a stream item straight into its notification, in a buffer each thread keeps - #71
Merged
Merged
Conversation
…ach thread keeps Each plain item of a streaming method was serialized into a JsonElement of its own, then written into a new buffer with the request id, grown from its default size, and parsed back with JsonDocument and a clone. JsonRpcStreamItem gains an overload that takes the item with its JsonTypeInfo and serializes it into the notification directly, and both overloads write into a buffer and a writer each thread keeps. A converter that creates an item while its own item is written gets a buffer of its own, and a writer left in the middle of an item by an exception is reset before the next. Through DispatchStreamingAsync an item allocates 488 bytes instead of 1168 and takes about a fifth less time; the bytes on the wire do not change. JsonElement.ParseValue, which the health check suggested, was measured too and allocated more than the document and clone here.
…he review's findings A buffer that grew for a large item stayed with its thread for as long as the thread lived, where the buffer it replaced went back to the pool. It is now dropped once it has grown past 64 KiB. The new overload lists its exceptions, the comments say why every use resets the writer, the CHANGELOG says the numbers are those of a small item, and a test compares escaped text with what a fresh writer writes.
|
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
A health check measured each item of a streamed call being copied on the server before it reaches the sink. This PR removes the two copies that do not depend on the public
IJsonRpcStreamSinkcontract.JsonRpcStreamItemgainsCreate(JsonRpcId, object?, JsonTypeInfo), which serializes the item into the notification's params directly.JsonRpcStreamStateuses it for plain items, instead ofJsonSerializer.SerializeToElementfollowed by writing that element again. Encrypted items still go throughCreate(JsonRpcId, JsonElement), sincePayloadFilter's item serializer returns aJsonElement.ArrayBufferWriterand aUtf8JsonWriterheld per thread, instead of a new buffer grown from its default size for every item. The write is synchronous. A converter that creates an item while its own item is written gets a fresh buffer, every use resets the writer, and a buffer that grew past 64 KiB for a large item is dropped afterwards, so a thread does not hold it for its lifetime.Measured with a benchmark through
JsonRpcDispatcher.DispatchStreamingAsyncand a sink that discards, three rounds each:main)The health check suggested
JsonElement.ParseValueinstead ofJsonDocument.Parse+Clone. Measured here, it allocated more (1296 B vs 1112 B with a fresh buffer) and was slower with a reused one, so it is not used.Left as is:
JsonRpcRequestthatEventStreamSinkserializes, because that is the public sink contract.data:copy (16 ns, 136 B) would need three writes per event, and a half-written event on failure.Downstream
JsonElementone byte for byte.JsonRpcStreamItem(PublicAPI.Unshipped.txt). The class is unreleased (Define the rpc.item notification once, in JsonRpcStreamItem #70), so nothing shipped changes.Verification
dotnet build0 warnings; 1518 tests pass;check-md-links.shclean.<&+>, U+2028) with what a fresh writer writes, and check that a buffer grown past 64 KiB is dropped. Each fails under a mutant: no nesting guard, no writer reset, one buffer for every thread, buffer not emptied, large buffer kept.WriteIndented,WriteAsString, both overloads, the encrypted path): identical bytes on the wire; reproduced 1168 → 488 B; 2000Task.Runitems spread over 18 threads. Its P2 (a thread kept a 4 MB buffer after a 4 MB item; measured 64 MB over 16 threads after idling) and wording findings are fixed in the second commit.