fix(assets-controller): avoid recomputing getStateForTransactionPay on every transaction state change#9546
Merged
Conversation
salimtb
reviewed
Jul 20, 2026
| params.nativeAssetIdentifiers, | ||
| ) | ||
| ) { | ||
| return lastCall.result; |
Contributor
There was a problem hiding this comment.
nit / optional : Cache hits now return the same object reference across calls (previously fresh each time). Since this is a public export, a consumer mutating the result would poison the cache. Worth freezing it before caching:
lastCall = { params, result: Object.freeze(result) };
salimtb
reviewed
Jul 20, 2026
Comment on lines
+68
to
+100
| function areAccountsEqual( | ||
| a: AccountForLegacyFormat[], | ||
| b: AccountForLegacyFormat[], | ||
| ): boolean { | ||
| if (a === b) { | ||
| return true; | ||
| } | ||
| if (a.length !== b.length) { | ||
| return false; | ||
| } | ||
| return a.every( | ||
| (account, index) => | ||
| account.id === b[index].id && account.address === b[index].address, | ||
| ); | ||
| } | ||
|
|
||
| function areRecordsShallowEqual( | ||
| a: Record<string, unknown> | undefined, | ||
| b: Record<string, unknown> | undefined, | ||
| ): boolean { | ||
| if (a === b) { | ||
| return true; | ||
| } | ||
| if (!a || !b) { | ||
| return false; | ||
| } | ||
| const aKeys = Object.keys(a); | ||
| const bKeys = Object.keys(b); | ||
| if (aKeys.length !== bKeys.length) { | ||
| return false; | ||
| } | ||
| return aKeys.every((key) => a[key] === b[key]); | ||
| } |
Contributor
There was a problem hiding this comment.
Optional: lodash is already a dep, so both helpers could just be isEqual (deep, but inputs are tiny/flat so equivalent):
isEqual(lastCall.params.accounts, params.accounts)
isEqual(lastCall.params.nativeAssetIdentifiers, params.nativeAssetIdentifiers)Only for these rebuilt inputs though — keep the big state slices reference-compared, and avoid _.memoize (unbounded cache).
Contributor
|
nice one , this is a good perf improvement |
Comment on lines
+139
to
+149
| lastCall?.params.assetsBalance === params.assetsBalance && | ||
| lastCall.params.assetsInfo === params.assetsInfo && | ||
| lastCall.params.assetsPrice === params.assetsPrice && | ||
| lastCall.params.selectedCurrency === params.selectedCurrency && | ||
| lastCall.params.networkConfigurationsByChainId === | ||
| params.networkConfigurationsByChainId && | ||
| areAccountsEqual(lastCall.params.accounts, params.accounts) && | ||
| areRecordsShallowEqual( | ||
| lastCall.params.nativeAssetIdentifiers, | ||
| params.nativeAssetIdentifiers, | ||
| ) |
Contributor
There was a problem hiding this comment.
Lets move this into a small util: isTransactionPayParamsIdentical()
…sEqual, freeze cached result
salimtb
previously approved these changes
Jul 20, 2026
…-state-for-transaction-pay # Conflicts: # packages/assets-controller/CHANGELOG.md # packages/assets-controller/src/index.ts # packages/assets-controller/src/utils/formatStateForTransactionPay.test.ts # packages/assets-controller/src/utils/formatStateForTransactionPay.ts
salimtb
approved these changes
Jul 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explanation
formatStateForTransactionPayconverts the full AssetsController state into the legacy five-controller shape for transaction-pay-controller, runningtoChecksumAddress(keccak256) for every asset andparseCaipAssetTypefor every asset ID on every call. It is exposed as theAssetsController:getStateForTransactionPaymessenger action, which transaction-pay-controller invokes on everyTransactionController:stateChange— many times during a single transaction approval, and at least twice per event through messenger delegation — while its inputs only change when the assets pipeline updates.In a CPU profile of MetaMask Mobile (RC) captured while triggering a transaction approval in the MetaMask Pay deposit flow, keccak256 via
toChecksumAddressaccounted for 10.4% of all samples (~23% of active CPU), the single largest active bucket, with GC pauses (4.1%) as a symptom of the per-event object churn. See #9545 for the full breakdown.This change memoizes the last result on input identity: the
assetsBalance/assetsInfo/assetsPricestate slices andnetworkConfigurationsByChainIdare compared by reference (BaseController state updates are immutable, so unchanged slices retain identity),selectedCurrencyby value, and the per-call-rebuiltaccountsarray andnativeAssetIdentifiersrecord element-wise. Repeat calls with unchanged inputs return the cached object; any real input change recomputes. The conversion body is unchanged, moved to an internalcomputeStateForTransactionPay.MetaMask Mobile currently carries this fix as a Yarn patch (MetaMask/metamask-mobile#33466), which can be dropped once this lands and a release is picked up.
References
Checklist
Note
Low Risk
Performance-only change on a read path; freezing the legacy state object could affect consumers that mutate the return value, though transaction-pay is expected to treat it as read-only.
Overview
Reduces CPU during transaction approval by keeping the last
formatStateForTransactionPayresult when inputs are unchanged, soAssetsController:getStateForTransactionPaydoes not repeat expensivetoChecksumAddress/ CAIP work on everyTransactionController:stateChange.Memoization compares immutable controller slices (
assetsBalance,assetsInfo,assetsPrice,networkConfigurationsByChainId) by reference and rebuiltaccounts/nativeAssetIdentifierswith lodashisEqual. Parameter equality is centralized inisTransactionPayParamsIdentical. Cached outputs areObject.freezed so in-place mutations cannot poison the cache for later callers.Tests expand the memoization suite (cache hits, invalidation on balance/accounts/currency changes, frozen results, cache vs recompute parity). Changelog documents the freeze behavior alongside the broader memoization work.
Reviewed by Cursor Bugbot for commit 186e8f3. Bugbot is set up for automated code reviews on this repo. Configure here.