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

optimize no-cycle rule using strongly connected components #2998

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

soryy708
Copy link
Contributor

@soryy708 soryy708 commented Apr 8, 2024

Lets make the no-cycle rule faster by not running many unnecessary BFSes.

For each dependency graph (aka ExportMap) we can run Tarjan's SCC once (which is a derivative of DFS = O(n))

That saves us a lot of work because we run a linear-complexity algorithm once, as opposed to for each linted file (which turned us O(n^2))

#2937

@soryy708
Copy link
Contributor Author

soryy708 commented Apr 8, 2024

This fails some tests. I need help with dealing with these edge-cases.

Copy link

socket-security bot commented Apr 8, 2024

Copy link

socket-security bot commented Apr 8, 2024

👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

View full report↗︎

@soryy708
Copy link
Contributor Author

soryy708 commented Apr 8, 2024

Why is socket-security failing this?
The changes in the dependencies it found don't make sense.
The library I added @rtsao/scc has no dependencies at all.
Here's their package.json: https://github.com/rtsao/scc/blob/1120edc92040b0ca748ab62a882a98565c85deed/package.json

@ljharb
Copy link
Member

ljharb commented Apr 8, 2024

Good question. I think it's that this is the first new PR it's been run on since i installed it?

src/rules/no-cycle.js Outdated Show resolved Hide resolved
tests/src/rules/no-cycle.js Outdated Show resolved Hide resolved
Copy link

codecov bot commented Apr 10, 2024

Codecov Report

Attention: Patch coverage is 64.00000% with 18 lines in your changes are missing coverage. Please review.

Project coverage is 85.41%. Comparing base (c0ac54b) to head (ebc2e80).

❗ Current head ebc2e80 differs from pull request most recent head a47b5b9. Consider uploading reports for the commit a47b5b9 to get more accurate results

Files Patch % Lines
src/scc.js 65.11% 15 Missing ⚠️
src/rules/no-cycle.js 57.14% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2998       +/-   ##
===========================================
- Coverage   95.66%   85.41%   -10.26%     
===========================================
  Files          78       79        +1     
  Lines        3275     3325       +50     
  Branches     1150     1159        +9     
===========================================
- Hits         3133     2840      -293     
- Misses        142      485      +343     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/scc.js Outdated Show resolved Hide resolved
src/scc.js Show resolved Hide resolved
tests/src/rules/no-cycle.js Outdated Show resolved Hide resolved
tests/src/rules/no-cycle.js Outdated Show resolved Hide resolved
@@ -62,6 +68,8 @@ module.exports = {
context,
);

const scc = StronglyConnectedComponentsBuilder.get(myPath, context);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this row and line 114-117 move after line 119?
when skipErrorMessagePath is false, there is no need to do scc check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the scc check is how we know if there's cycles or not.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I'll look at failing tests next


export default class StronglyConnectedComponentsBuilder {
static clearCache() {
cache = new Map();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cache = new Map();
cache.clear();

this might be less GC intensive? i don't feel strongly tho

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. clearCache is currently used only by tests, so I think it doesn't matter.

tests/src/rules/no-cycle.js Outdated Show resolved Hide resolved
tests/src/rules/no-cycle.js Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

3 participants