Repository navigation
perf(perps): cache filterAndSortMarkets per market snapshot - #38817
Conversation
Opening the market picker mounts three usePerpsMarkets instances (the list view plus two in the category badges). Each one filtered and sorted the same cached snapshot on mount and again in its subscribe callback. Cache the result per input array and filter settings. Repeat calls return the same array, so setMarkets bails out and the list view skips a second sort before the sheet starts moving. Old snapshots are freed when the stream replaces them.
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
Smart E2E Test SelectionSelected E2E tagsSmokePerps AI Confidence: 82 E2E reasoningExpand to readThe PR changes are:
No UI components, Redux state, navigation, or E2E-visible behavior was modified. The changes are purely additive (new tests + performance optimization via memoization). The
These consumers feed into the Perps UI (SmokePerps) and the Trending tab (SmokeWalletPlatform). While the risk is low (memoization is transparent to callers), running SmokePerps and SmokeWalletPlatform provides adequate E2E coverage to confirm the caching layer doesn't introduce any reference-equality issues that could affect market display in production flows. Per SmokePerps tag description, SmokeWalletPlatform is also required (Perps is a section inside Trending). SmokeConfirmations is not selected because no transaction confirmation flows are exercised by these utility changes. |
|



Description
Opening the Perps market picker runs about 111 ms of JS before the sheet can start to move (average of 10 opens, 8.20.0 RC 7579, iPhone 14, MetaMetrics off). Part of that is the same market data being filtered and sorted several times.
Three
usePerpsMarketsinstances mount with the picker: the list view's, plus one each inusePerpsCategoriesanduseHasNewMarketsinside the category badges. Each one ranfilterAndSortMarketson the same cached snapshot in itsuseStateinitializer, then again in its subscribe callback, which the stream calls immediately with that same cached array. The callback then calledsetMarketswith a new array, so the list view ransortMarketsa second time before the sheet moved.This caches
filterAndSortMarketsresults per input array in aWeakMap, with one entry pershowZeroVolume/showZeroOpenInterestpair. A repeat call returns the same array, sosetMarketsbails out and the list view skips the second sort. A new snapshot gets a new entry, and the old entry is freed with the old array. On a cache miss the behaviour is unchanged.Callers now share one result array. No caller sorts or edits it in place: Core's
sortMarketscopies first, and the other sorts on market data run on.filter()/.slice()/spread copies.Changelog
CHANGELOG entry: null
Related issues
Fixes: TAT-4028
Refs: TAT-4444
Manual testing steps
Screenshots/Recordings
Before
N/A
After
N/A
Pre-merge author checklist
Performance checks (if applicable)
trace()for usage andaddTokenfor an exampleFor performance guidelines and tooling, see the Performance Guide.
Pre-merge reviewer checklist
Note
Low Risk
Localized performance change to Perps market list filtering; output semantics are unchanged and callers are documented not to mutate cached arrays in place.
Overview
Adds memoization to
filterAndSortMarketsso repeated calls with the same market snapshot array and filter flags return the same result array reference, instead of filtering and sorting again.The implementation moves the work into
computeFilteredAndSortedMarketsand stores results in aWeakMapkeyed by the input array, with a per-options cache (showZeroVolume/showZeroOpenInterest). A new snapshot array gets a fresh cache entry; when the old array is dropped, the cache entry is collected.That stable reference lets multiple
usePerpsMarketsmounts (market picker list, categories, new-market badges) share one filtered list on the same channel snapshot, and avoids extra React updates when the subscription immediately re-delivers the snapshot used in initial state. Tests cover cache hits/misses and hook behavior with a shared snapshot.Reviewed by Cursor Bugbot for commit 0dca462. Bugbot is set up for automated code reviews on this repo. Configure here.