Skip to content

Use System.Text.Json for runtime graph reading - #7717

Merged
zivkan merged 3 commits into
NuGet:devfrom
baronfel:baronfel-aot-runtime-graph-reader
Oct 6, 2026
Merged

zivkan merged 3 commits into
NuGet:devfrom
baronfel:baronfel-aot-runtime-graph-reader

Conversation

@baronfel

@baronfel baronfel commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Bug

Contributes to dotnet/sdk#55501

Description

This PR migrates runtime graph file and stream reading to System.Text.Json so the public runtime graph API works in trimming and NativeAOT scenarios.

The shipped TextReader overload remains available as an obsolete Newtonsoft.Json compatibility path. The new reader parses each runtime graph incrementally and does not buffer the full document. The public Stream overload retains its ownership contract and disposes the supplied stream after parsing.

The System.Text.Json path accepts UTF-8 input with or without a byte order mark. It preserves legacy scalar coercion, comments, trailing commas, and root-section last-value behavior. Malformed nested duplicate properties and null dependency versions are rejected instead of preserving Newtonsoft.Json's permissive behavior.

The parser uses allocation-free comparisons for known runtimes, supports, and #import property names. It collects valid runtime graph entries directly instead of maintaining parallel lists and duplicate-property dictionaries.

A BenchmarkDotNet comparison parsed the .NET SDK 10.0.303 RuntimeIdentifierGraph.json with 100 in-process measurements:

Implementation Mean Allocated
Compatibility implementation 461.5 us 657.95 KB
Simplified implementation 361.7 us 445.23 KB

The simplified parser is 21.6% faster and allocates 32.3% less memory in this benchmark.

A separate follow-up commit changes one existing JsonPackageSpecReader error path to use the path-only FileFormatException overload. This prevents an implicit string-to-JToken conversion from retaining Newtonsoft.Json in the public System.Text.Json package-spec reader closure. Line information is not available for this semantic error because Utf8JsonReader only reports native line data when it throws JsonException.

Focused tests cover the runtime graph reader on .NET and .NET Framework. The NativeAOT test project roots the public API of each referenced NuGet assembly.

This is the first change in a planned series of focused System.Text.Json migrations, split for reviewability.

PR Checklist

  • Meaningful title, helpful description and a linked issue
  • Added tests
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc.

@baronfel
baronfel requested a review from a team as a code owner September 17, 2026 19:43
@dotnet-policy-service dotnet-policy-service Bot added the Community PRs created by someone not in the NuGet team label Sep 17, 2026
Comment thread build/Shared/Utf8JsonStream.cs Resolved
@baronfel
baronfel force-pushed the baronfel-aot-runtime-graph-reader branch 3 times, most recently from 37e814d to dc2f274 Compare September 21, 2026 15:40
Comment thread build/Shared/Utf8JsonStream.cs Resolved
Comment thread build/Shared/Utf8JsonStreamReader.cs Resolved
Comment thread build/Shared/Utf8JsonStreamReader.cs Resolved
Comment thread build/Shared/Utf8JsonStream.cs Resolved
Comment thread build/Shared/Utf8JsonStream.cs Resolved
@nkolev92 nkolev92 added the Merge next release PRs that should not be merged until the dev branch targets the next release label Sep 22, 2026
Read runtime graph files and streams with a buffered UTF-8 reader while retaining the obsolete Newtonsoft-backed TextReader compatibility API. Share allocation-free UTF-8 BOM normalization with explicit ownership, preserve legacy JSON semantics, and retain NativeAOT coverage.

Route every public package spec reader overload through the shared UTF-8 normalization path. Preserve diagnostics and stream ownership without the implicit JToken conversion that rooted Newtonsoft.Json in NativeAOT applications. Use native System.Text.Json positions for parser exceptions and calculate semantic error locations only when needed.

Co-authored-by: Copilot App <[email protected]>
@baronfel
baronfel force-pushed the baronfel-aot-runtime-graph-reader branch from dc2f274 to faca284 Compare September 23, 2026 19:08
Comment thread build/Shared/Utf8JsonStream.cs Resolved
Comment thread build/Shared/Utf8JsonStreamReader.cs Resolved
Comment thread build/Shared/Utf8JsonStreamReader.cs Resolved
Comment thread build/Shared/Utf8JsonStreamReader.cs Resolved
@baronfel
baronfel force-pushed the baronfel-aot-runtime-graph-reader branch from faca284 to a82e2e8 Compare September 24, 2026 14:56
Remove the redundant UTF-8 stream wrapper and unrelated PackageSpec/location work. Consolidate the runtime graph parser, avoid allocations for known property names, and reject malformed nested duplicate and null dependency data.

Benchmarking the full .NET SDK runtime graph shows 21.6% lower parse time and 32.3% fewer allocations.

Co-authored-by: Copilot App <[email protected]>
@baronfel
baronfel force-pushed the baronfel-aot-runtime-graph-reader branch from a82e2e8 to f525b84 Compare September 24, 2026 15:01
Use the path-only FileFormatException overload for invalid top-level package spec versions so the System.Text.Json reader path does not implicitly construct a Newtonsoft.Json JToken.

Co-authored-by: Copilot App <[email protected]>
@baronfel

Copy link
Copy Markdown
Contributor Author

Ok, I've addressed the compatibility, performance, and maintainability notes that the last reviews left! The PR is much simpler now, thankfully.

@dotnet-policy-service dotnet-policy-service Bot added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Oct 2, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 30 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch.

@baronfel

baronfel commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Hi Mr Stale Bot! This PR has the merge in next release label, so we're just waiting on that to open up before merging. So it's not actually stale!

@dotnet-policy-service dotnet-policy-service Bot removed the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Oct 2, 2026
@zivkan
zivkan merged commit 9c751bf into NuGet:dev Oct 6, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community PRs created by someone not in the NuGet team Merge next release PRs that should not be merged until the dev branch targets the next release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants