[Json] Avoid ReadOnlySequence with Stream deserializing#118408
Merged
BrennanConroy merged 2 commits intodotnet:mainfrom Aug 7, 2025
Merged
[Json] Avoid ReadOnlySequence with Stream deserializing#118408BrennanConroy merged 2 commits intodotnet:mainfrom
BrennanConroy merged 2 commits intodotnet:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes JSON deserialization from streams by eliminating redundant ReadOnlySequence creation and conditional checks when constructing Utf8JsonReader instances. The change aims to address performance regressions noted in issue #118173.
Key changes:
- Refactors reader creation logic into specialized methods on buffer state classes
- Eliminates duplicate conditional logic for single vs multi-segment sequences
- Simplifies the deserialization path by centralizing reader instantiation
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| IReadBufferState.cs | Adds abstract GetReader method to the interface |
| StreamReadBufferState.cs | Implements GetReader for stream-based buffering |
| PipeReadBufferState.cs | Implements GetReader with single/multi-segment optimization |
| JsonTypeInfoOfT.ReadHelper.cs | Replaces inline reader creation with GetReader call |
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/IReadBufferState.cs
Show resolved
Hide resolved
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
Member
|
@EgorBot -windows_intel --filter "*System.Text.Json.Serialization.Tests.ReadJson<*FromStream" |
eiriktsarpalis
approved these changes
Aug 6, 2025
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/IReadBufferState.cs
Show resolved
Hide resolved
This was referenced Aug 11, 2025
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Removes the construction of a
ReadOnlySequenceandifchecks for creating aUtf8JsonReaderin theStreamcase. Might fix the regressions shown in #118173 (comment).I say might because locally running the microbenchmarks didn't give the most consistent results, but over a bunch of runs the general trend was that this change improved things.