Repository navigation
Perf/spend filters hover - #97697
Perf/spend filters hover#97697
Conversation
Codecov Report✅ Changes either increased or maintained existing code coverage, great job!
|
51bb726 to
49096a3
Compare
Hovering a filter row in the Search filters popover remounted its whole content and rebuilt the option lists from scratch, which blocked the main thread for over a second per row on accounts with large personal details. - Switch the content pane to a debounced rested filter, so sweeping the cursor across rows moves only the highlight. - Keep the last three rested filter contents mounted inside <Activity>, so returning to one toggles visibility instead of remounting. - Memoize the three full passes over the personal details list (createOptionList, getValidOptions, the UserSelector list copy) with a comparator that walks one level into rebuilt record arguments, since the snapshot-aware useOnyx rebuilds collection identities on Search pages every render. - Keep the option list Onyx subscriptions warm for the popover lifetime.
49096a3 to
b745fb8
Compare
Drop the keep-warm Onyx subscription, the module-level memoization of the personal detail derivations and the equivalentArgsComparator they needed. Freezing the form values of backgrounded contents and comparing props on them keeps the same panes from re-rendering, so the caches are unnecessary. Explain why React.memo is still needed next to React Compiler: the compiler caches the whole mountedFilters.map() result in a single slot, so it cannot bail out on an individual element.
Building an option per personal details entry and filtering the whole option list are pure derivations of Onyx data, so a remounted filter content can reuse the previous result instead of walking the collection again. Both are memoized at module level behind a new equivalentArgsComparator in @libs/memoize, which compares memoization keys argument by argument: shallowly first, and one level deeper for plain objects that are rebuilt on every call from unchanged sources. Measured on an account with 100k personal details, sweeping and resting on every filter row: 4.2s of blocked JS down to 1.7s.
| * The two-level fallback below reads arguments through `Object.keys`, which only describes plain objects - for other | ||
| * types (Set, Map, Date, class instances) it returns an empty list and any two instances would look identical. | ||
| */ | ||
| const isPlainObject = (value: unknown): value is Record<string, unknown> => { |
There was a problem hiding this comment.
Can't we use lodash-es/isPlainObject instead ?
There was a problem hiding this comment.
Yes, switching to it.
| ACCESSIBILITY_ANNOUNCEMENT_DEBOUNCE_TIME: 1000, | ||
| SUGGESTION_DEBOUNCE_TIME: 100, | ||
| /** How long the cursor has to stay on an advanced filter row before its content is rendered */ | ||
| SEARCH_FILTER_HOVER_INTENT_DELAY: 80, |
There was a problem hiding this comment.
Can be a potential accessibility issue, if user navigates with keyboard they can land in old pane and then it switches ?
There was a problem hiding this comment.
Good catch, you're right, I'm fixing it.
The 80ms delay is currently applied to both mouse hover and keyboard focus. It makes sense for hover, because it avoids rendering filters you only pass over with the cursor. For keyboard navigation, every focus change is intentional, so the delay just causes the details pane to lag behind the selected row.
| * Building an option per personal details entry is the expensive step of this hook and depends only on Onyx values. | ||
| * Arguments are compared one level deep because the maps are rebuilt each render from those same unchanged values. | ||
| */ | ||
| const memoizedCreateOptionList = memoize(createOptionList, { |
There was a problem hiding this comment.
Does this memoize survive logout ?
There was a problem hiding this comment.
It does - great catch, fixing it.
- Keyboard focus no longer goes through the hover intent delay. Moving focus is deliberate and never sweeps across rows, so its content is shown right away instead of replacing the pane a keyboard user has already moved into. - The memoized personal detail caches are released on sign-out, so option lists built for the previous account do not stay in memory. Needed a clear() on the memoize cache, which it did not expose. - formAtLastRest and contentVersions are pruned to the mounted filters instead of gaining an entry per visited filter. - equivalentArgsComparator uses lodash/isPlainObject instead of a local copy.
|
🚧 mountiny has triggered a test Expensify/App build. You can view the workflow run here. |
This comment has been minimized.
This comment has been minimized.
|
🚧 mountiny has triggered a test Expensify/App build. You can view the workflow run here. |
This comment has been minimized.
This comment has been minimized.
Resolve conflicts in usePersonalDetailOptions and usePersonalDetailSearchSelector/base: keep the Onyx selectors and includeLoginsOnly filtering from main, and re-apply the memoized createOptionList, getValidOptions and selected-options derivations on top.
…nd trim the comments the review flagged
# Conflicts: # src/components/Search/SearchAutocompleteList.tsx
|
@mkhutornyi @truph01, do you have any other comments or feedback? |
|
Doing some more regression tests. Will update shortly |
|
@mkhutornyi Once you've finished testing, could you let me know if anything needs fixing, or if the PR is ready to merge? Thanks! |
|
@roryabraham Hi, if everything looks good, can we go ahead and merge this? We've already had feedback from @truph01 , and @mkhutornyi hasn't added any new comments in the last two days, so I think we might be ready. |
Yes, all look good from my side |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
|
Two regressions worth fixing before merge, plus three things to confirm. The perf mechanisms themselves check out — Onyx freshness on reveal, cross-account/locale cache isolation, and keyboard navigation are all safe. 1. The option-list caches are never released for the ~13 non-Search consumers
Second, smaller hole: the effect's cleanup only unsubscribes. If Fix: call the two clear functions from the effect cleanup as well, and mount the hook somewhere that covers the non-Search consumers (or release on a cheaper signal than "left Search"). 2. Pane-local state survives a reveal, because
|
|
🚧 roryabraham has triggered a test Expensify/App build. You can view the workflow run here. |
|
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |
|
🧪🧪 Use the links below to test this adhoc build on Android, iOS, and Web. Happy testing! 🧪🧪
|
|
🚀 Deployed to staging by https://github.com/roryabraham in version: 9.4.76-0 🚀
|
|
No help site changes are required for this PR, so I did not create a docs PR. This is a rendering-performance change to the advanced filters popover on the Spend page. It does not add, remove, rename, or reorder any filter, and it changes no user-facing copy. How I checkedNo copy changed. The diff touches no Nothing documented is now wrong. The help site documents which filters exist and how to apply them, not how panes mount or load:
The behavior changes that are user-visible are all below the level the help site describes:
I checked all articles under If you think one of these behaviors is worth documenting anyway — the in-pane search term being cleared is the closest call — reply here and I'll open the draft PR. @sumo-slonik, there is no linked help site PR to review, since no article needed a change. Please confirm you agree, or tell me which behavior you want documented. view run · no recording available |
|
🚀 Deployed to production by https://github.com/mountiny in version: 9.4.76-1 🚀
Bundle Size Analysis (Sentry): |




Explanation of Change
What was wrong
Every hover over a filter row remounted the whole content pane - one pane was rendered at a time, keyed by the filter it showed, so moving to another filter swapped it - and the people filters (From, To, Attendee, Assignee) rebuilt the entire personal detail option list from scratch on every mount: several full O(n) passes over the
personalDetailsListcollection (building options, filtering, sorting, cloning). On accounts with a large number of contacts this blocked the JS thread for hundreds of milliseconds per hover, so sweeping the cursor down the filter list froze the popover and the highlight lagged several rows behind the cursor.What this PR changes
Hover intent on the contact list only: pointing at a filter row shows that filter straight away, at any cursor speed. What waits for the cursor to come to rest is not the pane but the one derivation heavy enough to be felt: building the contact list, which only the four filters routed to
UserSelectorread (From, To, Attendee, Assignee). They mount at once and stand as their own spinner until the cursor rests forSEARCH_FILTER_HOVER_INTENT_DELAY(30 ms), and every other filter renders with no wait. Time spent on a row cannot tell a deliberate hover from a slow pass over the list, but coming to rest can. Movement smaller thanHOVER_INTENT_REST_RADIUS_PXstill counts as resting so a shaky hand is not held back, leaving the list releases whatever the row it ended on was withholding, and keyboard focus withholds nothing.Keeping visited panes mounted: the content of a filter stays mounted once visited, so returning to it costs nothing. A backgrounded pane is hidden with
<Activity mode="hidden">instead of being unmounted, so it keeps the list it has already built and holds no Onyx subscriptions while hidden. Panes are mounted lazily and only live while the popover is open. A hidden pane goes on rendering with the values it was rendering with when it was last shown, so moving between filters does not re-render it. Separately, the values an instance was built from are what a revisit is compared against: a pane whose values moved on since it was built is remounted rather than revealed, because a content decides its selection pinning on mount. Only the values that content reads count, so a change to a filter it does not read leaves it alone.Memoizing the derivations behind the people filters:
createOptionListinusePersonalDetailOptions, andgetValidOptionsplus the selected-state mapping inusePersonalDetailSearchSelector, are pure derivations of Onyx data, so they are memoized at module level and survive a remount. They use a newequivalentArgsComparatorexported from@libs/memoize, which compares memoization keys argument by argument: shallowly first, and one level deeper for plain objects that are rebuilt on every call from unchanged sources (e.g. a mapped Onyx collection).usePersonalDetailOptionsalso skips its whole derivation chain while it is loading or disabled. Because those caches live at module level,useReleaseOptionListCachesdrops them once another tab takes over from Search, so the lists are not pinned in memory beyond the screen that wanted them - tens of megabytes on an account with a large contact list.A revealed pane looks like the one that was built, not the one that was left: keeping a pane mounted also keeps what you did in it, and a search term you typed and a place you scrolled to are not worth keeping - coming back to a filter should offer the whole list again. A hidden
<Activity>subtree keeps its state but unmounts its effects, so a list runs its mount effect again when it is revealed and its cleanup runs as it is hidden. What was typed is dropped as the pane is hidden, so the debounce behind the input elapses while nobody is looking at it rather than in front of the eyes it is next revealed to. The list itself is replaced rather than scrolled: its container loses the scroll position while hidden while the recycler still reports the offset it was left at, which is what leaves the visible area blank, and a recycler takes an offset only from a scroll event, so scrolling to a top it already sits at produces none. A fresh recycler starts where its container already is. This lives inuseRevealedListResetand is used by bothBaseSelectionListandBaseSelectionListWithSections, so every filter pane is covered; a list left at the top is revealed untouched, and dropping the search term is opt-in per consumer. Selecting in a pane is the one thing a revisit keeps, because that pane is rebuilt anyway (see 2), which is what floats the selected rows to the top.A spinner rather than a row skeleton while a contact list is not being built yet, held at the height a contact list occupies so what arrives does not push the panel open under the cursor. A skeleton would draw a list that is not there and flicker row by row as the cursor moves between filters. This also changes the full-screen filter page on native, which passes
ready={didScreenTransitionEnd}and previously showed the skeleton during the screen transition.Measurements
Chrome, dev build, an account seeded with 106k personal details, on a 1440x900 viewport where the popover offers 14 filter rows. The metric is the total
longtaskduration - how long the JS thread was blocked - summed over a phase. Variants were interleaved rather than measured in blocks, n=5 per variant, and each run reads its own variant back from the bundle, so a run cannot be attributed to a tree that was not rebuilt.One run walks five phases: a fast sweep over every row (15 ms per row, three passes each way), a slow sweep where every row is dwelled on long enough to activate it (60 ms per row, three passes each way), a tour resting 500 ms on each row, alternating between From and To four times, and a detour of From -> merchant -> date -> amount three times over, ending back on From.
mainmean (sd)¹ Measured on an earlier revision where the pane itself waited for the cursor to rest, so a fast sweep rendered nothing at all. The current one renders every filter as the cursor reaches it, which puts back the work of mounting the panes a sweep passes over, so these two are a floor rather than what it does. The other phases all dwell long enough to activate a row either way.
Every phase separates cleanly, with disjoint ranges across runs, except the longest single task, where the ranges overlap. That is the honest limit of this change: the popover stops doing the same work over and over, but building the option list for 106k contacts once is still a single synchronous pass, and that pass is what the longest task measures. Splitting it up is a separate change.
Where each mechanism shows up: withholding the contact list is what the fast and slow sweeps measure, keeping panes mounted takes the revisit and the detour to zero, and the memoized derivations are what make the mounts that remain cheap - measured separately at -46% for
createOptionListand a further -23% forgetValidOptionsplus the selected-state mapping.Why
Activityrather than hiding a backgrounded pane with a style. Both variants measured on the same base, one run per branch checkout, strictly interleaved, n=5 per variant, on an account with 106k personal details.visibility: hiddenActivitypersonalDetailsListupdates while the popover is open, two heavy panes mountedThe first two separate cleanly (disjoint ranges across runs, tight spread); the third one's ranges overlap, so read it as a cost of up to ~10% on the static case rather than a measured number.
Activitydoes not reduce the total work under churn - 18.4 s vs 18.7 s of blocked JS summed, indistinguishable - it splits it into 24 tasks instead of 9, and that is what moves the worst blocking task.Worth flagging separately: those eight personal detail updates block ~18 s of JS in both variants. Neither approach reduces that, one only slices it differently, and it needs its own fix.
Absolute values are inflated by the dev build; the ordering and the ratios are the part to trust.
Before / after comparison
performance.filters.mp4
Fixed Issues
$ #97455
Tests
Best tested on an account with a large number of contacts (e.g. a High Traffic account), where the freeze was clearly visible before this change.
Offline tests
Unnecessary - this is a rendering performance change only, no network interaction is affected.
QA Steps
Same as Tests.
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectionAvatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))npm run compress-svg)Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.