Skip to content

[$175] perf: Share policy collection scans between subscribers #108486

Todo

Description

@mountiny

Problem

On accounts with thousands of workspaces, every write to one workspace made about 30 subscribers each scan the whole policy collection with the same few checks. On an account with about 6000 workspaces, one unrelated policy write cost about 290 to 370 ms of JavaScript on Home.

Solution

The shared policy helpers now cache their result per frozen Onyx collection snapshot, keyed by the login, account ID, or offline state each answer depends on. The first subscriber does the scan and the rest reuse it. The Workspaces tab red dot also remembers each policy's check results, so a write only re-checks the policy that changed. JavaScript work on Home dropped from 286 to 373 ms down to 183 to 222 ms.

PR

#108442

Issue OwnerCurrent Issue Owner: @samranahm
Upwork Automation - Do Not Edit

Activity

  1. added
    Reviewing
    Daily
    Bug
    on Dec 1, 2026
  2. added
    External
    on Dec 1, 2026
  3. expensify-chore-bot commented on Dec 1, 2026

    @expensify-chore-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @Pujan92 (External)

  4. changed the title [-]perf: Share policy collection scans between subscribers[/-] [+][$175] perf: Share policy collection scans between subscribers[/+] on Dec 1, 2026
  5. expensify-chore-bot commented on Dec 1, 2026

    @expensify-chore-bot
  6. MelvinBot commented on Dec 1, 2026

    @MelvinBot
    Contributor

    Proposal

    What is the root cause of that problem?

    Multiple subscribers independently filter the same workspace collection after every policy update. Even when a selector returns an unchanged boolean and prevents a re-render, its collection scan still runs. With thousands of workspaces, those repeated scans accumulate substantial JavaScript work.

    Confidence: Medium. The code supports this mechanism; the issue’s subscriber counts and performance measurements were not independently reproduced.

    What changes do you think we should make in order to solve the problem?

    Continue with the existing Share policy collection scans between subscribers PR rather than creating another implementation:

    • Share helper results by immutable collection snapshot, including login, account ID, and offline state where relevant.
    • Reuse policy-only red-dot checks for unchanged policy objects, while keeping sync-progress-dependent checks live.
    • Preserve shared arrays by copying before sorting.
    Code evidence and remaining validation

    Evidence chain

    Challenges and limitations

    • This reduces duplicate computation; it does not eliminate collection traversal on each changed snapshot.
    • Two helpers still perform Object.keys-based emptiness checks before reaching their memo lookup: policy.ts:297–335. Moving those checks inside the cached computation would avoid that remaining repeated enumeration.
    • In-place mutation could stale cached answers. No concrete regression was found in the intended immutable Onyx update flow.
    • Before merge, add coverage for account/login/offline transitions, sync-progress-only changes, unchanged-policy predicate reuse across subscribers, and shared-array sorting.
    • No runtime tests or profiling were performed during this analysis. Controlled before/after profiling remains necessary to substantiate the reported timings; production logs are not needed to establish this client-side computation mechanism.

    What alternative solutions did you explore? (Optional)

    Narrower selectors reduce re-renders but do not share their computation across subscribers. A fully incremental policy index could avoid more scanning, but requires substantially more invalidation logic.

    Related issue: Avoid unnecessary Home, Settings and tab bar renders on workspace updates addresses complementary rendering work, rather than duplicate helper computation.


    Next Steps for Contributor+ team:
    To accept: @MelvinBot implement [this](https://github.com/Expensify/App/issues/108486) to create a draft PR. An implementation PR already exists, so review that first.
    To refine: @MelvinBot <your feedback>
    To reject: Explain why you are rejecting Melvin's proposal.


    view run

  7. assigned and unassigned on Dec 1, 2026
  8. added
    Weekly
    and removed
    Daily
    on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugExternalReviewingWeekly

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions