Skip to content

Look packages up concurrently, and parse NuGet responses with System.Text.Json - #709

Merged
SimonCropp merged 2 commits into
mainfrom
concurrent-lookups
Nov 23, 2026
Merged

SimonCropp merged 2 commits into
mainfrom
concurrent-lookups

Conversation

@SimonCropp

Copy link
Copy Markdown
Owner

A run spent nearly all its time waiting on network latency, since each package's lookups were made one after another. This makes the lookups concurrent, and has the tool parse NuGet responses with System.Text.Json.

For a 40 package Directory.Packages.props, from an empty NuGet http cache:

main this branch
Time 18-41s 2.3-2.4s
Peak working set ~92MB ~83MB
Peak private ~45MB ~38MB

The same 38 updates and 2 migrations are made. The props file written is byte for byte the same, and the change and warning lines are logged in the same order.

Look packages up concurrently

  • Prefetch starts the lookups for the central packages, the VersionOverrides across every csproj, and the tools, up to 16 at a time (Prefetch.MaxConcurrentLookups), ahead of the loops that apply them
  • LookupCache and RepositoryReader keep each lookup as a task, shared via SharedLookup.GetOrStart, so a loop awaits the lookup already in progress rather than making its own. A failed lookup is not kept, so a later call tries again, as before
  • The loops are unchanged and still go through the items in order, so edits, migration checks, logged changes, the UpdateResult and progress callbacks are as before
  • A prefetch only looks up. A failure in it is swallowed, for the loop to meet in order
  • Disposing a Prefetch (await using) stops it starting lookups and waits for those started, so none outlive the SourceCacheContext

Parse NuGet responses with System.Text.Json in the tool

PackageUpdate.csproj turns on NuGet.Protocol's NuGet.UseSystemTextJsonDeserialization switch via a RuntimeHostConfigurationOption, so it is in the tool's runtimeconfig.json (checked in the packed nupkg). NuGet.Protocol then parses the service index and registration metadata with System.Text.Json, allocating about 39MB rather than 152MB for the 40 packages.

It applies only to the tool. PackageUpdateLib does not set it, so Lode stays on NuGet's default unless it opts in. Newtonsoft.Json is still a dependency, via NuGet.Packaging.

For review

  • Loggers passed to the library must now be thread-safe, since lookups, and NuGet.Protocol's own logging, run concurrently. Microsoft.Extensions.Logging and Serilog loggers are. The test RecordingLogger now takes a lock
  • NuGet's own GET/OK lines, and "Skipped ... minPublishAgeHours", are logged as each lookup runs, so they can come before the loop reaches that package
  • 16 concurrent lookups is a choice, not tuned. Solutions are still processed one after another
  • The switch is opt-in in NuGet.Protocol, and has only been checked against nuget.org. A private feed with JSON that Newtonsoft.Json tolerates but System.Text.Json does not would behave differently
  • Tests run on NuGet's default deserialization. All 150 pass both on it and with NUGET_USE_SYSTEM_TEXT_JSON_DESERIALIZATION=true
  • The SkipsMigrationWhenAlternativeAlreadyExists snapshot is updated for Azure.Storage.Common 12.29.1, published 2026-11-20. It fails on main too

Each package's lookups were made one after another, so a run was mostly
waiting on network latency. A 40 package Directory.Packages.props took
18-41s from an empty http cache. It now takes 2-3s, with the same changes,
in the same order.

- Prefetch starts the lookups for the central packages, the VersionOverrides
  across every csproj, and the tools, up to 16 at a time, ahead of the loops
  that apply them
- LookupCache and RepositoryReader keep each lookup as a task, shared via
  SharedLookup.GetOrStart, so the loops await the lookup already in progress.
  A failed lookup is not kept, so a later call tries again, as before
- The loops are unchanged and still go through the items in order, so
  edits, migrations, logged changes, the result and progress are as before
- Disposing a Prefetch stops it starting lookups and waits for those
  started, so none outlive the SourceCacheContext
- Loggers passed in must now be thread-safe. RecordingLogger takes a lock

Also updates the SkipsMigrationWhenAlternativeAlreadyExists snapshot, since
Azure.Storage.Common 12.29.1 has been published.
Turns on NuGet.Protocol's NuGet.UseSystemTextJsonDeserialization switch,
via a RuntimeHostConfigurationOption so it is in the tool's
runtimeconfig.json. NuGet.Protocol then parses the service index and
registration metadata with System.Text.Json rather than Newtonsoft.Json.

For a 40 package Directory.Packages.props, from an empty http cache:
- Allocates about 39MB rather than 152MB
- Peaks at about 83MB working set rather than 92MB, and 38MB private
  rather than 45MB
- Runs about 0.1-0.4s faster, of about 2.5s

The same changes are written. It applies only to the tool: PackageUpdateLib
does not set it, so a caller such as Lode stays on NuGet's default unless it
opts in. Newtonsoft.Json is still a dependency, via NuGet.Packaging.
@SimonCropp SimonCropp added this to the 5.0.3 milestone Nov 23, 2026
@SimonCropp
SimonCropp merged commit 09a751b into main Nov 23, 2026
5 checks passed
@SimonCropp
SimonCropp deleted the concurrent-lookups branch November 23, 2026 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant