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 5 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
@@ -1,4 +1,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00

karakasa marked this conversation as resolved.
Show resolved Hide resolved
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34112.27
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{E5B6A94F-615E-4DAF-8110-E5A776BB8296}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bcl.AsyncInterfaces", "..\Microsoft.Bcl.AsyncInterfaces\ref\Microsoft.Bcl.AsyncInterfaces.csproj", "{696D69C9-C5F6-405E-A8B5-5375D08BBADC}"
Expand Down Expand Up @@ -59,11 +63,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{5725D7DF-DC3
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "gen", "{55D04C80-4A8F-40AC-967D-3FA77C814D7B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "tools\gen", "{9C13782D-F7D7-4F02-A39D-D7BD992EB257}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "gen", "{9C13782D-F7D7-4F02-A39D-D7BD992EB257}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "tools\src", "{D9691871-5357-4B81-9B4E-A94394515042}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{D9691871-5357-4B81-9B4E-A94394515042}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "tools\ref", "{C5220846-E0E3-4326-B2C1-C3535910E354}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{C5220846-E0E3-4326-B2C1-C3535910E354}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{FDECE2E5-8E54-4DF7-95D4-35BB8D8432AA}"
EndProject
Expand Down Expand Up @@ -183,36 +187,40 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E5B6A94F-615E-4DAF-8110-E5A776BB8296} = {155C3F40-F63D-49DF-87D3-A3EEA27036E8}
{F5E89C81-2D78-4F86-ABD2-7D983F46EB58} = {155C3F40-F63D-49DF-87D3-A3EEA27036E8}
{5351C410-530D-4724-A8E6-430831E7332B} = {155C3F40-F63D-49DF-87D3-A3EEA27036E8}
{696D69C9-C5F6-405E-A8B5-5375D08BBADC} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{2AED2951-7724-4EFC-8E16-6DF877C6B4A6} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{6D9C22DB-C4E3-483E-AF78-C1DCE6ED8DD6} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{5C580568-6072-4F27-B5C6-FA04556E3B98} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{F5E89C81-2D78-4F86-ABD2-7D983F46EB58} = {155C3F40-F63D-49DF-87D3-A3EEA27036E8}
{5351C410-530D-4724-A8E6-430831E7332B} = {155C3F40-F63D-49DF-87D3-A3EEA27036E8}
{C8C58A2A-B254-4A74-8C7B-9B0CA0A9E67A} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{CC0E8CC8-88F5-4D72-9B3F-0281DD58461A} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{7323E18C-BF40-4236-9AEF-273A94F3DB03} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{536A305D-DC57-4E7F-B21B-B8DE916A63C3} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{50F89560-1449-44E4-844E-72815256534B} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{8E212A9D-391B-4EFA-943D-7D104A9D3D7E} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{2AED2951-7724-4EFC-8E16-6DF877C6B4A6} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{5C580568-6072-4F27-B5C6-FA04556E3B98} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{7902A0CA-E94D-4C96-A112-455A1E5E2390} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{FA7201FE-097D-4197-BDEC-329986814D8D} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{FA796FCD-5901-407B-BF40-166169718A0F} = {55D04C80-4A8F-40AC-967D-3FA77C814D7B}
{7B4B86A8-EF62-45F0-A431-05F4C11A2E0D} = {55D04C80-4A8F-40AC-967D-3FA77C814D7B}
{6F992337-EAEE-4F7A-BF3B-DDC526F054F7} = {55D04C80-4A8F-40AC-967D-3FA77C814D7B}
{536A305D-DC57-4E7F-B21B-B8DE916A63C3} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{50F89560-1449-44E4-844E-72815256534B} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{7902A0CA-E94D-4C96-A112-455A1E5E2390} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{240E7B1C-6D7D-4932-9598-815C2EA420AF} = {55D04C80-4A8F-40AC-967D-3FA77C814D7B}
{B4C5A0BE-3E21-4464-875C-E6EBED176EE3} = {55D04C80-4A8F-40AC-967D-3FA77C814D7B}
{30DEAC25-AFDB-4E91-90A4-7B94E1809C59} = {55D04C80-4A8F-40AC-967D-3FA77C814D7B}
{8E212A9D-391B-4EFA-943D-7D104A9D3D7E} = {50BA55F5-BD05-4C05-910F-2BFD20BD3465}
{FA7201FE-097D-4197-BDEC-329986814D8D} = {5725D7DF-DC33-47D2-90C9-D8736C579E77}
{69691ED6-ECDB-4AFB-87FA-BED4F8489746} = {9C13782D-F7D7-4F02-A39D-D7BD992EB257}
{0CA9C0FD-7D04-4EB8-9567-06171C795552} = {9C13782D-F7D7-4F02-A39D-D7BD992EB257}
{9C13782D-F7D7-4F02-A39D-D7BD992EB257} = {FDECE2E5-8E54-4DF7-95D4-35BB8D8432AA}
{B9F77365-105E-4FED-B0CE-83568C16B46C} = {D9691871-5357-4B81-9B4E-A94394515042}
{718E999B-18AE-4A65-A010-BFF0B8E3FB39} = {D9691871-5357-4B81-9B4E-A94394515042}
{D9691871-5357-4B81-9B4E-A94394515042} = {FDECE2E5-8E54-4DF7-95D4-35BB8D8432AA}
{417E717C-B67C-4CCF-BD8B-5662016DC17E} = {C5220846-E0E3-4326-B2C1-C3535910E354}
{9C13782D-F7D7-4F02-A39D-D7BD992EB257} = {FDECE2E5-8E54-4DF7-95D4-35BB8D8432AA}
{D9691871-5357-4B81-9B4E-A94394515042} = {FDECE2E5-8E54-4DF7-95D4-35BB8D8432AA}
{C5220846-E0E3-4326-B2C1-C3535910E354} = {FDECE2E5-8E54-4DF7-95D4-35BB8D8432AA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {810D114A-26F4-4151-9EFE-29A7F1BF62AA}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\..\tools\illink\src\ILLink.Shared\ILLink.Shared.projitems*{0ca9c0fd-7d04-4eb8-9567-06171c795552}*SharedItemsImports = 5
..\..\tools\illink\src\ILLink.Shared\ILLink.Shared.projitems*{718e999b-18ae-4a65-a010-bff0b8e3fb39}*SharedItemsImports = 5
EndGlobalSection
EndGlobal
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);
}

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
@@ -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;
}
}
}
}
}