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

perf(gatsby): Avoid unnecessary type inference during bootstrap #19781

Merged
merged 4 commits into from
Nov 28, 2019

Conversation

vladar
Copy link
Contributor

@vladar vladar commented Nov 25, 2019

Description

This PR should fix performance regression introduced in schema rebuilding PR (#19092). The regression only occurred for sites which created, deleted and then re-created nodes during a bootstrap (a typical example is createPage/deletePage pattern used by some plugins, i.e. gatsby-plugin-intl).

With this pattern, you could have 10 nodes by the end of the bootstrap preceded by 30 createNode/deleteNode/createNode operations. So before this PR incremental type inference would have performed 30 node traversals.

This PR defers initial inference until the first schema build, which means 10 node traversals for the example mentioned above. After the first schema build inference continues incrementally.

Issues with plugins

Schema rebuilding PR also revealed problems in several plugins which mutate data after createNode call. When they do so - mutated fields are never registered in the metadata and never added to GraphQL schema.

At the moment following plugins seem to be affected:

But there may be others. This PR will likely hide those issues back because first inference occurs only on schema build when all mutations are already applied. As a result, those plugins will keep working as they did before 2.18.0. But the issue still exists and must be addressed in every affected plugin. Otherwise, things like future incremental builds simply won't work.

@freiksenet
Copy link
Contributor

Nice!

freiksenet
freiksenet previously approved these changes Nov 27, 2019
@vladar vladar marked this pull request as ready for review November 27, 2019 11:05
@vladar vladar requested a review from a team as a code owner November 27, 2019 11:05
})
if (typeNames.length > 0) {
// Give event-loop a break
setTimeout(processNextType, 0)
Copy link
Contributor

@pieh pieh Nov 27, 2019

Choose a reason for hiding this comment

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

Is there need to do that (give up control)? Why this over regular processNextType call?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This whole process can take seconds (for big sites). And since it is synchronous it will block the event loop and even make CLI "freeze" for that time. So I applied it as a recommended best practice. Do you afraid of some race conditions / conflicts with redux actions in between?

Copy link
Contributor

Choose a reason for hiding this comment

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

That was my assumption that's the case, because this is called during bootstrap only so we are (more or less) in control what's being executed, so I didn't see "functional" reason to do that (and letting cli do the "spinner tick" periodically makes sense)

Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

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

LGTM, feel free to merge. Left one question

@vladar vladar added the bot: merge on green Gatsbot will merge these PRs automatically when all tests passes label Nov 28, 2019
@gatsbybot gatsbybot merged commit 006ecd8 into master Nov 28, 2019
@delete-merged-branch delete-merged-branch bot deleted the vladar/schema-rebuilding-perf branch November 28, 2019 08:21
@vladar
Copy link
Contributor Author

vladar commented Nov 28, 2019

Published in gatsby 2.18.5

@pvdz
Copy link
Contributor

pvdz commented Nov 28, 2019

Seems to work, nice! :D #19512 (comment)

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.

5 participants