Skip to content

perf: remove redundant O(employees) member scans#96080

Open
adhorodyski wants to merge 3 commits into
Expensify:mainfrom
callstack-internal:slow-large-account-hello
Open

perf: remove redundant O(employees) member scans#96080
adhorodyski wants to merge 3 commits into
Expensify:mainfrom
callstack-internal:slow-large-account-hello

Conversation

@adhorodyski

@adhorodyski adhorodyski commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

Every rendered SettlementButton (report header, each report preview, each Pay row on the Reports page) called getPolicyEmployeeAccountIDs, which walks the whole policy employee list. On a ~21k-member policy that is a 21k-entry array built per button, per render — and the result was thrown away unused.

The array fed one function: doesReportBelongToWorkspace. It was built for the workspace switcher (its call site was removed in #79940) and only reads the array for DMs — a case that cannot occur at any of its remaining call sites, because every caller derives policyID from the report chain itself. The payment code only uses the boolean to pick the NVP_LAST_PAYMENT_METHOD key, and that NVP is only ever written keyed by iouReport.policyID.

This PR deletes doesReportBelongToWorkspace and getPolicyEmployeeAccountIDs and replaces them with two field comparisons (isConciergeChatReport || isPolicyRelatedReport). Composer mention weighting now checks policy.employeeList[login] directly (O(1)) instead of scanning the array per suggestion. usePaymentOptions also drops a now-unused whole-policy subscription.

This does not break userspace. Two places differ on purpose:

  • isPolicyRelatedReport now returns false for an undefined or _FAKE_ policyID. Before, an undefined ID matched every report that also had no policyID, so useTransactionViolationOfWorkspace computed archive data before any delete was started. The delete flow itself is unchanged.
  • Composer mention weighting looks up employeeList[login] by exact key. The old path went through a lowercased email cache. If a key's casing ever differs from the login, that person sorts one tier lower in suggestions — nothing else changes.

For a 21k members workspace this took ~850ms each time to call getPolicyEmployeeAccountIDs during every render. With this PR it disappears.

Fixed Issues

$ https://github.com/Expensify/Expensify/issues/591294
PROPOSAL:

Tests

  1. As a workspace admin, open a report with expenses. Verify the Pay button shows the same payment options as before and the previously used payment method is preselected.
  2. Pay an expense report choosing a specific payment method (e.g. Pay elsewhere). Reopen the report from the Reports page and from the chat preview. Verify the same method is preselected in both places.
  3. In a workspace chat composer, type @ followed by a few letters. Verify report participants rank first, workspace members second, others last.
  4. In a 1:1 DM composer, type @. Verify suggestions are plain alphabetical (no member weighting).
  • Verify that no errors appear in the JS console

Offline tests

N/A (no network-dependent logic changed).

QA Steps

Same as tests.

  1. As a workspace admin, open a report with expenses. Verify the Pay button shows the same payment options as before and the previously used payment method is preselected.
  2. Pay an expense report choosing a specific payment method (e.g. Pay elsewhere). Reopen the report from the Reports page and from the chat preview. Verify the same method is preselected in both places.
  3. In a workspace chat composer, type @ followed by a few letters. Verify report participants rank first, workspace members second, others last.
  4. In a 1:1 DM composer, type @. Verify suggestions are plain alphabetical (no member weighting).
  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed 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 added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • 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: mWeb Chrome
    • iOS: Native
    • iOS: mWeb 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 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 any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • 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)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • 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 PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • 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.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari

getPolicyEmployeeAccountIDs enumerated the full policy employeeList
(~21k entries on large accounts) on every SettlementButton and
usePaymentOptions render, feeding doesReportBelongToWorkspace — a
helper built for the removed workspace switcher whose expensive DM
branch is unreachable at every remaining call site. Replace both with
field comparisons (isConciergeChatReport || isPolicyRelatedReport),
harden isPolicyRelatedReport against undefined/_FAKE_ policyIDs, and
switch composer mention weighting to an O(1) employeeList lookup.
# Conflicts:
#	src/hooks/usePaymentOptions.ts
#	src/pages/inbox/report/ReportActionCompose/SuggestionMention.tsx
#	tests/unit/PolicyUtilsTest.ts
#	tests/unit/SuggestionMentionTest.tsx
@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
src/components/SettlementButton/index.tsx 85.13% <100.00%> (-0.06%) ⬇️
src/hooks/usePaymentOptions.ts 55.91% <100.00%> (-1.38%) ⬇️
src/libs/PolicyUtils.ts 77.51% <ø> (-0.06%) ⬇️
src/libs/ReportUtils.ts 86.99% <50.00%> (-0.03%) ⬇️
...x/report/ReportActionCompose/SuggestionMention.tsx 80.41% <85.71%> (-0.10%) ⬇️
... and 33 files with indirect coverage changes

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@adhorodyski adhorodyski changed the title Remove dead doesReportBelongToWorkspace and its O(employees) member scan from SettlementButton perf: remove redundant O(employees) member scans Jul 14, 2026
@adhorodyski

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 6a65d950b6

ℹ️ 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".

@adhorodyski adhorodyski marked this pull request as ready for review July 15, 2026 13:01
@adhorodyski adhorodyski requested review from a team as code owners July 15, 2026 13:01
@melvin-bot melvin-bot Bot requested review from sobitneupane and removed request for a team July 15, 2026 13:01
@melvin-bot

melvin-bot Bot commented Jul 15, 2026

Copy link
Copy Markdown

@sobitneupane Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot melvin-bot Bot requested review from JmillsExpensify and removed request for a team July 15, 2026 13:01
const {accountID} = useCurrentUserPersonalDetails();

// The app would crash due to subscribing to the entire report collection if chatReportID is an empty string. So we should have a fallback ID here.
// eslint-disable-next-line rulesdir/no-default-id-values

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.

❌ CONSISTENCY-3 (docs)

The expression isConciergeChatReport(chatReport, conciergeReportID) || isPolicyRelatedReport(chatReport, policyID) is duplicated verbatim here and in src/components/SettlementButton/index.tsx, and the same workspace-membership check also appears in src/pages/inbox/report/ReportActionCompose/SuggestionMention.tsx. This logic previously lived in the now-removed doesReportBelongToWorkspace helper. Re-inlining it in multiple places violates DRY and means every future change must be applied in each copy.

Consolidate the check into a single reusable helper (e.g. a slimmed-down doesReportBelongToWorkspace(report, policyID, conciergeReportID) in @libs/ReportUtils) and call it here instead of duplicating the expression.


Reviewed at: 6a65d95 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

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

Copy link
Copy Markdown

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: 6a65d950b6

ℹ️ 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".

Comment on lines +82 to 83
const reportBelongsToWorkspace = policyID ? isConciergeChatReport(chatReport, conciergeReportID) || isPolicyRelatedReport(chatReport, policyID) : false;
const policyIDKey = reportBelongsToWorkspace ? policyID : CONST.POLICY.ID_FAKE;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the workspace key for DM-backed reports

When this hook is used by the report header/selection pay menus with an iouReport.policyID but a linked 1:1 chat whose policyID is missing or _FAKE_, this new check now falls through to CONST.POLICY.ID_FAKE. The removed helper used to classify those DM chats as belonging to the workspace when a participant was in the policy, and payment writes the preferred method under iouReport.policyID (PayMoneyRequest.ts stores NVP_LAST_PAYMENT_METHOD keyed by that policy), so the previously selected workspace payment method will no longer be read/preselected for those reports.

Useful? React with 👍 / 👎.

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.

1 participant