STJ avoid full document string allocation#5908
Open
eMelgooG wants to merge 3 commits into
Open
Conversation
Both read paths in CosmosSystemTextJsonSerializer materialize the entire document as a System.String before parsing: 1. Binary path: cosmosObject.ToString() builds a UTF-16 JSON string from the UTF-8 bytes produced by IJsonWriter.GetResult() (via Utf8StringHelpers), only for JsonSerializer.Deserialize<T>(string, ...) to re-parse that UTF-16. Fix: feed the UTF-8 ReadOnlyMemory<byte> directly into JsonSerializer.Deserialize<T>(ReadOnlySpan<byte>, ...). 2. DeserializeStream<T> helper: StreamReader.ReadToEnd() transcodes UTF-8 to a full UTF-16 string before Deserialize<T>(string, ...). Fix: call JsonSerializer.Deserialize<T>(Stream, ...) which feeds Utf8JsonReader directly from the UTF-8 byte stream. Both changes are semantically equivalent for Cosmos response bodies (UTF-8 JSON) and eliminate full-document string allocations that land on the Large Object Heap for non-trivial documents. Refs: Azure#4652 (comment) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Author
@microsoft-github-policy-service agree company="Microsoft" |
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.
Refs: #4652 (comment)
Pull Request Template
Description
Both read paths in
CosmosSystemTextJsonSerializercurrently materialize the entire response document as a UTF-16System.Stringbefore parsing. This PR feeds the UTF-8 bytes directly intoUtf8JsonReaderinstead, eliminating the allocation and the redundant UTF-8 ↔ UTF-16 transcoding on every read.Binary path: replace
cosmosObject.ToString()(which produces a UTF-16string) withcosmosObject.WriteTo(textJsonWriter)+JsonSerializer.Deserialize<T>(ReadOnlySpan<byte>, ...).Text path (
DeserializeStream<T>): replaceStreamReader.ReadToEnd()+Deserialize<T>(string, ...)withJsonSerializer.Deserialize<T>(Stream, ...).Both changes are semantically equivalent for Cosmos response bodies (UTF-8 JSON), no public API impact. Reduces CPU, allocations, and GC/LOH pressure on every read.
Type of change
Please delete options that are not relevant.
Closing issues
n/a
Changelog
### Unreleasedinchangelog.mdfor the user-facing impact of this change.