Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit d7b0190

Browse files
author
Livar
authored
Merge pull request #4446 from livarcocc/fix_projectmodel_perf
Applying @brthor perf improvement to GetTypeBuildExclusionList.
2 parents a0d5104 + 12d12f9 commit d7b0190

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

src/Microsoft.DotNet.ProjectModel/ProjectModelPlatformExtensions.cs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,65 @@ private static void CollectDependencies(IDictionary<string, LibraryExport> expor
3838

3939
public static HashSet<string> GetTypeBuildExclusionList(this ProjectContext context, IDictionary<string, LibraryExport> exports)
4040
{
41-
var acceptedExports = new HashSet<string>();
41+
var rootProject = context.RootProject;
42+
var buildExports = new HashSet<string>();
43+
var nonBuildExports = new HashSet<string>();
4244

43-
// Accept the root project, obviously :)
44-
acceptedExports.Add(context.RootProject.Identity.Name);
45+
var nonBuildExportsToSearch = new Stack<string>();
46+
var buildExportsToSearch = new Stack<string>();
4547

46-
// Walk all dependencies, tagging exports. But don't walk through Build dependencies.
47-
CollectNonBuildDependencies(exports, context.RootProject.Dependencies, acceptedExports);
48+
LibraryExport export;
49+
string exportName;
4850

49-
// Whatever is left in exports was brought in ONLY by a build dependency
50-
var exclusionList = new HashSet<string>(exports.Keys);
51-
exclusionList.ExceptWith(acceptedExports);
52-
return exclusionList;
53-
}
51+
// Root project is non-build
52+
nonBuildExportsToSearch.Push(rootProject.Identity.Name);
53+
nonBuildExports.Add(rootProject.Identity.Name);
5454

55-
private static void CollectNonBuildDependencies(IDictionary<string, LibraryExport> exports, IEnumerable<LibraryRange> dependencies, HashSet<string> acceptedExports)
56-
{
57-
foreach (var dependency in dependencies)
55+
// Mark down all nonbuild exports and all of their dependencies
56+
// Mark down build exports to come back to them later
57+
while (nonBuildExportsToSearch.Count > 0)
5858
{
59-
var export = exports[dependency.Name];
60-
if (!dependency.Type.Equals(LibraryDependencyType.Build))
59+
exportName = nonBuildExportsToSearch.Pop();
60+
export = exports[exportName];
61+
62+
foreach (var dependency in export.Library.Dependencies)
6163
{
62-
acceptedExports.Add(export.Library.Identity.Name);
63-
CollectNonBuildDependencies(exports, export.Library.Dependencies, acceptedExports);
64+
if (!dependency.Type.Equals(LibraryDependencyType.Build))
65+
{
66+
if (!nonBuildExports.Contains(dependency.Name))
67+
{
68+
nonBuildExportsToSearch.Push(dependency.Name);
69+
nonBuildExports.Add(dependency.Name);
70+
}
71+
}
72+
else
73+
{
74+
buildExportsToSearch.Push(dependency.Name);
75+
}
6476
}
6577
}
66-
}
6778

79+
// Go through exports marked build and their dependencies
80+
// For Exports not marked as non-build, mark them down as build
81+
while (buildExportsToSearch.Count > 0)
82+
{
83+
exportName = buildExportsToSearch.Pop();
84+
export = exports[exportName];
85+
86+
buildExports.Add(exportName);
87+
88+
foreach (var dependency in export.Library.Dependencies)
89+
{
90+
if (!nonBuildExports.Contains(dependency.Name))
91+
{
92+
buildExportsToSearch.Push(dependency.Name);
93+
}
94+
}
95+
}
96+
97+
return buildExports;
98+
}
99+
68100
public static IEnumerable<LibraryExport> FilterExports(this IEnumerable<LibraryExport> exports, HashSet<string> exclusionList)
69101
{
70102
return exports.Where(e => !exclusionList.Contains(e.Library.Identity.Name));

0 commit comments

Comments
 (0)