Skip to content

Commit 49daa16

Browse files
Improve efficiency of IsDependentOn (#150)
1 parent 75fc064 commit 49daa16

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/Common/MSBuildCachePluginBase.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,27 @@ private static Task<int> TimeAndLogAsync(PluginLoggerBase logger, Func<Task> inn
13471347

13481348
public static class ProjectGraphNodeExtensions
13491349
{
1350-
public static bool IsDependentOn(this NodeContext possibleDependent, NodeContext possibleDependency) =>
1351-
possibleDependent.Dependencies.Any(n => n == possibleDependency || n.IsDependentOn(possibleDependency));
1350+
public static bool IsDependentOn(this NodeContext possibleDependent, NodeContext possibleDependency)
1351+
{
1352+
HashSet<NodeContext> visited = new();
1353+
Queue<NodeContext> toCheck = new();
1354+
toCheck.Enqueue(possibleDependent);
1355+
while (toCheck.Count > 0)
1356+
{
1357+
NodeContext current = toCheck.Dequeue();
1358+
if (current == possibleDependency)
1359+
{
1360+
return true;
1361+
}
1362+
foreach (NodeContext dependency in current.Dependencies)
1363+
{
1364+
if (visited.Add(dependency))
1365+
{
1366+
toCheck.Enqueue(dependency);
1367+
}
1368+
}
1369+
}
1370+
1371+
return false;
1372+
}
13521373
}

0 commit comments

Comments
 (0)