Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize capturing lambdas in DependencyContextExtensions and DependencyContextJsonReader #92462

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ public static IEnumerable<string> GetDefaultNativeAssets(this DependencyContext
{
ThrowHelper.ThrowIfNull(self);

return self.RuntimeLibraries.SelectMany(library => library.GetDefaultNativeAssets(self));
return LinqHelpers.SelectMany(self.RuntimeLibraries, static (library, context) => library.GetDefaultNativeAssets(context), self);
}

public static IEnumerable<RuntimeFile> GetDefaultNativeRuntimeFileAssets(this DependencyContext self)
{
ThrowHelper.ThrowIfNull(self);

return self.RuntimeLibraries.SelectMany(library => library.GetDefaultNativeRuntimeFileAssets(self));
return LinqHelpers.SelectMany(self.RuntimeLibraries, static (library, context) => library.GetDefaultNativeRuntimeFileAssets(context), self);
}

public static IEnumerable<string> GetRuntimeNativeAssets(this DependencyContext self, string runtimeIdentifier)
{
ThrowHelper.ThrowIfNull(self);
ThrowHelper.ThrowIfNull(runtimeIdentifier);

return self.RuntimeLibraries.SelectMany(library => library.GetRuntimeNativeAssets(self, runtimeIdentifier));
return LinqHelpers.SelectMany(self.RuntimeLibraries, static (library, context, id) => library.GetRuntimeNativeAssets(context, id), self, runtimeIdentifier);
}

public static IEnumerable<RuntimeFile> GetRuntimeNativeRuntimeFileAssets(this DependencyContext self, string runtimeIdentifier)
{
ThrowHelper.ThrowIfNull(self);
ThrowHelper.ThrowIfNull(runtimeIdentifier);

return self.RuntimeLibraries.SelectMany(library => library.GetRuntimeNativeRuntimeFileAssets(self, runtimeIdentifier));
return LinqHelpers.SelectMany(self.RuntimeLibraries, static (library, context, id) => library.GetRuntimeNativeRuntimeFileAssets(context, id), self, runtimeIdentifier);
}

public static IEnumerable<string> GetDefaultNativeAssets(this RuntimeLibrary self, DependencyContext context)
Expand Down Expand Up @@ -79,15 +79,15 @@ public static IEnumerable<AssemblyName> GetDefaultAssemblyNames(this DependencyC
{
ThrowHelper.ThrowIfNull(self);

return self.RuntimeLibraries.SelectMany(library => library.GetDefaultAssemblyNames(self));
return LinqHelpers.SelectMany(self.RuntimeLibraries, static (library, context) => library.GetDefaultAssemblyNames(context), self);
}

public static IEnumerable<AssemblyName> GetRuntimeAssemblyNames(this DependencyContext self, string runtimeIdentifier)
{
ThrowHelper.ThrowIfNull(self);
ThrowHelper.ThrowIfNull(runtimeIdentifier);

return self.RuntimeLibraries.SelectMany(library => library.GetRuntimeAssemblyNames(self, runtimeIdentifier));
return LinqHelpers.SelectMany(self.RuntimeLibraries, static (library, context, id) => library.GetRuntimeAssemblyNames(context, id), self, runtimeIdentifier);
}

public static IEnumerable<AssemblyName> GetDefaultAssemblyNames(this RuntimeLibrary self, DependencyContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,9 +741,18 @@ private IEnumerable<Library> CreateLibraries(IEnumerable<TargetLibrary>? librari
{
return Enumerable.Empty<Library>();
}
return libraries
.Select(property => CreateLibrary(property, runtime, libraryStubs))
.Where(library => library != null)!;

return CreateLibrariesNotNull(libraries, runtime, libraryStubs);

IEnumerable<Library> CreateLibrariesNotNull(IEnumerable<TargetLibrary> libraries, bool runtime, Dictionary<string, LibraryStub>? libraryStubs)
{
foreach (TargetLibrary library in libraries)
{
Library? createdLibrary = CreateLibrary(library, runtime, libraryStubs);
if (createdLibrary is not null)
yield return createdLibrary;
}
}
}

private Library? CreateLibrary(TargetLibrary targetLibrary, bool runtime, Dictionary<string, LibraryStub>? libraryStubs)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Extensions.DependencyModel
{
internal static class LinqHelpers
{
public static IEnumerable<TResult> SelectMany<TSource, TResult, TParam1>(IEnumerable<TSource> src,
Func<TSource, TParam1, IEnumerable<TResult>> func, TParam1 param1)
{
foreach (TSource? elem in src)
{
foreach (TResult? subElem in func(elem, param1))
{
yield return subElem;
}
}
}

public static IEnumerable<TResult> SelectMany<TSource, TResult, TParam1, TParam2>(IEnumerable<TSource> src,
Func<TSource, TParam1, TParam2, IEnumerable<TResult>> func, TParam1 param1, TParam2 param2)
{
foreach (TSource? elem in src)
{
foreach (TResult? subElem in func(elem, param1, param2))
{
yield return subElem;
}
}
}
}
}
Loading