-
Notifications
You must be signed in to change notification settings - Fork 10.3k
feat(redux): cache truePath lookups for performance #12693
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
feat(redux): cache truePath lookups for performance #12693
Conversation
sidharthachatterjee
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.
Thank you @bennetthardwick
Looks good overall, left a small comment!
packages/gatsby/src/redux/actions.js
Outdated
| ) | ||
| const hasWarnedForPageComponentInvalidContext = new Set() | ||
| const hasWarnedForPageComponentInvalidCasing = new Set() | ||
| const pageComponentCache = new Map() |
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.
Want to replace this with a WeakMap? WeakMaps can be cleaned up by GC so great for caches
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.
This is a great idea! We should also see if this approach improves performance in other areas where we're using Map (where it makes sense to use a WeakMap!).
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.
Unfortunately according to MDN primitive values can't be as keys for weak maps, only object references.
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.
This map won't ever be huge (as this just map template paths), so I don't worry about memory/gc here
DSchau
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.
Quick nit: re a typo, otherwise this looks good.
With your comment on WeakMap only supporting objects, we could just use an object here--or we can just keep the original Map.
I'm not sure there'd be a hugely meaningful difference.
Thanks @DSchau! I can't believe I missed those.
Reading more in to Maps, it seems the difference between them and objects is that the keys are kept in the order they were inserted. Since this isn't needed it's probably unneeded overhead (albeit marginal). I'll update it to use an object instead. 👍 |
|
Holy buckets, @bennetthardwick — we just merged your PR to Gatsby! 💪💜 Gatsby is built by awesome people like you. Let us say “thanks” in two ways:
If there’s anything we can do to help, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’. Thanks again! |
Description
When creating 50k pages, Gatsby spends a lot of time inside the
truePathmethod (which down the stack reads every directory on each of the patch segments). In an application with a small number of components (2 or so), this method doesn't need to be run 50k times.To fix this, a small
Mapwas added to cache these calls, instead of running them every time.Using this patch with gatsby-intense-benchmark, the
createPagesstep of the build goes from about 60 seconds to 20 - 30 seconds.