-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix(core): optimize task graph cycle detection and formatting #33737
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
base: master
Are you sure you want to change the base?
fix(core): optimize task graph cycle detection and formatting #33737
Conversation
👷 Deploy request for nx-docs pending review.Visit the deploys page to approve it
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Improve performance of task graph operations from O(n³) to O(n): 1. Task graph cycle detection (task-graph-utils.ts): - Replace Array.includes() with Set.has() for O(1) path lookup - Use mutable Set/Array pair to avoid creating new arrays per recursion - Collect cyclic deps before removal to avoid mutation during iteration - Affects: findCycle(), findCycles(), makeAcyclic() 2. Task formatting (formatting-utils.ts): - Convert projectNames and targets arrays to Sets upfront - Replace O(n) Array.includes() with O(1) Set.has() in forEach loop - Add type annotations to Sets for clarity Performance impact: - Cycle detection: O(n³) → O(n) where n = number of tasks - Formatting: O(n*m) → O(n+m) where n = tasks, m = projects - For 100+ task monorepos: 10-50x improvement in cycle detection
1f5475a to
400d833
Compare
|
View your CI Pipeline Execution ↗ for commit 4f178bc
☁️ Nx Cloud last updated this comment at |
JamesHenry
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nx Cloud has identified a possible root cause for your failed CI:
Our test failure is unrelated to the task graph optimizations in this PR. The e2e-gradle tests are failing because the external foojay service (used by Gradle for Java 21 toolchain provisioning) is returning 503 errors and timeouts. This requires waiting for the external service to recover.
No code changes were suggested for this issue.
If the issue was transient, you can trigger a rerun by pushing an empty commit:
git commit --allow-empty -m "chore: trigger rerun"
git push
🎓 Learn more about Self-Healing CI on nx.dev
JamesHenry
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our team is currently benchmarking this in more detail, early attempts show the results are nuanced. Depending on the task graph and size the changes provide some benefits or actually degrade the experience. This relates to my response on your other PRs, we need to back things up with real world benchmarks in order to be sure about the results.
Pausing until the results are definitive
|
@JamesHenry can I be of assistance in some of the scenario mapping? Happy to open a couple discussions around hopefully more well thought out performance wins. My goal here is to optimize performance/hot paths for large projects without affecting performance for any other scenarios. I know this hasn't quite directly been the case but happy to assist in any way I can. Some of my own testing showed similar results for some of the rust optimizations as well. Let me know what some ideal next steps would be from my perspective to help with infra/testing/benchmarking. |
|
Also interested in being of further assistance here! |

Summary
This PR reduces the complexity of critical task graph operations from O(n³) to O(n), providing 10-50x performance improvements for large monorepos.
Current Behavior
Problem 1: O(n³) Cycle Detection
Current code:
Problem 2: O(n×m) Task Formatting
Expected Behavior
Solution 1: O(n) Cycle Detection with Set
Solution 2: O(n+m) Formatting with Set
Changes
task-graph-utils.tspath: string[]withpathSet: Set<string>task-graph-utils.tstask-graph-utils.tsformatting-utils.tsReal-World Impact
Testing
All 11 task-graph-utils tests pass, verifying:
Related Issue(s)
Contributes to #33366
Merge Dependencies
This PR has no dependencies and can be merged independently.
Must be merged BEFORE: #33745