Skip to content

Conversation

@Szymon20000
Copy link
Contributor

Turned out that during chat switching we have cases when we remove several items at the same time.
In those cases we don't want to recreate iterator.

Details

Related Issues

GH_LINK

Automated Tests

Linked PRs

@Szymon20000 Szymon20000 requested a review from a team as a code owner September 23, 2023 10:09
@melvin-bot melvin-bot bot requested review from francoisl and removed request for a team September 23, 2023 10:10
Copy link
Contributor

@francoisl francoisl left a comment

Choose a reason for hiding this comment

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

Makes sense, just one small question to clarify

Comment on lines +197 to +198
delete this.storageMap[temp[i]];
this.recentKeys.delete(temp[i]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, why not use drop() here?

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 don't want to remove key from storageKeys as that's used for getAllKeys. We don't remove the key from the db only from cache.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

danieldoglas
danieldoglas previously approved these changes Sep 25, 2023
Copy link
Contributor

@danieldoglas danieldoglas left a comment

Choose a reason for hiding this comment

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

LGTM

mountiny
mountiny previously approved these changes Sep 25, 2023
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.

Makes sense to me, thanks!

lib/OnyxCache.js Outdated
removeLeastRecentlyUsedKeys() {
while (this.recentKeys.size > this.maxRecentKeysSize) {
const iterator = this.recentKeys.values();
let toRemove = this.recentKeys.size - this.maxRecentKeysSize;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let toRemove = this.recentKeys.size - this.maxRecentKeysSize;
let numKeysToRemove = this.recentKeys.size - this.maxRecentKeysSize;

lib/OnyxCache.js Outdated
const temp = [];
while (toRemove > 0) {
const value = iterator.next().value;
if (value !== undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If value is undefined, should we break?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it shouldn't every be undefined. I will remove it.

toRemove--;
}

for (let i = 0; i < temp.length; ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why aggregate temp in a separate array? Could we just do these two lines inline instead of in a separate loop?

- temp.push(value);
+ delete this.storageMap[value];
+ this.recentKeys.delete(value);

Copy link
Contributor Author

@Szymon20000 Szymon20000 Sep 27, 2023

Choose a reason for hiding this comment

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

because it modifies the Set and most likely breaks iterator

@Szymon20000 Szymon20000 dismissed stale reviews from mountiny and danieldoglas via a6af414 September 27, 2023 11:34
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.

@roryabraham all yours

@roryabraham roryabraham merged commit 98a2e60 into Expensify:main Sep 28, 2023
@abdulrahuman5196 abdulrahuman5196 mentioned this pull request Sep 28, 2023
59 tasks
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.

5 participants