MetaMask / MetaMask/metamask-mobile

SwapsController persistence opperation delay

Open
#17,719 0 comments 0 reactions 0 assignees View on GitHub
area-performance INVALID-ISSUE-TEMPLATE team-swaps-and-bridge
Dominant language
TypeScript
Stars
3k
Forks
1.7k
Avg merge
1d 14h
Merged PRs (30d)
669

Description

## What is this about?

Performance optimization for MetaMask's SwapsController and persistence operations that are causing significant delays in user interactions.

## Scenario

Users experience slow response times when:
- Opening token details (300-600ms delay)
- Any persistence operations, especially with multiple accounts (~300ms per operation)

The root causes are:
1. `fetchTokenWithCache` function in SwapsController taking 300-600ms, primarily due to `immer` overhead in `this.update` calls
2. Slow persistence operations caused by processing the large `engine` field from state

## Design

NA

## Technical Details

**Current Issues:**
- `@metamask/swaps-controller/dist/SwapsController.js:425` - `fetchTokenWithCache` function performance bottleneck
- `immer` processing in `this.update` consuming majority of execution time
- Persistence operations scaling poorly with account count due to `engine` field processing

**Proposed Solutions:**
1. **SwapsController Optimization:**
- Implement selective state updates to avoid full `immer` processing
- Cache token data more efficiently to reduce redundant API calls
- Consider lazy loading or background fetching strategies

2. **Persistence Optimization:**
- Exclude or minimize `engine` field processing during persistence
- Implement incremental state persistence
- Add state segmentation to avoid processing unnecessary data

3. **General Improvements:**
- Add performance monitoring to track improvements
- Implement batching for multiple rapid state changes
- Consider using more efficient state management patterns for frequently updated data

## Threat Modeling Framework

- **Availability:** Performance issues affect user experience and app responsiveness
- **Integrity:** Ensure optimizations don't compromise data consistency
- **Confidentiality:** No security implications expected from performance improvements

## Acceptance Criteria

- [ ] `fetchTokenWithCache` execution time reduced to <100ms
- [ ] Persistence operations complete in <100ms regardless of account count
- [ ] Token detail opening feels instantaneous to users
- [ ] All existing functionality remains intact
- [ ] Performance improvements verified through automated tests
- [ ] No regressions in swap functionality
- [ ] Memory usage doesn't increase significantly

## Stakeholder review needed before the work gets merged

- [x] Engineering (needed in most cases)
- [ ] Design
- [x] Product
- [x] QA (automation tests are required to pass before merging PRs but not all changes are covered by automation tests - please review if QA is needed beyond automation tests)
- [ ] Security
- [ ] Legal
- [ ] Marketing
- [ ] Management (please specify)
- [ ] Other (please specify)

## References

- SwapsController source: `@metamask/swaps-controller/dist/SwapsController.js:425`
- Related to token detail view performance
- Immer documentation for optimization strategies
- Performance profiling data showing 300-600ms delays

Contributor guide

Open the contributing guide

Research direction

Start by profiling fetchTokenWithCache in @metamask/swaps-controller/dist/SwapsController.js:425 and reviewing the reported Immer and persistence delays. Compare the results against the proposed persistence and state-update areas, then verify the acceptance criteria with automated performance and swap regression tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.