Skip to content

fix(node): reduce deepReadDirSync runtime complexity #7910

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

Merged
merged 1 commit into from
Apr 19, 2023

Conversation

JonasBa
Copy link
Member

@JonasBa JonasBa commented Apr 19, 2023

I'm experimenting with some eslint rules to detect runtime performance issues and part of my testing was to run it on sentry-javascript and sentry codebase. There are currently only two rules, one for detecting On^2 in reduce callbacks and a rule for detecting unnecessary array spread operators (which the codebase doesn't seem to use much of anyways).

The reduce rule found one instance where the reducer runtime complexity is exponential (due to the accumulator being spread on each iteration). The fix is to treat the accumulator as mutable and avoid shallow copies inside each iteration.

(gh is not very helpful with reviewer suggestions so tagging @lforst and @AbhiPrasad, but feel free to reassign)

@JonasBa JonasBa requested review from lforst and AbhiPrasad April 19, 2023 15:58
@@ -27,10 +27,11 @@ export function deepReadDirSync(targetDir: string): string[] {
const itemAbsPath = path.join(currentDirAbsPath, itemName);

if (fs.statSync(itemAbsPath).isDirectory()) {
return [...absPaths, ...deepReadCurrentDir(itemAbsPath)];
return absPaths.concat(deepReadCurrentDir(itemAbsPath));
Copy link
Member Author

Choose a reason for hiding this comment

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

Depending on how we transpile this, it might already be transpiled to something like [].concat(abspaths, [deppread]), doesn't hurt to omit the empty array though

Copy link
Member

Choose a reason for hiding this comment

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

We transpile to es6, so the spread operator is kept.

Copy link
Member

@mydea mydea left a comment

Choose a reason for hiding this comment

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

LGTM! 🚀

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

I actually did a quick grep and saw that we export this method from the SDK, but nothing actually uses it anymore 😭 (other than tests).

Change makes sense to me though!

@@ -27,10 +27,11 @@ export function deepReadDirSync(targetDir: string): string[] {
const itemAbsPath = path.join(currentDirAbsPath, itemName);

if (fs.statSync(itemAbsPath).isDirectory()) {
return [...absPaths, ...deepReadCurrentDir(itemAbsPath)];
return absPaths.concat(deepReadCurrentDir(itemAbsPath));
Copy link
Member

Choose a reason for hiding this comment

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

We transpile to es6, so the spread operator is kept.

@JonasBa
Copy link
Member Author

JonasBa commented Apr 19, 2023

Ah dang @AbhiPrasad, maybe something for v8 of SDK to remove then.

@JonasBa JonasBa merged commit 7896c68 into develop Apr 19, 2023
@JonasBa JonasBa deleted the jb/fix/node-utils-runtime branch April 19, 2023 17:24
@jeengbe
Copy link
Contributor

jeengbe commented Apr 20, 2023

@JonasBa which rules are you talking about? I've never heard of eslint doing performance stuff

@JonasBa
Copy link
Member Author

JonasBa commented Apr 20, 2023

@jeengbe yeah, it seems like nobody uses it for that, but I started to experiment with a couple of my own custom rules here, not sure it will lead anywhere, but I'm trying to see if it can find some promising stuff. The reduce rule also found a On^2 run in our sentry dashboard

@jeengbe
Copy link
Contributor

jeengbe commented Apr 20, 2023

@jeengbe yeah, it seems like nobody uses it for that, but I started to experiment with a couple of my own custom rules here, not sure it will lead anywhere, but I'm trying to see if it can find some promising stuff. The reduce rule also found a On^2 run in our sentry dashboard

Thanks for sharing! Definitely an interesting idea worth exploring.

@AbhiPrasad
Copy link
Member

Sounds like we need a blog post @JonasBa 😄

@jeengbe
Copy link
Contributor

jeengbe commented Apr 20, 2023

Sounds like we need a blog post @JonasBa 😄

Maybe let the project ripe a little bit more 🙃 But I agree, would surely be interesting if it turns out as promising as expected

@JonasBa
Copy link
Member Author

JonasBa commented Apr 24, 2023

@jeengbe @AbhiPrasad I guess this would fall into a similar set of rules as https://eslint.org/docs/latest/rules/no-await-in-loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants