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] OnyxUtils keyChanged cached collection retrieval optimisation #577

Merged
merged 2 commits into from
Aug 7, 2024

Conversation

kacper-mikolajczak
Copy link
Contributor

@kacper-mikolajczak kacper-mikolajczak commented Aug 4, 2024

Details

PR introduces an improvement to how getCachedCollection is called in keyChanged.

After investigating this trace, we've noticed that when looping through subscribers list in keyChanged, the getCachedCollection is called for every iteration with the same key.

To do:

I could not reproduce a case from the trace where getCachedCollection is called on each iteration to compare the performance gains. By comparing PR perf to main there was no regression seen and from static analysis we should help when the aforementioned edge case happens.

Related Issues

Expensify/App#46863

Automated Tests

Manual Tests

Author Checklist

  • I linked the correct issue in the ### Related 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 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 / Chrome
    • iOS / native
    • iOS / 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 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 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 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

@kacper-mikolajczak kacper-mikolajczak requested a review from a team as a code owner August 4, 2024 20:21
Copy link
Contributor

github-actions bot commented Aug 4, 2024

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@melvin-bot melvin-bot bot requested review from MariaHCD and removed request for a team August 4, 2024 20:21
@kacper-mikolajczak kacper-mikolajczak changed the title [PERF] OnyxUtils keyChanged [PERF] OnyxUtils keyChanged cached collection retrieval optimisation Aug 5, 2024
Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

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

Comjng from Slack, this seems quite straightforward. Can you create App issue for this with details and link it? Also update the cla and checklist, thank you!

@hannojg @blazejkustra do you want to give it a quick review too

Copy link
Contributor

@blazejkustra blazejkustra left a comment

Choose a reason for hiding this comment

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

TS-wise looks good to me, I'm not an Onyx expert but it seems like a good improvement 👍

@@ -804,6 +804,9 @@ function keyChanged<TKey extends OnyxKey>(
return;
}
}

const cachedCollections: Record<string, ReturnType<typeof getCachedCollection>> = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

cachedCollections stores all collections all in one object? Let's verify that we have enough memory for that 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha :D

On serious note, it won't store all the collections, only those from subscriber.key. Also the reference is hold for the keyChange lifespan and the object size is the same as we've got from getCachedCollection.

Furthermore, it will hold a reference to only one of such collection object, whereas multiple calls to getCachedCollection spawn many of those.

Let me know if my understanding is correct here, thanks!

@hannojg
Copy link
Contributor

hannojg commented Aug 6, 2024

Changes look good to me as well. Just wanted to check with y'all if you think we can solve this more holistically - discussion here

@kacper-mikolajczak
Copy link
Contributor Author

kacper-mikolajczak commented Aug 6, 2024

Can you create App issue for this with details and link it? Also update the cla and checklist, thank you!

@mountiny Done ✅ !

@kacper-mikolajczak
Copy link
Contributor Author

Just wanted to check with y'all if you think we can solve this more holistically - discussion here

Thanks for your input @hannojg ❤️

I think it's definitely worth looking into how the cache retrieval process works!

For now, let's go ahead with the current improvement. It should still have a positive impact, even when we consider the changes you've suggested. What do you think?

@kacper-mikolajczak
Copy link
Contributor Author

Continuation of Slack thread where Hanno wrote:

I think one solution could be to have an additional map directly in the OnyxCache that stores for each collection key the member keys that are in cache. This way we could easily retrieve a collection from cache.

Option A where we would just store the keys of the collection that we have cached

// OnyxCache.ts (pseudo code)
const collectionKeyMap = new Map<string, string>();

set(key: OnyxKey, value: OnyxValue<OnyxKey>): OnyxValue<OnyxKey> {

  if (isCollectionKey(key) {
    collectionKeyMap[key] = splitCollectionMemberKey(key)[1]
  }
}

getCachedCollection(key: CollectionKey) {
  const result = {}
  Object.values(collectionKeyMap[key]).forEach(...)
  return result
}

And maybe there could be an option B, where we have a collectionCacheMap that stores the collections directly :thinking_face:

in regards to the PR the Problem with solution A would be that we might still for the same key (lets say reports_) create the collection twice, so Option B might be better :thinking_face:

Options B seems more practical but I am not sure of what would be the memory impact of storing such collections once again just to serve as a middleman between in-memory cache and end-user. Let's think of how we can improve the idea even further.

Having that said @mountiny, what are the next steps if there is a positive response from reviewers?

@hannojg
Copy link
Contributor

hannojg commented Aug 7, 2024

For now, let's go ahead with the current improvement. It should still have a positive impact, even when we consider the changes you've suggested. What do you think?

Absolutely, lets do that 😊

@hannojg
Copy link
Contributor

hannojg commented Aug 7, 2024

Options B seems more practical but I am not sure of what would be the memory impact of storing such collections once again just to serve as a middleman between in-memory cache and end-user. Let's think of how we can improve the idea even further.

Maybe, instead of having an extra copy just for collections, we could store collections and regular keys separately in the cache (so the data is only there once)

@hurali97
Copy link
Contributor

hurali97 commented Aug 7, 2024

Options B seems more practical but I am not sure of what would be the memory impact of storing such collections once again just to serve as a middleman between in-memory cache and end-user. Let's think of how we can improve the idea even further.

Maybe, instead of having an extra copy just for collections, we could store collections and regular keys separately in the cache (so the data is only there once)

@hannojg Though I agree with this but if we do this it would require us adding some if condition in set function which would check whether a key is collection or otherwise and then add to the right data structure.

I was thinking if we can do something like this:

diff --git a/lib/OnyxCache.ts b/lib/OnyxCache.ts
index 530f8e6..69da4f6 100644
--- a/lib/OnyxCache.ts
+++ b/lib/OnyxCache.ts
@@ -9,7 +9,7 @@ import type {OnyxKey, OnyxValue} from './types';
  */
 class OnyxCache {
     /** Cache of all the storage keys available in persistent storage */
-    private storageKeys: Set<OnyxKey>;
+    private storageKeys: Map<string, Set<OnyxKey>>;
 
     /** A list of keys where a nullish value has been fetched from storage before, but the key still exists in cache */
     private nullishStorageKeys: Set<OnyxKey>;
@@ -30,7 +30,7 @@ class OnyxCache {
     private maxRecentKeysSize = 0;
 
     constructor() {
-        this.storageKeys = new Set();
+        this.storageKeys = new Map();
         this.nullishStorageKeys = new Set();
         this.recentKeys = new Set();
         this.storageMap = {};
@@ -59,8 +59,8 @@ class OnyxCache {
     }
 
     /** Get all the storage keys */
-    getAllKeys(): Set<OnyxKey> {
-        return this.storageKeys;
+    getAllKeys(key: string): Set<OnyxKey> {
+        return this.storageKeys.get(key) || new Set();
     }
 
     /**
@@ -74,15 +74,15 @@ class OnyxCache {
      *
      * @param keys - an array of keys
      */
-    setAllKeys(keys: OnyxKey[]) {
-        this.storageKeys = new Set(keys);
+    setAllKeys(key: string, keys: OnyxKey[]) {
+        this.storageKeys.set(key, new Set(keys));
     }
 
     /** Saves a key in the storage keys list
      * Serves to keep the result of `getAllKeys` up to date
      */
     addKey(key: OnyxKey): void {
-        this.storageKeys.add(key);
+        this.storageKeys.set(key, this.storageKeys.get(key)?.add(key) || new Set([key]));
     }
 
     /** Used to set keys that are null/undefined in storage without adding null to the storage map */

@kacper-mikolajczak
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

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

Thanks for the discussion, I agree that this should be beneficial and we can move ahead and brainstorm a holistic solution in parallel @hannojg @hurali97

@mountiny
Copy link
Contributor

mountiny commented Aug 7, 2024

All yours @MariaHCD

@MariaHCD MariaHCD merged commit 16349bb into Expensify:main Aug 7, 2024
5 checks passed
Copy link
Contributor

github-actions bot commented Aug 7, 2024

🚀Published to npm in v2.0.62

@adhorodyski adhorodyski deleted the perf/keyChanged branch August 29, 2024 13:34
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.

6 participants