From f940abb438c2dfac5de45ab2282d88569bbd18c0 Mon Sep 17 00:00:00 2001 From: sumo_slonik Date: Wed, 29 Jul 2026 15:16:27 +0200 Subject: [PATCH 1/3] Speed up SearchRouter open: overlap list render with the push animation, stabilize autocomplete suggestions On native the SearchRouter list was deferred until the native-stack push animation ended, so its render ran serially after the ~350ms slide. Mount the list as soon as the skeleton has laid out so the render overlaps the native-driven animation and the list's onLayout (which ends the ManualOpenSearchRouter span) fires ~at animation end instead of ~150ms after it. Also return a stable empty array from useAutocompleteSuggestions on the empty-query path, so the consumer's sections memo stops invalidating on every render and the list no longer re-renders on each parent render. Measured on a settled heavy account (Android, n=8): ManualOpenSearchRouter ~-29% cold / ~-33% warm, SearchRouter.ListRender ~-21%, with much tighter variance (stdev 59 -> 10). --- .../index.native.tsx | 14 +++++++------- src/hooks/useAutocompleteSuggestions.ts | 7 ++++++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/components/Search/DeferredSearchAutocompleteList/index.native.tsx b/src/components/Search/DeferredSearchAutocompleteList/index.native.tsx index 41d2a01b6d3d..c8b3e33ce829 100644 --- a/src/components/Search/DeferredSearchAutocompleteList/index.native.tsx +++ b/src/components/Search/DeferredSearchAutocompleteList/index.native.tsx @@ -3,7 +3,6 @@ import type {SearchAutocompleteListProps} from '@components/Search/SearchAutocom import SearchAutocompleteList from '@components/Search/SearchAutocompleteList'; import useIsFocusedUntilTransitionEnd from '@hooks/useIsFocusedUntilTransitionEnd'; -import useRunAfterTransitions from '@hooks/useRunAfterTransitions'; import {endSpan} from '@libs/telemetry/activeSpans'; import type {SkeletonSpanReasonAttributes} from '@libs/telemetry/useSkeletonSpan'; @@ -32,12 +31,13 @@ function DeferredAutocompleteList(props: SearchAutocompleteListProps) { setHasLayout(true); }; - // Wait for the slide-in animation to finish before rendering the list. With startTransition, a competing update - // interrupted and discarded the (expensive) first mount render before it could commit, forcing React to redo - // that same render a second time. useRunAfterTransitions fires a plain, synchronous update instead, so the first render always completes in a single pass. - const shouldRender = useRunAfterTransitions(hasLayout); - - if (!shouldRender || !isFocusedUntilTransitionEnd) { + // Mount the real list as soon as the skeleton has laid out, overlapping its render with the + // native-stack push animation instead of waiting for the transition to end. The push runs on the + // UI thread, so rendering the list on the JS thread during it does not stutter the slide, and the + // list's onLayout (which ends the ManualOpenSearchRouter span) now fires while the animation is + // still finishing rather than ~150ms after it. Affordable because the option-list build is cheap + // after the caching/deferral in #95378 and #95683. + if (!hasLayout || !isFocusedUntilTransitionEnd) { return ( getUserFriendlyValue(value)); @@ -148,7 +153,7 @@ function useAutocompleteSuggestions({ } if (!autocompleteKey) { - return []; + return EMPTY_SUGGESTIONS; } const alreadyAutocompletedKeys = new Set( From b0e6f74bc2530a2b13f684b932411155510e7b9d Mon Sep 17 00:00:00 2001 From: sumo_slonik Date: Fri, 31 Jul 2026 11:39:35 +0200 Subject: [PATCH 2/3] Use the shared getEmptyArray helper for the empty autocomplete suggestions result --- src/hooks/useAutocompleteSuggestions.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/hooks/useAutocompleteSuggestions.ts b/src/hooks/useAutocompleteSuggestions.ts index 8be9f7192cd9..63a2666b300b 100644 --- a/src/hooks/useAutocompleteSuggestions.ts +++ b/src/hooks/useAutocompleteSuggestions.ts @@ -26,6 +26,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {Beta, CardFeeds, CardList, PersonalDetailsList, Policy} from '@src/types/onyx'; import type {VisibleReportActionsDerivedValue} from '@src/types/onyx/DerivedValues'; import type {SearchDataTypes} from '@src/types/onyx/SearchResults'; +import getEmptyArray from '@src/types/utils/getEmptyArray'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; @@ -67,11 +68,6 @@ type UseAutocompleteSuggestionsParams = { autocompleteSubstitutions?: SubstitutionMap; }; -// Stable empty result so the no-suggestions cases (notably the empty-query open) return the same -// reference every render, instead of a fresh [] that invalidates the consumer's `sections` memo and -// forces the list to re-render on every parent render. -const EMPTY_SUGGESTIONS: AutocompleteItemData[] = []; - // Static autocomplete lists derived from CONST values, computed once at module load const DATA_TYPE_VALUES = Object.values(CONST.SEARCH.DATA_TYPES); const GROUP_BY_FRIENDLY_VALUES = Object.values(CONST.SEARCH.GROUP_BY).map((value) => getUserFriendlyValue(value)); @@ -153,7 +149,8 @@ function useAutocompleteSuggestions({ } if (!autocompleteKey) { - return EMPTY_SUGGESTIONS; + // Returns the same array reference on every render, so the consumer's `sections` memo stays valid and the list doesn't re-render. + return getEmptyArray(); } const alreadyAutocompletedKeys = new Set( From f43ad17d5f6558704cf2bd5d6d441bfbc626166f Mon Sep 17 00:00:00 2001 From: sumo_slonik Date: Fri, 31 Jul 2026 12:14:46 +0200 Subject: [PATCH 3/3] Cover the SearchRouter open path with tests Add DeferredSearchAutocompleteListTest for the deferred wrapper: it keeps the skeleton until layout, mounts the list right after the skeleton lays out while a navigation transition is still pending, and falls back to the skeleton once the screen loses focus. Assert in useAutocompleteSuggestions that the empty-query path keeps one array reference across renders and across hook instances, which toEqual([]) could not catch. --- .../DeferredSearchAutocompleteListTest.tsx | 82 +++++++++++++++++++ .../hooks/useAutocompleteSuggestions.test.ts | 14 ++++ 2 files changed, 96 insertions(+) create mode 100644 tests/unit/Search/DeferredSearchAutocompleteListTest.tsx diff --git a/tests/unit/Search/DeferredSearchAutocompleteListTest.tsx b/tests/unit/Search/DeferredSearchAutocompleteListTest.tsx new file mode 100644 index 000000000000..bf159d55d5ec --- /dev/null +++ b/tests/unit/Search/DeferredSearchAutocompleteListTest.tsx @@ -0,0 +1,82 @@ +import {fireEvent, render, screen} from '@testing-library/react-native'; + +import DeferredSearchAutocompleteList from '@components/Search/DeferredSearchAutocompleteList'; +import type {SearchAutocompleteListProps} from '@components/Search/SearchAutocompleteList'; + +import TransitionTracker from '@libs/Navigation/TransitionTracker'; + +import type {LayoutChangeEvent} from 'react-native'; + +import React from 'react'; +import {View} from 'react-native'; + +// jest.mock factories can't reference imported bindings, but `mock`-prefixed locals are allowed. +const MockView = View; + +const mockIsFocusedUntilTransitionEnd = jest.fn(() => true); + +jest.mock('@hooks/useIsFocusedUntilTransitionEnd', () => ({ + __esModule: true, + default: () => mockIsFocusedUntilTransitionEnd(), +})); + +// Stand in for the two children so the test only exercises which one the wrapper picks. +jest.mock('@components/OptionsListSkeletonView', () => ({ + __esModule: true, + default: ({onLayout}: {onLayout?: (event: LayoutChangeEvent) => void}) => ( + + ), +})); + +jest.mock('@components/Search/SearchAutocompleteList', () => ({ + __esModule: true, + default: () => , +})); + +jest.mock('@libs/Navigation/TransitionTracker', () => ({ + runAfterTransitions: jest.fn(), +})); + +jest.mock('@libs/telemetry/activeSpans', () => ({ + endSpan: jest.fn(), +})); + +const mockedRunAfterTransitions = jest.mocked(TransitionTracker.runAfterTransitions); + +const listProps: SearchAutocompleteListProps = { + autocompleteQueryValue: '', + handleSearch: jest.fn(), + onListItemPress: jest.fn(), +}; + +function layOutSkeleton() { + fireEvent(screen.getByTestId('options-list-skeleton'), 'layout', {nativeEvent: {layout: {width: 300, height: 400, x: 0, y: 0}}}); +} + +beforeEach(() => { + jest.clearAllMocks(); + mockIsFocusedUntilTransitionEnd.mockReturnValue(true); + // A transition that never completes: anything gating on it would stay on the skeleton forever. + mockedRunAfterTransitions.mockReturnValue({cancel: jest.fn()}); +}); + +describe('DeferredSearchAutocompleteList', () => { + it('swaps the skeleton for the list on layout, without waiting for the navigation transition, and back on blur', () => { + const {rerender} = render(); + + expect(screen.queryByTestId('search-autocomplete-list')).not.toBeOnTheScreen(); + + layOutSkeleton(); + + expect(screen.getByTestId('search-autocomplete-list')).toBeOnTheScreen(); + expect(mockedRunAfterTransitions).not.toHaveBeenCalled(); + + mockIsFocusedUntilTransitionEnd.mockReturnValue(false); + rerender(); + + expect(screen.getByTestId('options-list-skeleton')).toBeOnTheScreen(); + }); +}); diff --git a/tests/unit/hooks/useAutocompleteSuggestions.test.ts b/tests/unit/hooks/useAutocompleteSuggestions.test.ts index 61027e145191..b5b66b54b581 100644 --- a/tests/unit/hooks/useAutocompleteSuggestions.test.ts +++ b/tests/unit/hooks/useAutocompleteSuggestions.test.ts @@ -139,6 +139,20 @@ describe('useAutocompleteSuggestions', () => { expect(result.current).toEqual([]); }); + it('keeps the same empty array reference across renders and hook instances', () => { + parseForAutocomplete.mockReturnValue({autocomplete: null, ranges: []}); + + // Each render builds a fresh params object, mirroring a parent re-render. + const {result: first, rerender} = renderHook(() => useAutocompleteSuggestions({...defaultParams, autocompleteQueryValue: ''})); + const firstResult = first.current; + rerender({}); + + const {result: second} = renderHook(() => useAutocompleteSuggestions({...defaultParams, autocompleteQueryValue: ''})); + + expect(first.current).toBe(firstResult); + expect(second.current).toBe(firstResult); + }); + it('returns tag suggestions when autocomplete key is tag', () => { parseForAutocomplete.mockReturnValue({ autocomplete: {key: CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG, value: 'eng'},