Repository navigation
Stream packages.lock.json reading with System.Text.Json - #7769
Conversation
58fe573 to
35a4f66
Compare
0e259cc to
699d7cf
Compare
zivkan
left a comment
There was a problem hiding this comment.
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.
| #if NET6_0_OR_GREATER | ||
| ArgumentNullException.ThrowIfNull(log); | ||
| #else | ||
| if (log == null) throw new ArgumentNullException(nameof(log)); | ||
| #endif |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 😆
|
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]>
699d7cf to
9f62722
Compare
|
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. |
| #if NET6_0_OR_GREATER | ||
| ArgumentNullException.ThrowIfNull(log); | ||
| #else | ||
| if (log == null) throw new ArgumentNullException(nameof(log)); | ||
| #endif |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
Bug
Progress: dotnet/sdk#55501
Description
This migrates
packages.lock.jsonreading 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:
versionmust 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
TextReaderoverload remains as its existing legacy Newtonsoft.Json path. Both string-basedParseoverloads are obsolete in favor of the Stream API because retaining an entire lock file as a string is expensive.Local BenchmarkDotNet 0.15.8
MediumRunmeasurements 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.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.NuGet.AotCompatibility.TestApp— passed; the executable printedPassed.and exited with code 0.MediumRunon 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.git diff --check— passed.PR Checklist