Skip to content

Stream packages.lock.json reading with System.Text.Json - #7769

Merged
zivkan merged 1 commit into
NuGet:devfrom
baronfel:baronfel-aot-package-lock-reader
Oct 31, 2026
Merged

zivkan merged 1 commit into
NuGet:devfrom
baronfel:baronfel-aot-package-lock-reader

Conversation

@baronfel

@baronfel baronfel commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Bug

Progress: dotnet/sdk#55501

Description

This migrates packages.lock.json reading to a streaming System.Text.Json implementation while retaining the Newtonsoft.Json writer. The supported Stream, file, and string reader paths accept UTF-8 JSON and use native System.Text.Json token semantics.

The streaming reader assumes the NuGet-authored schema: version must be the first root property, then parsing dispatches to separate V1/V2 and V3 readers. Unknown properties are skipped, while known schema properties with incorrect JSON token types invalidate the lock file through the existing logged fallback path. Dependency collections are materialized in one pass.

The obsolete public TextReader overload remains as its existing legacy Newtonsoft.Json path. Both string-based Parse overloads are obsolete in favor of the Stream API because retaining an entire lock file as a string is expensive.

Local BenchmarkDotNet 0.15.8 MediumRun measurements compare the public Stream reader at the merged #7755 base (9d88850a) with the final strict-schema reader (9f62722c) on .NET 10.0.11. Both runs used .NET SDK 11.0.100-rc.1.26411.101 on the same Windows 11 / AMD Ryzen 7 5700X3D machine. The benchmark generates one-target UTF-8 V3 lock files with 10–10,000 packages; lower time and allocation are better.

Packages JSON size Base mean time PR mean time Speedup Base allocated PR allocated Allocation reduction
10 1.37 KB 17.10 μs 6.427 μs 2.66× 40.91 KB 5.62 KB 86.3%
100 13.41 KB 162.51 μs 59.754 μs 2.72× 336.48 KB 51.74 KB 84.6%
1,000 136.46 KB 1.716 ms 0.471 ms 3.64× 3,295.13 KB 515.81 KB 84.3%
10,000 1.36 MB 46.920 ms 7.099 ms 6.61× 32,973.03 KB 5,326.37 KB 83.8%

The review-driven version-first dispatch and one-pass dependency materialization improved the prior PR implementation by another 4.8–10.1% in mean time and reduced its managed allocation by 22.4–23.4%.

This is the third of six focused System.Text.Json migrations, following #7717 and #7755.

Validation:

  • dotnet test test/NuGet.Core.Tests/NuGet.ProjectModel.Test/NuGet.ProjectModel.Test.csproj -f net10.0 --filter "FullyQualifiedName~PackagesLockFileFormatTests|FullyQualifiedName~Utf8JsonStreamReaderTests" — passed, 123 tests.
  • dotnet test test/NuGet.Core.Tests/NuGet.ProjectModel.Test/NuGet.ProjectModel.Test.csproj -f net472 --filter "FullyQualifiedName~PackagesLockFileFormatTests|FullyQualifiedName~Utf8JsonStreamReaderTests" — passed, 123 tests.
  • NativeAOT publish and execution of NuGet.AotCompatibility.TestApp — passed; the executable printed Passed. and exited with code 0.
  • BenchmarkDotNet MediumRun on the original and final UTF-8 Stream readers — final reader was 2.66–6.61× faster and allocated 83.8–86.3% less managed memory across the measured sizes.
  • Changed-file whitespace verification and git diff --check — passed.

PR Checklist

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

@dotnet-policy-service dotnet-policy-service Bot added the Community PRs created by someone not in the NuGet team label Oct 12, 2026
@baronfel
baronfel force-pushed the baronfel-aot-package-lock-reader branch 2 times, most recently from 58fe573 to 35a4f66 Compare October 12, 2026 23:36
@baronfel
baronfel marked this pull request as ready for review October 13, 2026 14:10
@baronfel
baronfel requested a review from a team as a code owner October 13, 2026 14:10
@Nigusu-Allehu
Nigusu-Allehu self-requested a review October 13, 2026 22:56
@baronfel
baronfel force-pushed the baronfel-aot-package-lock-reader branch 4 times, most recently from 0e259cc to 699d7cf Compare October 16, 2026 16:52

@zivkan zivkan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike the lack of error handling in the old Newtonsoft.Json code. So, this PR is a faithful migration to System.Text.Json, but I'm wondering if we should tighten it up.

Comment on lines +119 to +123
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(log);
#else
if (log == null) throw new ArgumentNullException(nameof(log));
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: if we're going to make any other changes to this PR, let's also use the if syntax all the time, and remove the conditional compilation. There's no benefit to adding more lines of code.

This was also done in at least one other place, so remove them all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the conditional here intentionally. Modern targets use the BCL ArgumentNullException.ThrowIfNull helper, while net472 needs the explicit throw. This follows the requested cross-target API pattern.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad copilot, I didn't want it to respond to you. I saw an opportunity to move to 'cleaner' BCL APIs and explicitly requested that. Happy to remove them, but I'd also consider polyfilling them to get the benefit without the ifdef bloat. Thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find 5 lines to achieve what can be done in a single line more offensive than not using modern .NET conventions. I'm fine with a poly fill, or using the .NET Framework compatible code everywhere. I just think copilot's decision was the wrong one here 😆

@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 28, 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.

Migrate package lock reading to a streaming UTF-8 System.Text.Json parser while retaining the Newtonsoft.Json writer and obsolete TextReader compatibility path.

Document input ownership, obsolete whole-string parsing APIs, validate logger arguments, and avoid property-name and target-splitting allocations with span-based parsing.

Co-authored-by: Copilot App <[email protected]>
@baronfel
baronfel force-pushed the baronfel-aot-package-lock-reader branch from 699d7cf to 9f62722 Compare October 28, 2026 19:18
@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 28, 2026
@baronfel

Copy link
Copy Markdown
Contributor Author

Hey folks - I'm back from vacation and have taken a pass at the feedback here. This resulted in less code overall thanks to the strictness, and improved performance measurements.

Comment on lines +119 to +123
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(log);
#else
if (log == null) throw new ArgumentNullException(nameof(log));
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find 5 lines to achieve what can be done in a single line more offensive than not using modern .NET conventions. I'm fine with a poly fill, or using the .NET Framework compatible code everywhere. I just think copilot's decision was the wrong one here 😆

|| reader.TokenType != JsonTokenType.PropertyName
|| !reader.ValueTextEquals(VersionPropertyUtf8))
{
throw new JsonException();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, it is possible to give JsonException a message? I'm not sure what the exception looks like when the first property isn't version, but a message explaining that it was expecting the version and didn't get one would be useful.

@zivkan
zivkan merged commit aa53916 into NuGet:dev Oct 31, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants