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

Cache Intl instances to improve performance #894

Merged
merged 1 commit into from
Sep 23, 2024

Conversation

zbigg
Copy link
Contributor

@zbigg zbigg commented Jul 26, 2024

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:

[Violation] 'click' handler took 106ms

(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)

  • Fix
  • Refactor

Acceptance

N/A performance issue

Basic checklist

  • Good PR name
  • Shortcut link
  • Changelog entry
  • Just one issue per PR
  • GitHub labels
  • Proper status & reviewers
  • Tests
  • Documentation

@zbigg zbigg changed the title performance: cache Intl instances Cache Intl instances to improve performanace Jul 26, 2024
@zbigg zbigg changed the title Cache Intl instances to improve performanace Cache Intl instances to improve performance Jul 26, 2024
Copy link

github-actions bot commented Jul 26, 2024

Pull Request Test Coverage Report for Build 10990614340

Details

  • 15 of 15 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.06%) to 70.916%

Totals Coverage Status
Change from base Build 10961003110: 0.06%
Covered Lines: 2851
Relevant Lines: 3697

💛 - Coveralls

Copy link

github-actions bot commented Jul 26, 2024

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

@zbigg zbigg requested a review from a team July 26, 2024 11:21
@zbigg zbigg force-pushed the zbigg/cache-intl-instances branch from 2f2d7b3 to af88c72 Compare July 26, 2024 11:22
@zbigg zbigg requested a review from VictorVelarde July 26, 2024 11:38
// 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
Copy link
Contributor Author

@zbigg zbigg Jul 26, 2024

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.

Copy link
Contributor Author

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.

@zbigg zbigg force-pushed the zbigg/cache-intl-instances branch from a127d5f to 31ab222 Compare July 29, 2024 09:31
Copy link
Member

@donmccurdy donmccurdy left a comment

Choose a reason for hiding this comment

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

LGTM!

packages/react-ui/src/hooks/useImperativeIntl.js Outdated Show resolved Hide resolved
packages/react-ui/src/hooks/useImperativeIntl.js Outdated Show resolved Hide resolved
// 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);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let cachedInstance = intlInstanceCache.get(intlConfig);
const cachedInstance = intlInstanceCache.get(intlConfig);

@zbigg zbigg merged commit b510bcc into master Sep 23, 2024
2 checks passed
@zbigg zbigg deleted the zbigg/cache-intl-instances branch September 23, 2024 12:25
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.

2 participants