MetaMask / MetaMask/metamask-extension

[Bug]: adding accounts has O(n^2) complexity

Open
#14,220 0 comments 1 reaction 0 assignees View on GitHub
external-contributor INVALID-ISSUE-TEMPLATE type-discussion
Dominant language
TypeScript
Stars
13.2k
Forks
5.6k
Avg merge
2d 5h
Merged PRs (30d)
451

Description

### Describe the bug

Hi there,

I was curious to test whether Metamask is scalable with regard to the amount of accounts one can have in his wallet.
After modifying the E2E tests, I logged the time it took to create an account. To my surprise, the time it takes to create an account scales linearly. (Creating account number 80 takes more than 20 seconds on my M1 mac!)

Below you can see the cumulative time it takes to create n wallets.

![Screenshot 2022-03-28 at 10-43-42 Essay 4](https://user-images.githubusercontent.com/10885710/160360608-4003e53d-b803-4897-b0bc-d96a0876db0a.png)

I attempted to figure out why this is the case, and started out at the HD Keyring protocol. The deriveChild() call however seems to be an O(1) operation.
Perhaps someone from the team can elaborate on why this addNewAccount operation is so expensive? My current idea is that it has to do with sequential account information fetching.

### Steps to reproduce

1. Change the E2E test in test/e2e/tests/add-account.spec.js to generate n accounts in stead of just 1.
2. After every account creation, log the time it took.

### Error messages or log output

```shell
wallet,time
0, 2072
1, 1852
2, 2119
3, 2660
4, 2854
5, 2979
6, 3247
7, 3383
8, 3763
9, 3929
10, 4329
11, 4647
12, 4915
13, 5067
14, 5346
15, 5438
16, 5829
17, 6133
18, 6308
19, 6508
20, 6765
21, 7114
22, 7463
23, 7612
24, 7615
25, 7787
26, 7994
27, 8215
28, 8579
29, 9050
30, 8977
31, 9207
32, 9399
33, 9685
34, 10106
35, 10480
36, 10416
37, 10511
38, 10821
39, 11171
40, 11679
41, 11864
42, 12089
43, 12526
44, 12310
45, 12579
46, 12759
47, 13138
48, 13303
49, 14109
50, 14209
51, 14469
52, 14257
53, 14375
54, 14629
55, 15280
56, 15079
57, 15365
58, 15678
59, 16764
60, 16514
61, 16877
62, 16511
63, 16708
64, 17526
65, 17335
66, 17599
67, 17869
68, 18134
69, 18639
70, 19161
71, 19078
72, 18827
73, 19245
74, 19571
75, 19471
76, 19723
77, 19985
78, 20561
79, 20556
80, 20778
```

### Version

10.11.4

### Build type

Beta

### Browser

Firefox

### Operating system

MacOS

### Hardware wallet

Other (please elaborate in the "Additional Context" section)

### Additional context

Issue [#13243](https://github.com/MetaMask/metamask-extension/issues/13243) could be relevant to this issue, as this person also has a large number of accounts in his wallet.

Contributor guide

Open the contributing guide

Research direction

Start with test/e2e/tests/add-account.spec.js and reproduce the account-creation timing across increasing account counts. Trace the addNewAccount flow and account information fetching to locate the source of the scaling cost. Done means account creation no longer shows the reported worsening performance and the E2E test can demonstrate the improvement.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.