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(gatsby): fix an infinite loop in node child collection #17078

Merged
merged 3 commits into from
Sep 1, 2019

Conversation

me4502
Copy link
Contributor

@me4502 me4502 commented Aug 26, 2019

Description

Currently there are cases where a cyclic node graph can cause findChildrenRecursively to hit the limit of the stack. Also, recursive functions are notoriously slow in JS.

I've replaced this with a non-recursive implementation of a pre-order depth first search tree traversal, and also made it keep track of traversed nodes. This will prevent duplicate nodes being found, and also prevent infinite loops with cyclic node trees.

Performance wise this should theoretically be the same or possibly slightly better - I have not done a performance benchmark. Edit: Just made a change and benchmarked it, this is 3x faster than before.

Correct me if I'm wrong - but renaming this method is fine as it is not exported, and all local usages have been fixed.

@me4502 me4502 requested a review from a team as a code owner August 26, 2019 02:01
@lannonbr lannonbr changed the title fix: fix an infinite loop in node child collection fix(gatsby): fix an infinite loop in node child collection Aug 26, 2019
@ehowey
Copy link
Contributor

ehowey commented Aug 30, 2019

Any chance this relates to what I am experiencing in this issue?

#17131

@me4502
Copy link
Contributor Author

me4502 commented Aug 31, 2019

Very unlikely as this will actually crash node due to exhausting the stack

Copy link
Contributor

@sidharthachatterjee sidharthachatterjee left a comment

Choose a reason for hiding this comment

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

This is a really solid improvement! Thank you so much @me4502 💜

@sidharthachatterjee sidharthachatterjee added the bot: merge on green Gatsbot will merge these PRs automatically when all tests passes label Sep 1, 2019
@gatsbybot gatsbybot merged commit 240b4f8 into gatsbyjs:master Sep 1, 2019
waltercruz pushed a commit to waltercruz/gatsby that referenced this pull request Sep 8, 2019
…17078)

* fix: fix an infinite loop in node child collection

* Flip the traversal for speed gains

* After using the cache a lot, it's possible for getNode to return undefined
@sidharthachatterjee
Copy link
Contributor

Thank you once again for your work here! Featuring it in this month's Gazette in #17548

@me4502
Copy link
Contributor Author

me4502 commented Sep 13, 2019

Nice, thanks :)

@me4502 me4502 deleted the fix/non-recursive-children branch September 13, 2019 04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: merge on green Gatsbot will merge these PRs automatically when all tests passes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants