fix(assets-controller): avoid recomputing getStateForTransactionPay on every transaction state change#9546
Open
Kureev wants to merge 2 commits into
Open
fix(assets-controller): avoid recomputing getStateForTransactionPay on every transaction state change#9546Kureev wants to merge 2 commits into
Kureev wants to merge 2 commits into
Conversation
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 with identical output when inputs change; single-entry module cache is appropriate for the hot path but assumes callers pass stable slice references as today.
Overview
formatStateForTransactionPay(used byAssetsController:getStateForTransactionPay) now memoizes the last result when inputs are unchanged, so repeated calls duringTransactionController:stateChangeno longer redo the full legacy conversion (including per-assettoChecksumAddress/ keccak256).Cache hits compare state slices by reference (
assetsBalance,assetsInfo,assetsPrice,networkConfigurationsByChainId),selectedCurrencyby value, and accounts /nativeAssetIdentifierswith shallow equality; any real change still runscomputeStateForTransactionPay. The conversion logic is unchanged, only split behind the cache layer.Exports the new
FormatStateForTransactionPayParamstype and adds memoization unit tests plus a changelog entry.Reviewed by Cursor Bugbot for commit ed31a9a. Bugbot is set up for automated code reviews on this repo. Configure here.