perf: Add accountIdByAddress to AccountsController state#7893
Merged
perf: Add accountIdByAddress to AccountsController state#7893
accountIdByAddress to AccountsController state#7893Conversation
accountIdByAddress to AccountsController stateaccountIdByAddress to AccountsController state
ccharly
reviewed
Feb 10, 2026
ccharly
reviewed
Feb 10, 2026
ccharly
reviewed
Feb 10, 2026
packages/assets-controllers/src/selectors/token-selectors.test.ts
Outdated
Show resolved
Hide resolved
packages/assets-controllers/src/selectors/token-selectors.test.ts
Outdated
Show resolved
Hide resolved
Contributor
Author
|
@metamaskbot publish-previews |
Contributor
|
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions. |
Contributor
Author
|
@metamaskbot publish-previews |
Contributor
|
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions. |
ccharly
reviewed
Mar 4, 2026
packages/assets-controllers/src/selectors/token-selectors.test.ts
Outdated
Show resolved
Hide resolved
ccharly
reviewed
Mar 5, 2026
ccharly
reviewed
Mar 5, 2026
Prithpal-Sooriya
approved these changes
Mar 5, 2026
cursor bot
pushed a commit
that referenced
this pull request
Mar 5, 2026
## Explanation Currently when doing an account lookup by address we're doing an O(n) operation in the clients, this has been improved to O(1) by adding an address -> account id mapping in state. The `getAccountByAddress` function now also takes advantage of this new piece of state to improve its lookup time. ## References N/A ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md) - [x] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Medium risk because it changes the public controller state shape (consumer migrations required) and alters address-lookup behavior with new caching/fallback logic that could surface mismatches if callers use non-normalized addresses. > > **Overview** > **Performance improvement:** `AccountsController` state now includes a derived `accountIdByAddress` (address → account id) map, marked *non-persisted* and excluded from state logs. > > `getAccountByAddress` is rewritten to use this map (with a lowercase fallback + debug logging on cache misses), and the map is constructed/maintained across constructor initialization, `updateAccounts`, `loadBackup`, and keyring add/remove patches. > > Tests and typings are updated accordingly (including `utils.constructAccountIdByAddress` tests and downstream state-shape updates such as `ProfileMetricsController` tests). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 903ebe3. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
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
Currently when doing an account lookup by address we're doing an O(n) operation in the clients, this has been improved to O(1) by adding an address -> account id mapping in state. The
getAccountByAddressfunction now also takes advantage of this new piece of state to improve its lookup time.References
N/A
Checklist
Note
Medium Risk
Medium risk because it changes the public controller state shape (consumer migrations required) and alters address-lookup behavior with new caching/fallback logic that could surface mismatches if callers use non-normalized addresses.
Overview
Performance improvement:
AccountsControllerstate now includes a derivedaccountIdByAddress(address → account id) map, marked non-persisted and excluded from state logs.getAccountByAddressis rewritten to use this map (with a lowercase fallback + debug logging on cache misses), and the map is constructed/maintained across constructor initialization,updateAccounts,loadBackup, and keyring add/remove patches.Tests and typings are updated accordingly (including
utils.constructAccountIdByAddresstests and downstream state-shape updates such asProfileMetricsControllertests).Written by Cursor Bugbot for commit 903ebe3. This will update automatically on new commits. Configure here.