-
Notifications
You must be signed in to change notification settings - Fork 16
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
Cache Intl instances to improve performance #894
Conversation
Pull Request Test Coverage Report for Build 10990614340Details
💛 - Coveralls |
Visit the preview URL for this PR (updated for commit fffcd45): https://cartodb-fb-storybook-react-dev--pr894-zbigg-cache-intl-v5om38k0.web.app (expires Mon, 30 Sep 2024 08:41:07 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 Sign: 517cc4d31d7e09cf277774e034094b67c301cd4c |
2f2d7b3
to
af88c72
Compare
// this is very naive cache that bases on fact that Intl instance is actually same for most of time | ||
// so we can reuse those maps across several instances of same components | ||
// note, useMemo can't do that globally and flattenMessages over _app_ and c4r is quite costly and would | ||
// be paid for evey c4r component mounted |
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 cache assumes we have 1 active Intl instance in app which is usually the case.
It can be changed to WeakMap<IntlConfig, IntlShape> if we have use case where one app use many languages at once, not sure if we need it.
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.
Changed to WeakMap, so we're on safe side.
a127d5f
to
31ab222
Compare
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.
LGTM!
95e68a8
to
898268f
Compare
// so we can reuse those maps across several instances of same components | ||
// note, useMemo can't cache accross many that globally and flattenMessages over _app_ and c4r messages is quite costly | ||
// and would be paid for every c4r component mounted. | ||
let cachedInstance = intlInstanceCache.get(intlConfig); |
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.
let cachedInstance = intlInstanceCache.get(intlConfig); | |
const cachedInstance = intlInstanceCache.get(intlConfig); |
898268f
to
fffcd45
Compare
Description
Cache intl instances globally.
flattenMessages
over c4r and app could take up to 2-4 milliseconds (on fast machine). And with current,useMemo
approach it was executed for each component mounted .. and lost when unmounted.Real app can mount even 10+ c4r+intl-based components a time, causing some mounts to miss sane deadlines and cause warnings like:
(it's not that this call is only one slow in big components, but this can be easily avoided as
intlConfig
instance is generally stable)We also don't want to waste memory to keep next 10+ copies of all messages (3.5k keys)
Type of change
(choose one and remove the others)
Acceptance
N/A performance issue
Basic checklist