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

Created migration to change ONYX resource personalDetailsList to COLLECTION #30333

Conversation

sebryu
Copy link
Contributor

@sebryu sebryu commented Oct 25, 2023

Created migration to change ONYX resource personalDetailsList to ONYX.COLLECTION personalDetails - optimized BaseUserDetailsTooltip.

I still need to change other personalDetailsList occurrences, but I'm submitting this draft for opening a discussion.

Details

In many benchmarks on High Traffic account a lot of time is generated by processing of huge personalDetailsList resource from Onyx (visible on BaseUserDetailsTooltip component on screen transition / typing / cmd + k).

I've migrated Onyx resource personalDetailsList_ to a collection personalDetails_, which allow us to make small components to subscribe only to resource they need (by using withOnyx key: ({accountID}) => COLLECTION + id).

Below I'm attaching benchmarks for following scenario - on HT account on report screen focused input and pressed cmd+k.
We can see that on avg. it takes:

  • ~150ms for each commit (and ~5ms for each withOnyx(personalDetailsList) component) for main branch
old-norm1
  • ~1000ms for each commit (and ~30ms for each withOnyx(personalDetailsList) component) for main branch on x6 cpu slowdown
old-6x-cpu-slow1
  • ~35ms for each commit (and 0.1ms for each withOnyx(personalDetailsList) component ) for this branch
new-norm1
  • ~200ms for each commit (and 0.1ms for each withOnyx(personalDetailsList) component) for this branch on x6 cpu slowdown
new-6x-cpu-slow1

Results

500% faster for whole commit, and just 5000% faster (or 30000% faster on x6 cpu slowdown) for rendering single BaseUserDetailsTooltip component

Fixed Issues

$ #30318
PROPOSAL:

Tests

  • Verify that no errors appear in the JS console

Offline tests

QA Steps

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
    • MacOS: Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
MacOS: Desktop

Copy link
Contributor

@kidroca kidroca left a comment

Choose a reason for hiding this comment

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

Hey @sebryu, appreciate the update

I have a question regarding the new structure where personal details are stored as a collection. Does this mean that each entry in the personal details will now have its own unique key?

I'm asking because my High Traffic account currently has 4136 entries in the list of personal details. At present, there are approximately 152 unique keys after I log in. With the new changes, this number would increase to 4288 unique keys. Could this potentially impact the performance of the getAllKeys method in Onyx?

@sebryu
Copy link
Contributor Author

sebryu commented Oct 27, 2023

Benchmarking how number of personalDetails keys length correlates to commits/component render duration. (cc: @marcaaron )

Number of keys: 100
commit time ~28ms, component render time ~0.2ms
norm100-28ms-0,2ms

Number of keys: 250
commit time ~38ms, component render time ~0.3ms
norm250-38ms-0 3ms

Number of keys: 500
commit time ~51ms, component render time ~0.6ms
norm-500-51ms-0,6ms

Number of keys: 1000
commit time ~76ms, component render time ~1.2ms
norm1000-76ms-1,2ms

Number of keys: 2000
commit time ~120ms, component render time ~2.5ms
norm2000-120ms-2,5ms

So I would say that from personalDetailsList with keys count between 250-500 problems starts to be visible.

@sebryu
Copy link
Contributor Author

sebryu commented Oct 27, 2023

@kidroca Benchmark on how number of unique keys in Onyx correlates to speed of Onyx.getAllKeys():
Tested with running await Onyx.getAllKeys() between console.time/timeEnd 100x times and taking average:

Number of keys: 100 - 0.02ms
Number of keys: 4000 - 0.13ms
Number of keys: 8000 - 0.26ms
Number of keys: 16000 - 0.4ms

Moreover when clicking through the app with big collection (16k keys) I haven't noticed any performance decrease.

@marcaaron
Copy link
Contributor

Good stuff @sebryu thanks for the benchmarks!

@mountiny I am not too sure how we would go about doing a migration for this if we really wanted to switch over. While there's not so many places in the backend where we need to change this (< 20 or so).

The problem is that there's not really anyway to verify at the moment which clients should get the new format vs. the old. So, while I am interested in this idea, we just don't have a clean way to change it because the API is not versioned, subscribers to the data receive things that are not versioned, there could be "missing updates" in a format that they can't use.

I guess we could send both formats and have newer clients ignore the old and old ones store useless data. But that is not satisfying.

All that leads me to think we need to have some larger conversation about how the API interacts with the App and how a migration like this could work longer term.

For now, one workaround is that we might be able to do a pure front end solution...

We could:

  1. Give the personalDetailsList some special significance to Onyx by flagging it as a "virtual collection" (or something like that)
  2. Intercept it whenever someone calls Onyx.merge() and do not store anything under personalDetailsList key in storage
  3. Whenever a merge is called for this key we can re-write the keys and call mergeCollection() after to be personalDetails_<accountID> or something.
  4. Then we can update those subscribers to use the collection key.

This might come with a downside of being slightly confusing, but would solve the problem without having to crack the migration problem.

@mountiny
Copy link
Contributor

I am sure this is challenging, but I was wondering if we could actually do this the other way around, hence make the App code compatible with both collection version and still the old version of the personal details format. This might come in with some slowdown when we check if the new format is available and if not just use the exisiting code without change.

Currently I need to work on an external commit for the Bottom up flow so cant really explore this much, but I feel like there should be a way @sebryu such that the App code will just continue using the existing array format and once we deploy this leave it out for maybe a week to ensure there are 3-4 versions of the app which have a support for the new format, we can then switch over the API to return the collection format and with migration all should work fine.

However, not sure how feasible this is from App code perspective.

@marcaaron
Copy link
Contributor

It definitely needs an internal champion. Sorry, I have too many things in my backlog already so can't help right now.

@sebryu sebryu force-pushed the Sebryu-performance-personal-details-to-onyx-collection branch from af9f612 to 441d996 Compare November 1, 2023 10:46
@mountiny
Copy link
Contributor

mountiny commented Nov 1, 2023

Same we will try to call out for help in the weekly update

@mountiny
Copy link
Contributor

I think we can close this PR for now

@mountiny mountiny closed this Dec 28, 2024
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.

4 participants