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

fix: drastically reduce time taken to check for cycles #2874

Merged
merged 1 commit into from Jun 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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