Skip to content

Commit

Permalink
(chocolatey#3231) Don't refresh local package info during upgrade no-ops
Browse files Browse the repository at this point in the history
This optimizes the flow of the upgrade command. First, it uses the
list of locally installed package from the if all is specified check,
instead of throwing that away. Then, it will re-use the list for each
package upgrade attempt, only refreshing the list when a package
upgrade/install is attempted. This speeds up upgrade all runs,
especially when a large (200+) number of packages are installed.
  • Loading branch information
TheCakeIsNaOH committed Jul 2, 2023
1 parent d836138 commit 0ee9377
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,9 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
var projectContext = new ChocolateyNuGetProjectContext(config, _nugetLogger);

var configIgnoreDependencies = config.IgnoreDependencies;
SetPackageNamesIfAllSpecified(config, () => { config.IgnoreDependencies = true; });
var allLocalPackages = SetPackageNamesIfAllSpecified(config, () => { config.IgnoreDependencies = true; }).ToList();
config.IgnoreDependencies = configIgnoreDependencies;
var localPackageListValid = true;

config.CreateBackup();

Expand All @@ -981,7 +982,12 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
// before we start reading it.
config.RevertChanges();

var allLocalPackages = GetInstalledPackages(config).ToList();
if (!localPackageListValid)
{
allLocalPackages = GetInstalledPackages(config).ToList();
localPackageListValid = true;
}

var installedPackage = allLocalPackages.FirstOrDefault(p => p.Name.IsEqualTo(packageName));
var packagesToInstall = new List<IPackageSearchMetadata>();
var packagesToUninstall = new HashSet<PackageResult>();
Expand Down Expand Up @@ -1014,6 +1020,8 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon

string logMessage = @"{0} is not installed. Installing...".FormatWith(packageName);

localPackageListValid = false;

if (config.RegularOutput) this.Log().Warn(ChocolateyLoggers.Important, logMessage);

var packageNames = config.PackageNames;
Expand Down Expand Up @@ -1177,6 +1185,7 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon

if (performAction)
{
localPackageListValid = false;

NugetCommon.GetPackageDependencies(availablePackage.Identity, NuGetFramework.AnyFramework, sourceCacheContext, _nugetLogger, remoteEndpoints, sourcePackageDependencyInfos, sourceDependencyCache, config).GetAwaiter().GetResult();

Expand Down

0 comments on commit 0ee9377

Please sign in to comment.