Skip to content

Commit

Permalink
Stop using anonymous functions in ResolverUtility.FindLibraryByVersio…
Browse files Browse the repository at this point in the history
…nAsync to reduce memory allocations (#4348)
  • Loading branch information
kartheekp-ms authored Nov 24, 2021
1 parent edd8d7a commit 9187a50
Showing 1 changed file with 34 additions and 35 deletions.
69 changes: 34 additions & 35 deletions src/NuGet.Core/NuGet.DependencyResolver.Core/ResolverUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,24 +374,20 @@ public static async Task<RemoteMatch> FindLibraryByVersionAsync(
return await FindLibraryFromSourcesAsync(
libraryRange,
providers,
provider => provider.FindLibraryAsync(
libraryRange,
framework,
cacheContext,
logger,
token));
framework,
cacheContext,
logger,
token);
}

// Try the non http sources first
var nonHttpMatch = await FindLibraryFromSourcesAsync(
libraryRange,
providers.Where(p => !p.IsHttp),
provider => provider.FindLibraryAsync(
libraryRange,
framework,
cacheContext,
logger,
token));
framework,
cacheContext,
logger,
token);

// If we found an exact match then use it
if (nonHttpMatch != null && nonHttpMatch.Library.Version.Equals(libraryRange.VersionRange.MinVersion))
Expand All @@ -403,12 +399,10 @@ public static async Task<RemoteMatch> FindLibraryByVersionAsync(
var httpMatch = await FindLibraryFromSourcesAsync(
libraryRange,
providers.Where(p => p.IsHttp),
provider => provider.FindLibraryAsync(
libraryRange,
framework,
cacheContext,
logger,
token));
framework,
cacheContext,
logger,
token);

// Pick the best match of the 2
if (libraryRange.VersionRange.IsBetter(
Expand All @@ -424,27 +418,16 @@ public static async Task<RemoteMatch> FindLibraryByVersionAsync(
private static async Task<RemoteMatch> FindLibraryFromSourcesAsync(
LibraryRange libraryRange,
IEnumerable<IRemoteDependencyProvider> providers,
Func<IRemoteDependencyProvider, Task<LibraryIdentity>> action)
NuGetFramework framework,
SourceCacheContext cacheContext,
ILogger logger,
CancellationToken token)
{
var tasks = new List<Task<RemoteMatch>>();

foreach (var provider in providers)
{
Func<Task<RemoteMatch>> taskWrapper = async () =>
{
var library = await action(provider);
if (library != null)
{
return new RemoteMatch
{
Provider = provider,
Library = library
};
}

return null;
};

tasks.Add(taskWrapper());
tasks.Add(FindLibraryFromProviderAsync(provider, libraryRange, framework, cacheContext, logger, token));
}

RemoteMatch bestMatch = null;
Expand Down Expand Up @@ -474,6 +457,22 @@ private static async Task<RemoteMatch> FindLibraryFromSourcesAsync(
}
}

static async Task<RemoteMatch> FindLibraryFromProviderAsync(IRemoteDependencyProvider provider, LibraryRange libraryRange,
NuGetFramework framework, SourceCacheContext cacheContext, ILogger logger, CancellationToken token)
{
var library = await provider.FindLibraryAsync(libraryRange, framework, cacheContext, logger, token);
if (library != null)
{
return new RemoteMatch
{
Provider = provider,
Library = library
};
}

return null;
}

return bestMatch;
}

Expand Down

0 comments on commit 9187a50

Please sign in to comment.