Skip to content

Commit

Permalink
Speed up package search (microsoft#3884)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmelBawa-msft authored Sep 24, 2024
1 parent 2df7847 commit 19b988c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using DevHome.Services.WindowsPackageManager.Contracts;
Expand Down Expand Up @@ -81,6 +82,30 @@ public WinGetPackageUri GetUri(string installVersion = null)
return new(CatalogName, Id, uriOptions);
}

/// <summary>
/// Copies the packages from out-of-proc to in-proc.
/// </summary>
/// <param name="logger">Logger</param>
/// <param name="packageFinder">Package finder</param>
/// <param name="packagesOutOfProc">Packages to copy</param>
/// <returns>List of in-proc packages</returns>
public static List<IWinGetPackage> FromOutOfProc(
ILogger logger,
IWinGetPackageFinder packageFinder,
IList<CatalogPackage> packagesOutOfProc)
{
// Copy packages from out-of-process in parallel to enhance the performance
var perfTimer = Stopwatch.StartNew();
logger.LogDebug($"Copying {packagesOutOfProc.Count} packages from out-of-proc to in-proc");
var packagesInProc = packagesOutOfProc
.AsParallel()
.AsOrdered()
.Select(p => new WinGetPackage(logger, p, packageFinder.IsElevationRequired(p)))
.ToList<IWinGetPackage>();
logger.LogDebug($"Finished copying {packagesOutOfProc.Count} packages from out-of-proc to in-proc in: {perfTimer.ElapsedMilliseconds} ms");
return packagesInProc;
}

private PackageVersionInfo GetPackageVersionInfo(CatalogPackage package)
{
// Pinned packages do not have a default install version set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ public async Task<IList<IWinGetPackage>> GetPackagesAsync(IList<WinGetPackageUri
// Get packages from the catalog
var catalog = await _protocolParser.ResolveCatalogAsync(firstParsedUri);
var packagesOutOfProc = await _packageFinder.GetPackagesAsync(catalog, packageIds);
var packagesInProc = packagesOutOfProc
.Select(p => new WinGetPackage(_logger, p, _packageFinder.IsElevationRequired(p)))
.ToList<IWinGetPackage>();
packagesInProc
.ForEach(p => _packageCache.TryAddPackage(_protocolParser.CreatePackageUri(p), p));
var packagesInProc = WinGetPackage.FromOutOfProc(_logger, _packageFinder, packagesOutOfProc);
packagesInProc.ForEach(p => _packageCache.TryAddPackage(_protocolParser.CreatePackageUri(p), p));
return packagesInProc;
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public async Task<IList<IWinGetPackage>> SearchAsync(string query, uint limit)
{
var searchCatalog = await _catalogConnector.GetCustomSearchCatalogAsync();
var results = await _packageFinder.SearchAsync(searchCatalog, query, limit);
return results
.Select(p => new WinGetPackage(_logger, p, _packageFinder.IsElevationRequired(p)))
.ToList<IWinGetPackage>();
return WinGetPackage.FromOutOfProc(_logger, _packageFinder, results);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using DevHome.Services.WindowsPackageManager.COM;
Expand Down Expand Up @@ -154,7 +155,13 @@ private async Task<IList<CatalogPackage>> GetPackagesMultiQueriesAsync(WinGetCat
private async Task<IList<CatalogPackage>> GetPackagesInternalAsync(WinGetCatalog catalog, FindPackagesOptions options)
{
_logger.LogInformation($"Performing search on catalog {catalog.GetDescriptiveName()}");
_logger.LogDebug($"Calling WinGet COM API {nameof(PackageCatalog.FindPackagesAsync)}");

// Find packages using WinGet COM API
var perfTimer = Stopwatch.StartNew();
var findResult = await catalog.Catalog.FindPackagesAsync(options);
perfTimer.Stop();

if (findResult.Status != FindPackagesResultStatus.Ok)
{
_logger.LogError($"Failed to find packages with status {findResult.Status}");
Expand All @@ -170,7 +177,7 @@ private async Task<IList<CatalogPackage>> GetPackagesInternalAsync(WinGetCatalog
}

_logger.LogInformation($"Found {result.Count} results from catalog {catalog.GetDescriptiveName()} [{string.Join(", ", result.Select(p => p.Id))}]");

_logger.LogDebug($"Finished calling WinGet COM API {nameof(PackageCatalog.FindPackagesAsync)} with {result.Count} results in {perfTimer.ElapsedMilliseconds} ms");
return result;
}
}

0 comments on commit 19b988c

Please sign in to comment.