-
Notifications
You must be signed in to change notification settings - Fork 74
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: Lru cache #93
Perf: Lru cache #93
Conversation
This helps with extracting information to excel in a consistent matter Add an optional `methods` list parameter When such a list is provided only the methods from this list will be printed Removed any sorting Tables order varies and makes it harder to deal with in Excel
Keep a list of recently accessed keys in `OnyxCache` - `recentKeys` This serves to clean "Least Recently Used" keys from cache Keys are added to `recentKeys` for any cache access operation - get/set/merge Added a configurable limit of 150 keys in cache This looks like a sane value from observations during chat browsing
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.
Just leaving some comments as this is still in draft.
lib/Onyx.js
Outdated
@@ -742,13 +689,16 @@ function mergeCollection(collectionKey, collection) { | |||
* @param {function} registerStorageEventListener a callback when a storage event happens. | |||
* This applies to web platforms where the local storage emits storage events | |||
* across all open tabs and allows Onyx to stay in sync across all open tabs. | |||
* @param {Number} [options.maxCachedKeysCount=150] Sets how many recent keys should we try to keep in cache |
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.
Not a blocker, would maybe remove the =150
here since it's possible this could go out of date if someone changes the default at the top of the file.
Having aliases with no data captured prints empty methods/tables
We shouldn't be in a hurry to clean up cache, we can clean it up periodically every 10, 30 or 60sec. or on another event that happens less frequently
lib/Onyx.js
Outdated
setInterval(() => { | ||
// Try to free anything dated from cache | ||
cache.removeLeastRecentUsedKeys(maxCachedKeysCount); | ||
}, 10 * 1000); |
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.
Created this interval based cleanup because trying to free cache after each disconnect is too often
For example when I switch to a new chat - 10 old connections would disconnect at the same time and cause 10 cache cleans, where only 1 would be enough
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.
Instead of interval based solution perhaps we can hook to something that happens less frequently
I think it would be OK to clean cache every minute or even less frequently as long as we clean 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.
I feel maybe the interval based solution is too aggressive since most of the time we don't really need to clean the cache at all.
What if we hook the cache clean into the connection of a safe eviction key like the reportActions_*
. If we are connecting to another large object that seems like a good opportunity to clean the cache in preparation.
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.
Thanks for the idea, that sounds much better, will update
Removed tests covering the old behavior wehere a connection would keep data in cache Added tests that cover periodic cache cleanup and the new LRU functionality
…irst usage" This reverts commit d34cbb6. The functionality is actually needed to prevent duplicate decorations
Remove the setInterval cache cleanup solution Instead try to free cache when a new connection to safe eviction key happens This shouldn't happen very often and as such is ideal for cleaning cache
Updated |
I've moved to test the changes here on the latest Expensify The cache already contains everything from storage (For the Before run) But my chat list is small - I've got only 10 Here's why the cache already contains everything from storage
The changes in this PR set a default limit of 55 recent keys in cache so they should help reduce the cache footprint for users that have more than 10 Chats (With 10 chats I have 58 unique keys in storage) I've explained why we don't need to keep items in cache just because they have an active connection here: #93 (comment)
As a summary the changes here don't seem to provider any performance benefit over the existing strategy, but they are simplifying the code and can help with freeing more cache |
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.
Thanks for the breakdown. I don't mind merging these changes anyway since they are an improvement over the previous logic.
Should we get these changes into Expensify/App?
I can open a PR and test all platforms on Monday |
@marcaaron I've Opened this PR: Expensify/App#4509 to update Onyx version in Expensify/App |
cc @marcaaron
Details
PR4. Of the changes planned here: #88 (comment)
Keep a list of recently accessed keys in
OnyxCache
-recentKeys
This serves to clean "Least Recently Used" keys from cache
Keys are added to
recentKeys
for any cache access operation - get/set/mergeAdded a configurable limit of 150 keys in cache
This looks like a sane value from observations during chat browsing
Note on print/benchmark updates:
This helps with extracting information to excel in a consistent matter
Add an optional
methods
list parameterWhen such a list is provided only the methods from this list will be printed
Removed any sorting
Tables order varies and makes it harder to deal with in Excel
Related Issues
Expensify/App#2667
Automated Tests
Added test covering that recent keys are available in cache
and that older keys past the LRU limit are cleared from cache when safe eviction connection happens
Linked PRs