Skip to content

[$175] perf: Reuse one policy list across selected reports in the change-workspace check #108489

Todo

Description

@mountiny

Problem

When reports are selected in Spend, useSearchBulkActions checks every selected report to decide whether to offer "Change workspace". Both checks behind it, isChangeWorkspaceAction and canChangeWorkspaceOfSearchReport, loop over Object.values(policies). That builds a new array of every workspace for each selected report. On an account with about 6,000 workspaces, selecting a page of reports copies the whole collection once per report.

Solution

A new getPolicyList helper in PolicyUtils lists the workspaces once per frozen policy collection snapshot and reuses that list. A changed workspace always gets a fresh list. Both checks loop over the cached list, and their results are unchanged. canChangeWorkspaceOfSearchReport dropped from 169 ms to 8 ms when selecting 50 reports.

PR

#108471

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

Activity

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

    @expensify-chore-bot

    Current assignee @DylanDylann is eligible for the External assigner, not assigning anyone new.

  5. changed the title [-]perf: Reuse one policy list across selected reports in the change-workspace check[/-] [+][$175] perf: Reuse one policy list across selected reports in the change-workspace check[/+] on Dec 1, 2026
  6. expensify-chore-bot commented on Dec 1, 2026

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

    @MelvinBot
    Contributor

    Proposal

    Confidence: Medium — the code supports the allocation bottleneck and proposed fix; browser performance measurements were not independently reproduced.

    What is the root cause of that problem?

    The change-workspace eligibility checks repeatedly build arrays containing every workspace, even when their subsequent loops stop early. With thousands of workspaces and many selected reports, this creates unnecessary enumeration and allocation.

    The bulk-actions hook stops at the first movable report, so it does not always check every selected report. The worst case is when none can move. The workspace-selection page checks every resolved selected report.

    Code evidence

    These arrays contain policy references; they are not deep copies of workspace data.

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

    The existing Reuse one policy list across selected reports PR addresses this bottleneck appropriately: share one policy list per frozen collection identity, while continuing to evaluate eligibility separately for each report. Review that PR rather than opening another implementation.

    Before merging, extend regression coverage for workspace additions/removals and repeat the large-account browser profile.

    Correctness checks and validation limits
    • The proposed getPolicyList helper uses a WeakMap, caches only frozen collections, and rebuilds lists for mutable inputs.
    • The pinned Onyx snapshot implementation freezes collections and replaces snapshot identity when membership or policy references change. This supports cache invalidation.
    • I challenged whether Search supplies mutable reconstructed collections. The affected callers sit outside Search’s snapshot scope and receive live Onyx data; the switching logic is in useOnyx.ts:63–90.
    • Existing helper tests cover frozen-list reuse, replacement snapshots, undefined entries, and mutable additions. They were inspected, not executed.
    • Report-dependent eligibility scans remain; this does not eliminate all report-by-workspace work. The reported 169 ms → 8 ms improvement comes from the PR author’s benchmark, not this investigation.

    What alternative solutions did you explore? (Optional)

    Compute the policy list once in each caller and pass it through both helpers. That avoids shared caching but requires broader signature changes.

    Related: Share policy collection scans between subscribers addresses similar repeated scanning across subscribers, rather than across selected reports.


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


    view run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugDailyExternalReviewing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions