perf: throttle perps PnL price ticks and gate Polymarket WS on focus - #34823
perf: throttle perps PnL price ticks and gate Polymarket WS on focus#34823javiergarciavera wants to merge 1 commit into
Conversation
Two JS-thread cost reductions identified via CPU profile analysis:
1. usePerpsLivePositions: add pnlThrottleMs option (default 1000ms)
Price updates for live PnL were sharing the position subscription's
throttleMs=0, so setPriceData({ ...prev, ...newPrices }) ran on every
raw Hyperliquid WS tick, creating short-lived merged objects and
triggering frequent GC. Separate throttle limits the merge to ≤1/s,
matching usePerpsLivePrices' own default.
2. useLiveMarketPrices / useLiveCryptoPrices: gate on useIsFocused()
Polymarket WebSocket callbacks (parseMarketMessageData → JSON.parse)
were running on the JS main thread during perps sessions because React
Navigation keeps tab-mounted components alive. Adding useIsFocused()
stops subscriptions when the user is off the Predict tab and
re-establishes them instantly on return (WS connection stays alive).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
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. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 66fc6eb. Configure here.
| isMountedRef.current = true; | ||
|
|
||
| if (!symbol) { | ||
| if (!symbol || !isFocused) { |
There was a problem hiding this comment.
Focus pause triggers false stream failure
Medium Severity
Gating useLiveCryptoPrices on useIsFocused() stops ticks while Predict tabs stay mounted, but useCryptoUpDownChartData still treats a quiet stream as failure. After the stale timeout it resumes HTTP polling in the background, then marks connectionError, so returning to Predict can show a false disconnect UI until ticks resume.
Reviewed by Cursor Bugbot for commit 66fc6eb. Configure here.
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection: The PR contains performance optimization changes to live data streaming hooks in Perps and Predict features:
Tag selection:
Performance Test Selection: |
|
|
| Platform | Device | Reason | Recording |
|---|---|---|---|
| Android | Google Pixel 8 Pro (v14.0) | no_performance_metrics | 📹 Watch |
🔬 App profiling check · Current run 31818134286 · Baseline (last run on main (scenario also failing)) run 30897750395 @ 67486d2
⚠️ No green baseline onmain— comparing against the latest usable profiling.
Summary:
ℹ️ API calls unavailable:
Network logs API error: Bad Request
Full metric table (+10% variance rules)
Disclaimer — allowed variance: a +10% margin over the baseline is permitted.
- If
Current <= Baseline + 10%, treated as acceptable noise.- If
Current > Baseline + 10%, Current and variance % are highlighted with⚠️ .
| Metric | Baseline | Current | Δ |
|---|---|---|---|
| CPU avg | 10.44% | 7.61% | -2.83 (-27.1%) |
| CPU max | 18.83% | 22.13% | +3.3 (+17.5%) |
| Memory avg | 555.64 MB | 836.28 MB | +280.64 (+50.5%) |
| Memory max | 609.03 MB | 999.98 MB | +390.95 (+64.2%) |
| Slow frames | 5.16% | 9% | +3.84 (+74.4%) |
| Frozen frames | 0% | 0% | 0 (0%) |
| ANRs | 0 | 0 | 0 (0%) |
| Issues | 2 | 2 | 0 (0%) |
| Critical issues | 1 | 1 | 0 (0%) |
| App size | 328.67 MB | 329.45 MB | +0.78 (+0.2%) |
✅ Passed Tests (4)
| Test | Platform | Device | Duration | Team | Recording |
|---|---|---|---|---|---|
| Predict Available Balance - Complete Flow Performance | Android | Google Pixel 8 Pro (v14.0) | 0.73s | @team-predict | 📹 Watch |
| Predict Deposit - Complete Flow Performance | Android | Google Pixel 8 Pro (v14.0) | 11.67s | @team-predict | 📹 Watch |
| Perps add funds | Android | Google Pixel 8 Pro (v14.0) | 9.07s | @mm-perps-engineering-team | 📹 Watch |
| Predict Market Details - Complete Flow Performance | Android | Google Pixel 8 Pro (v14.0) | 5.89s | @team-predict | 📹 Watch |
Branch: perf/perps-ws-throttle-and-predict-focus-gate · Build: E2E · Commit: 1327016 · View full run





Description
Updates
@metamask/perps-controllerfrom v11 to v12 and adapts Mobile to the controller's new order types and error codes. Close-position flows remain restricted to ordinary market and limit orders, while the new TWAP, scale, and chase validation errors are mapped to user-facing English messages.The change also updates Perps controller fixtures for the v12
proLayoutPreferencesstate and adds default Hyperliquid API mocks required by E2E initialization, including a validuserAbstractionresponse.Changelog
CHANGELOG entry: Updated Perps with the latest trading engine and clearer errors for advanced order types
Related issues
Fixes: https://consensyssoftware.atlassian.net/browse/TAT-3761
Manual testing steps
N/A — this dependency integration has no intentional visual changes. The affected behavior is covered by Perps error-translation tests, type checking, and E2E fixture validation.
Screenshots/Recordings
N/A — no intentional visual changes.
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