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 4 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 SelectManyWithParameterHelpers.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 SelectManyWithParameterHelpers.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 SelectManyWithParameterHelpers.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 SelectManyWithParameterHelpers.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 SelectManyWithParameterHelpers.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 SelectManyWithParameterHelpers.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);
}

private 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;
}
karakasa marked this conversation as resolved.
Show resolved Hide resolved
}

private Library? CreateLibrary(TargetLibrary targetLibrary, bool runtime, Dictionary<string, LibraryStub>? libraryStubs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ By default, the dependency manifest contains information about the application's
<ItemGroup>
<Compile Include="$(CoreLibSharedDir)\System\Numerics\Hashing\HashHelpers.cs"
Link="System\Numerics\Hashing\HashHelpers.cs" />
<Compile Include="$(LibrariesProjectRoot)\System.Linq\src\System\Linq\SelectManyWithParameterHelpers.cs"
Link="System\Linq\SelectManyWithParameterHelpers.cs" />
<Compile Include="$(CommonPath)System\ThrowHelper.cs"
Link="Common\System\ThrowHelper.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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;

namespace System.Linq
{
internal static class SelectManyWithParameterHelpers
karakasa marked this conversation as resolved.
Show resolved Hide resolved
{
public static IEnumerable<TResult> SelectMany<TSource, TResult, TParam1>(IEnumerable<TSource> src,
Func<TSource, TParam1, IEnumerable<TResult>> func, TParam1 param1)
{
if (src is TSource[] srcArray)
{
return SelectManyArray(srcArray, func, param1);
}

return SelectManyNonArray(src, func, param1);
}

private static IEnumerable<TResult> SelectManyArray<TSource, TResult, TParam1>(TSource[] src,
Func<TSource, TParam1, IEnumerable<TResult>> func, TParam1 param1)
{
foreach (var elem in src)
{
foreach (var subElem in func(elem, param1))
{
yield return subElem;
}
}
}
private static IEnumerable<TResult> SelectManyNonArray<TSource, TResult, TParam1>(IEnumerable<TSource> src,
Func<TSource, TParam1, IEnumerable<TResult>> func, TParam1 param1)
{
foreach (var elem in src)
{
foreach (var 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)
{
if (src is TSource[] srcArray)
{
return SelectManyArray(srcArray, func, param1, param2);
}

return SelectManyNonArray(src, func, param1, param2);
}

private static IEnumerable<TResult> SelectManyNonArray<TSource, TResult, TParam1, TParam2>(IEnumerable<TSource> src,
Func<TSource, TParam1, TParam2, IEnumerable<TResult>> func, TParam1 param1, TParam2 param2)
{
foreach (var elem in src)
{
foreach (var subElem in func(elem, param1, param2))
{
yield return subElem;
}
}
}
private static IEnumerable<TResult> SelectManyArray<TSource, TResult, TParam1, TParam2>(TSource[] src,
Func<TSource, TParam1, TParam2, IEnumerable<TResult>> func, TParam1 param1, TParam2 param2)
{
foreach (var elem in src)
{
foreach (var subElem in func(elem, param1, param2))
{
yield return subElem;
}
}
}
karakasa marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading