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

Don't remove nulls in cache merge #411

Merged
merged 2 commits into from
Nov 6, 2023

Conversation

janicduplessis
Copy link
Contributor

@janicduplessis janicduplessis commented Oct 31, 2023

Details

When merging cache we should not remove null values, they are useful to know an item was fetched but doesn't exist.

Related Issues

#408 (comment)

Automated Tests

Added a unit test to assert behaviour of null values in OnyxCache.

Manual Tests

Tested in Expensify app and made sure the money request button renders at the same time as the rest of the header.

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

Web
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android

@janicduplessis janicduplessis marked this pull request as ready for review October 31, 2023 21:02
@janicduplessis janicduplessis requested a review from a team as a code owner October 31, 2023 21:02
@melvin-bot melvin-bot bot requested review from chiragsalian and removed request for a team October 31, 2023 21:03
Copy link
Contributor

@chiragsalian chiragsalian left a comment

Choose a reason for hiding this comment

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

I think the code is fine but i thought we were using null to clear unused onyx data to free up memory so I'm unsure if this code would cause a negative impact.
Either way I'll let @marcaaron also review since he's more familiar here.

@marcaaron
Copy link
Contributor

When merging cache we should not remove null values, they are useful to know an item was fetched but doesn't exist.

Sorry, I am not sure what this means. Is there more context? Can you show where we are removing the null values? And how exactly "they are useful to know an item was fetched but doesn't exist".

Copy link
Contributor

@marcaaron marcaaron left a comment

Choose a reason for hiding this comment

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

@chiragsalian maybe we can get more eyes on this one. I don't have the bandwidth to help validate this right now, but left some comments.

@@ -121,7 +121,7 @@ class OnyxCache {

// lodash adds a small overhead so we don't use it here
// eslint-disable-next-line prefer-object-spread, rulesdir/prefer-underscore-method
this.storageMap = Object.assign({}, utils.fastMerge(this.storageMap, data));
this.storageMap = Object.assign({}, utils.fastMerge(this.storageMap, data, false));
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean we would just leave in the storage map a bunch of keys with null and never delete them? I think it does.

It is a bit hard to wrap one's head around what the side effects of this change would be 😄

One possible problem is here:

function get(key) {
// When we already have the value in cache - resolve right away
if (cache.hasCacheForKey(key)) {
return Promise.resolve(cache.getValue(key));
}

Since a value that we are setting again after deleting previously would be undefined but now would be regarded as null so we would not check to see if it should be read from storage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We will have more null values in cache storage yes, but this is only the in memory cache and not the persisted storage so I think it is fine to speedup lookup of values that we know do not exist in storage.

The behavior for deleted values should not change since the cache has a drop method which should be used to remove values completely. The only case I know of that we remove values from cache so they will be fetched from storage again is

removeLeastRecentlyUsedKeys() {
which doesn't use merge. In other cases if we use merge with nulls to signal values were deleted it is fine to have them in cache still since we know their latest value is that they do not exist.

I hope that clears up any confusion.

Copy link
Contributor

@marcaaron marcaaron Nov 2, 2023

Choose a reason for hiding this comment

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

it is fine to speedup lookup of values that we know do not exist in storage.

That's a good point - I was thinking more along the lines of whether this could lead to anything unexpected e.g. could it somehow be possible for the value to exist in storage, but still null in the cache? But doesn't seem likely.

@marcaaron
Copy link
Contributor

at the very least I think we should do some testing for this PR against the App

@janicduplessis
Copy link
Contributor Author

Sorry I think this PR lacked a little bit more context around this change. The idea was to improve Onyx caching by caching null for missing values. Previously if a key did not exist we never cached it which means withOnyx components will start in a loading state then have to re-render, even if the data was loaded before. The PR to implement this is #401. More specifically in the caching layer it means that undefined represent a value that we do not know if it exists and null represents a value that we know does not exist.

This PR is a bug fix since calling merge will wipe all null values from cache and cause all those components to start in loading state again. I don't think there is much downside to keeping these null values in cache. It seems like wiping them is kind of an accidental side effect of 69c11fa. Previously they were not removed. Maybe @chrispader can confirm this is fine as I'm not familiar with the original intent of that PR.

@janicduplessis
Copy link
Contributor Author

@chiragsalian We have a method that clears least recently used keys from cache, but it doesn't use the merge method that is affected here, the cache has a drop method which should be used to fully remove items from cache and cause them to be fetched again from storage. Also note this is only the caching layer and doesn't affect persisted storage.

@janicduplessis
Copy link
Contributor Author

@chiragsalian @marcaaron Thanks for having a look, lmk if there's anything I forgot to address.

@janicduplessis
Copy link
Contributor Author

Also going to ping @mountiny and @tgolen which reviewed the previous PRs related to that.

@tgolen
Copy link
Collaborator

tgolen commented Nov 2, 2023

i thought we were using null to clear unused onyx data to free up memory

I think there is a subtle difference here. null is used to clear onyx data to free up disk space, not memory. To my knowledge, we haven't done much with optimizing memory.

at the very least I think we should do some testing for this PR against the App

I definitely always agree with this one (I have learned my own hard lessons from it). To alleviate this concern a little bit, this PR is coming immediately on the heels of doing a lot of extensive in-app testing and it fixes one of the last remaining bugs that was found which prevents Onyx from being updated in App.

@tgolen
Copy link
Collaborator

tgolen commented Nov 2, 2023

More context around the discussion and testing was in this Slack thread.

@marcaaron
Copy link
Contributor

Thanks for the thorough explanation @janicduplessis. With the additional context there are no blocking concerns from me.

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.

Thank you for the explanation indeed

@chrispader
Copy link
Contributor

chrispader commented Nov 5, 2023

This PR is a bug fix since calling merge will wipe all null values from cache and cause all those components to start in loading state again. I don't think there is much downside to keeping these null values in cache. It seems like wiping them is kind of an accidental side effect of 69c11fa. Previously they were not removed. Maybe @chrispader can confirm this is fine as I'm not familiar with the original intent of that PR.

Sorry for the late reply! I also don't think there's anything wrong with this PR, as long as it's only for the cache 👍 @janicduplessis

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