Conversation
Co-authored-by: krishkat <krishna.kater@gmail.com>
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.
This round finalizes the approach for async deserialization.
Async deserialization uses a two-pass design:
the first pass scans for message boundaries, and the second pass synchronously deserializes the now-bounded buffer.
True async, meaning a design that checks for and awaits buffer refills in the middle of decoding a value, costs too much performance.
Even granting that .NET 11's async runtime may improve things somewhat.
Adding complex code to compensate for that degradation, such as buffer-size checks or bouncing between async and sync paths, is wasted effort.
Grabbing a buffer and processing it synchronously in one go is the foundation of performance,
and serializers are inherently suited to working that way: message boundaries form well-defined blocks, and a single block is never extremely large.
Since skipping is much lighter than actually reading, the performance concern about being two-pass mostly doesn't materialize.
It is certainly better than true async.
PipeReader's (consumed, examined) model is a natural fit for this kind of processing.
By delegating all the complex buffer management to PipeReader, the scanner that actually finds message boundaries ends up being a small amount of code.
Additionally, when the reader is
IsCompleted, the two-pass flow is skipped entirely and the data is deserialized synchronously right away.This eliminates the overhead in typical situations such as a fully received request body in ASP.NET.
The final API looks like this:
DeserializeMessagesAsyncstreams a sequence of concatenated messages, JSONL-style.DeserializeElementsAsyncstreams the elements of a MessagePack array.These correspond to
DeserializeAsyncEnumerable(bool topLevelValues)in System.Text.Json.