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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!


for (let i = 0; i < stateMappingKeys.length; i++) {
const subscriber = callbackToStateMapping[stateMappingKeys[i]];
if (!subscriber || !isKeyMatch(subscriber.key, key) || !canUpdateSubscriber(subscriber)) {
Expand All @@ -820,7 +823,12 @@ function keyChanged<TKey extends OnyxKey>(
}

if (isCollectionKey(subscriber.key) && subscriber.waitForCollectionCallback) {
const cachedCollection = getCachedCollection(subscriber.key);
let cachedCollection = cachedCollections[subscriber.key];

if (!cachedCollection) {
cachedCollection = getCachedCollection(subscriber.key);
cachedCollections[subscriber.key] = cachedCollection;
}

cachedCollection[key] = value;
subscriber.callback(cachedCollection);
Expand Down
Loading