Skip to content

[Structural Sharing] PR 3: Frozen collection snapshots with lazy rebuild and per-key merge#768

Open
fabioh8010 wants to merge 6 commits intoExpensify:mainfrom
callstack-internal:feature/structural-sharing-cache-pr-3
Open

[Structural Sharing] PR 3: Frozen collection snapshots with lazy rebuild and per-key merge#768
fabioh8010 wants to merge 6 commits intoExpensify:mainfrom
callstack-internal:feature/structural-sharing-cache-pr-3

Conversation

@fabioh8010
Copy link
Copy Markdown
Contributor

@fabioh8010 fabioh8010 commented Apr 1, 2026

Details

Third PR of Expensify/App#86181

Replace the mutable collectionData (which returned {...spread} copies) with collectionSnapshots — a Map of Object.freeze()'d snapshots. When a member key is written (via set(), merge(), or drop()), its parent collection is marked dirty in a dirtyCollections Set. The frozen snapshot is only rebuilt on the next read (getCollectionData()), not on every write.

rebuildCollectionSnapshot() iterates only the members of the affected collection (using the indexed forward lookup from PR 1), creates a new frozen object, and compares each member reference against the previous snapshot. If nothing actually changed, the old snapshot reference is reused — this is what makes useOnyx's === check work.

Additionally, replace the O(total_keys) storageMap spread in merge() with a per-key loop. Keys whose merged result is === to the existing value (enabled by PR 2) are skipped entirely.

E/App PR: Expensify/App#86885

Related Issues

Expensify/App#86181

Automated Tests

Unit tests were added.

Manual Tests

General testing over the app. Use Expensify/App#86885 for testing:

  1. Login with an account.
  2. Go to Inbox tab, scroll the chat list a bit.
  3. Select a chat and send a message.
  4. React to that message and create a thread.
  5. Create a task and complete it.
  6. Create an expense via FAB, go to the expense details.
  7. Go to Reports tab and check if expense is there. Go to its details.
  8. Go to Settings and logout.

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
  • 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
Screen.Recording.2026-04-03.at.15.07.16-compressed.mov
Android: mWeb Chrome

N/A, chrome is always crashing in my emulator

iOS: Native
Screen.Recording.2026-04-03.at.15.13.56-compressed.mov
iOS: mWeb Safari
Screen.Recording.2026-04-03.at.15.17.41-compressed.mov
MacOS: Chrome / Safari
Screen.Recording.2026-04-03.at.15.20.27-compressed.mov
Screen.Recording.2026-04-03.at.15.23.32-compressed.mov

fabioh8010 and others added 3 commits March 19, 2026 16:57
Replace mutable collectionData with frozen collectionSnapshots and dirty
tracking. Collection snapshots are lazily rebuilt on read, returning
stable frozen references for structural sharing. The merge() method now
operates per-key instead of spreading the entire storageMap, and
hasValueChanged() uses reference equality as a fast path.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@fabioh8010 fabioh8010 changed the title [WIP][Structural Sharing] PR 3: Frozen collection snapshots with lazy rebuild and per-key merge [Structural Sharing] PR 3: Frozen collection snapshots with lazy rebuild and per-key merge Apr 3, 2026
@fabioh8010 fabioh8010 marked this pull request as ready for review April 3, 2026 15:11
@fabioh8010 fabioh8010 requested a review from a team as a code owner April 3, 2026 15:11
@fabioh8010
Copy link
Copy Markdown
Contributor Author

@Julesssss @tgolen ready to review!

@melvin-bot melvin-bot bot requested review from AndrewGable and removed request for a team April 3, 2026 15:11
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7f580cee2d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@AndrewGable AndrewGable requested review from Julesssss and tgolen and removed request for AndrewGable April 3, 2026 16:24
@Julesssss Julesssss requested a review from roryabraham April 3, 2026 19:02
Comment on lines +544 to 547
if (this.storageKeys.size > 0) {
return FROZEN_EMPTY_COLLECTION;
}
return undefined;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is feedback from Claude so I could be mistaken, but should we be checking for storage keys from a specific key? Rather than any key.

The decision between "this collection is empty" vs "this collection hasn't loaded yet"
hinges entirely on whether storageKeys has any key in it — from any collection or
non-collection key.

  • getCollectionData("report_") is called before the report collection has loaded from
    storage.

So in theory, subscribers might temporarily receive an empty object instead of FROZEN_EMPTY_COLLECTION?

import OnyxKeys from './OnyxKeys';

/** Frozen object containing all collection members — safe to return by reference */
type CollectionSnapshot = Readonly<NonUndefined<OnyxCollection<KeyValueMapping[OnyxKey]>>>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I understand there's a high cost to deep-freezing the object at runtime, but should we consider using ReadonlyDeep from type-fest to prevent accidental mutations lower in the tree at compile time?

}

const snapshot = this.collectionSnapshots.get(collectionKey);
if (!snapshot || Object.keys(snapshot).length === 0) {
Copy link
Copy Markdown
Contributor

@roryabraham roryabraham Apr 3, 2026

Choose a reason for hiding this comment

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

micro-optimization: Skip an array allocation of all the keys - it's not necessary for an empty check. Using for-in instead will stop as soon as the first value is found:

diff --git a/lib/OnyxCache.ts b/lib/OnyxCache.ts
index 063ea5eb..6980e69c 100644
--- a/lib/OnyxCache.ts
+++ b/lib/OnyxCache.ts
@@ -538,7 +538,7 @@ class OnyxCache {
         }
 
         const snapshot = this.collectionSnapshots.get(collectionKey);
-        if (!snapshot || Object.keys(snapshot).length === 0) {
+        if (utils.isEmptyObject(snapshot)) {
             // If we know we have storage keys loaded, return a stable empty reference
             // to avoid new {} allocations that break useSyncExternalStore === equality.
             if (this.storageKeys.size > 0) {
diff --git a/lib/utils.ts b/lib/utils.ts
index 1fca1c8a..efdc5173 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -172,7 +172,17 @@ function mergeObject<TObject extends Record<string, unknown>>(
 
 /** Checks whether the given object is an object and not null/undefined. */
 function isEmptyObject<T>(obj: T | EmptyValue): obj is EmptyValue {
-    return typeof obj === 'object' && Object.keys(obj || {}).length === 0;
+    if (typeof obj !== 'object') {
+        return false;
+    }
+    // Use is for-in loop to avoid using Object.keys to do an unnecessary array allocation
+    // eslint-disable-next-line no-restricted-syntax
+    for (const key in obj) {
+        if (Object.hasOwn(obj, key)) {
+            return false;
+        }
+    }
+    return true;
 }
 
 /**

This should be faster (O(1) instead of O(n)), particularly for objects with lots of keys

if (collectionKey && this.collectionData[collectionKey]) {
delete this.collectionData[collectionKey][key];
if (collectionKey) {
affectedCollections.add(collectionKey);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Rather than having a separate affectedCollections variable (which is kind of confusing), could you add these directly to this.dirtyollections?

* @param collectionKey - The collection key to rebuild
*/
private rebuildCollectionSnapshot(collectionKey: OnyxKey): void {
const oldSnapshot = this.collectionSnapshots.get(collectionKey);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please rename to previousSnapshot or currentSnapshot. I don't think "old" is as helpful.

const needsPrefixCheck = !memberKeys;

for (const key of keysToScan) {
if (needsPrefixCheck && OnyxKeys.getCollectionKey(key) !== collectionKey) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add a code comment to explain why this early continue is there (I'm not sure I understand why).

const oldSnapshot = this.collectionSnapshots.get(collectionKey);

const members: NonUndefined<OnyxCollection<KeyValueMapping[OnyxKey]>> = {};
let hasChanges = false;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you rename this to be a little more descriptive? What has changes? The old snapshot?

// Check if any members were removed (old snapshot had more keys)
if (!hasChanges && oldSnapshot) {
const oldMemberCount = Object.keys(oldSnapshot).length;
if (oldMemberCount !== newMemberCount) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this logic safe? What if Key A was removed and Key B was added? The counts would be the same, but the snapshot would be different.

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.

4 participants