Skip to content

perf(perps): cache filterAndSortMarkets per market snapshot - #38817

Merged
geositta merged 1 commit into
mainfrom
TAT-4028-mobile-reduce-market-picker-js-cost
Nov 6, 2026
Merged

geositta merged 1 commit into
mainfrom
TAT-4028-mobile-reduce-market-picker-js-cost

Conversation

@Bigshmow

@Bigshmow Bigshmow commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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 usePerpsMarkets instances mount with the picker: the list view's, plus one each in usePerpsCategories and useHasNewMarkets inside the category badges. Each one ran filterAndSortMarkets on the same cached snapshot in its useState initializer, then again in its subscribe callback, which the stream calls immediately with that same cached array. The callback then called setMarkets with a new array, so the list view ran sortMarkets a second time before the sheet moved.

This caches filterAndSortMarkets results per input array in a WeakMap, with one entry per showZeroVolume/showZeroOpenInterest pair. A repeat call returns the same array, so setMarkets bails 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 sortMarkets copies 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

Feature: Perps market picker

  Scenario: the market list is unchanged
    Given the user has Perps enabled and has opened a market
    When the user opens the market picker from the chart header
    Then markets are sorted by volume, highest first
    And markets with zero or invalid volume or open interest are not listed

  Scenario: category badges match the list
    Given the market picker is open
    When the user taps each category badge in turn
    Then the list shows only markets in that category

  Scenario: new market data is not served from the cache
    Given the market picker is open
    When the market data refreshes with changed volumes
    Then the list order reflects the new volumes

Screenshots/Recordings

Before

N/A

After

N/A

Pre-merge author checklist

Performance checks (if applicable)

  • I've tested on Android
    • Ideally on a mid-range device; emulator is acceptable
  • I've tested with a power user scenario
    • Use these power-user SRPs to import wallets with many accounts and tokens
  • I've instrumented key operations with Sentry traces for production performance metrics

For performance guidelines and tooling, see the Performance Guide.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

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 filterAndSortMarkets so 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 computeFilteredAndSortedMarkets and stores results in a WeakMap keyed 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 usePerpsMarkets mounts (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.

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.
@Bigshmow Bigshmow added the team-perps Perps team label Nov 4, 2026
@github-actions github-actions Bot added the size-M label Nov 4, 2026
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

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.

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Smart E2E Test Selection

Selected E2E tags

SmokePerps
SmokeWalletPlatform

AI Confidence: 82

E2E reasoning

Expand to read

The PR changes are:

  1. filterAndSortMarkets.ts — adds a WeakMap-based memoization cache to the existing filterAndSortMarkets utility. The filtering/sorting logic itself is unchanged; only a caching wrapper is added.
  2. filterAndSortMarkets.test.ts — 84 lines of new unit tests covering the caching behavior.
  3. usePerpsMarkets.test.ts — 50 lines of new unit tests for hook reference stability and per-instance filter options.

No UI components, Redux state, navigation, or E2E-visible behavior was modified. The changes are purely additive (new tests + performance optimization via memoization).

The filterAndSortMarkets utility is consumed by:

  • usePerpsMarkets.ts (Perps market list hook)
  • useTradablePerpsMarketSymbols.ts (WhatsHappening/Trending)
  • tradableAssets.ts (WhatsHappening/Trending)
  • usePerpsTrendingCarouselData.ts (Homepage Perpetuals section)

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.

View run

@Bigshmow
Bigshmow marked this pull request as ready for review November 4, 2026 02:58
@Bigshmow
Bigshmow requested a review from a team as a code owner November 4, 2026 02:58
@sonarqubecloud

sonarqubecloud Bot commented Nov 4, 2026

Copy link
Copy Markdown

@geositta
geositta added this pull request to the merge queue Nov 6, 2026
Merged via the queue into main with commit 8b6f213 Nov 6, 2026
147 checks passed
@geositta
geositta deleted the TAT-4028-mobile-reduce-market-picker-js-cost branch November 6, 2026 03:24
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 6, 2026
@metamask-ci metamask-ci Bot added the release-8.21.0 Issue or pull request that will be included in release 8.21.0 label Nov 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-8.21.0 Issue or pull request that will be included in release 8.21.0 size-M team-perps Perps team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants