Skip to content

Commit

Permalink
fix: drastically reduce time taken to check for cycles (#2874)
Browse files Browse the repository at this point in the history
  • Loading branch information
rix0rrr committed Jun 14, 2022
1 parent f2ece94 commit 4b81a98
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions core/package-graph/index.js
Expand Up @@ -218,6 +218,9 @@ class PackageGraph extends Map {
/** @type {(PackageGraphNode | CyclicPackageGraphNode)[]} */
const walkStack = [];

/** @type {Set<PackageGraphNode>} */
const alreadyVisited = new Set();

function visits(baseNode, dependentNode) {
if (nodeToCycle.has(baseNode)) {
return;
Expand All @@ -228,6 +231,12 @@ class PackageGraph extends Map {
topLevelDependent = nodeToCycle.get(topLevelDependent);
}

// Otherwise the same node is checked multiple times which is very wasteful in a large repository
if (alreadyVisited.has(topLevelDependent)) {
return;
}
alreadyVisited.add(topLevelDependent);

if (
topLevelDependent === baseNode ||
(topLevelDependent.isCycle && topLevelDependent.has(baseNode.name))
Expand Down

0 comments on commit 4b81a98

Please sign in to comment.