go-ethereum/accounts/keystore
Alexander Yastrebov a00430d4aa accounts/keystore: use map instead of slice to keep all accounts
After profiling geth startup times for #16874
it turned out that most of the time was spent resizing accounts slice
for ordered insertion.

This change uses map to track all accounts instead of slice to improve performance.

geth startup time using keystore with one million accounts reduced from
23 minutes to 2.5 minutes.

Benchmarks show relatively-small overhead increase for small keystores and large
decrease for huge:
```
goos: linux
goarch: amd64
pkg: github.com/ethereum/go-ethereum/accounts/keystore
                                  │      HEAD~1       │                  HEAD                   │
                                  │      sec/op       │    sec/op      vs base                  │
Add/preload=10-8                         1.030µ ±  5%    1.447µ ±  7%    +40.51% (p=0.000 n=10)
Add/preload=100-8                        1.109µ ±  4%    1.463µ ±  3%    +31.88% (p=0.000 n=10)
Add/preload=1000-8                       1.860µ ±  3%    1.477µ ±  5%    -20.57% (p=0.000 n=10)
Add/preload=1000000-8                 5177.640µ ±  2%    1.654µ ± 11%    -99.97% (p=0.000 n=10)
Find/preload=10/by_address-8             23.70n ±  1%    23.88n ±  3%          ~ (p=0.271 n=10)
Find/preload=10/by_path-8                50.43n ±  2%    39.88n ±  5%    -20.94% (p=0.000 n=10)
Find/preload=10/ambiguous-8              323.6n ±  1%   1049.0n ±  3%   +224.17% (p=0.000 n=10)
Find/preload=100/by_address-8            23.69n ±  1%    23.63n ±  6%          ~ (p=0.739 n=10)
Find/preload=100/by_path-8              362.70n ±  1%    37.84n ±  3%    -89.57% (p=0.000 n=10)
Find/preload=100/ambiguous-8             2.683µ ±  2%   19.235µ ±  2%   +617.05% (p=0.000 n=10)
Find/preload=1000/by_address-8           26.45n ±  1%    27.73n ±  2%     +4.82% (p=0.000 n=10)
Find/preload=1000/by_path-8            3211.00n ±  3%    38.22n ±  8%    -98.81% (p=0.000 n=10)
Find/preload=1000/ambiguous-8            26.14µ ±  2%   263.59µ ±  1%   +908.41% (p=0.000 n=10)
Find/preload=1000000/by_address-8        26.47n ±  4%    26.41n ±  1%          ~ (p=0.566 n=10)
Find/preload=1000000/by_path-8      3683325.50n ±  4%    44.09n ± 45%   -100.00% (p=0.000 n=10)
Find/preload=1000000/ambiguous-8         39.68m ± 14%   819.48m ±  7%  +1965.01% (p=0.000 n=10)
geomean                                  2.346µ          791.4n          -66.27%

                                  │     HEAD~1     │                   HEAD                    │
                                  │      B/op      │      B/op       vs base                   │
Add/preload=10-8                      643.0 ± 0%        662.0 ±  0%    +2.95% (p=0.000 n=10)
Add/preload=100-8                     643.0 ± 0%        662.0 ±  0%    +2.95% (p=0.000 n=10)
Add/preload=1000-8                    584.0 ± 5%        662.0 ±  0%   +13.36% (p=0.000 n=10)
Add/preload=1000000-8                 88.00 ± 0%       662.00 ± 17%  +652.27% (p=0.000 n=10)
Find/preload=10/by_address-8          0.000 ± 0%        0.000 ±  0%         ~ (p=1.000 n=10) ¹
Find/preload=10/by_path-8             0.000 ± 0%        0.000 ±  0%         ~ (p=1.000 n=10) ¹
Find/preload=10/ambiguous-8           624.0 ± 0%       1200.0 ±  0%   +92.31% (p=0.000 n=10)
Find/preload=100/by_address-8         0.000 ± 0%        0.000 ±  0%         ~ (p=1.000 n=10) ¹
Find/preload=100/by_path-8            0.000 ± 0%        0.000 ±  0%         ~ (p=1.000 n=10) ¹
Find/preload=100/ambiguous-8        6.047Ki ± 0%     12.047Ki ±  0%   +99.22% (p=0.000 n=10)
Find/preload=1000/by_address-8        0.000 ± 0%        0.000 ±  0%         ~ (p=1.000 n=10) ¹
Find/preload=1000/by_path-8           0.000 ± 0%        0.000 ±  0%         ~ (p=1.000 n=10) ¹
Find/preload=1000/ambiguous-8       56.05Ki ± 0%     112.05Ki ±  0%   +99.92% (p=0.000 n=10)
Find/preload=1000000/by_address-8     0.000 ± 0%        0.000 ±  0%         ~ (p=1.000 n=10) ¹
Find/preload=1000000/by_path-8        0.000 ± 0%        0.000 ±  0%         ~ (p=1.000 n=10) ¹
Find/preload=1000000/ambiguous-8    53.41Mi ± 0%     106.81Mi ±  0%  +100.00% (p=0.000 n=10)
geomean                                          ²                    +36.09%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                                  │    HEAD~1    │                 HEAD                  │
                                  │  allocs/op   │ allocs/op   vs base                   │
Add/preload=10-8                    3.000 ± 0%     4.000 ± 0%   +33.33% (p=0.000 n=10)
Add/preload=100-8                   3.000 ± 0%     4.000 ± 0%   +33.33% (p=0.000 n=10)
Add/preload=1000-8                  3.000 ± 0%     4.000 ± 0%   +33.33% (p=0.000 n=10)
Add/preload=1000000-8               2.000 ± 0%     4.000 ± 0%  +100.00% (p=0.000 n=10)
Find/preload=10/by_address-8        0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=10) ¹
Find/preload=10/by_path-8           0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=10) ¹
Find/preload=10/ambiguous-8         2.000 ± 0%     3.000 ± 0%   +50.00% (p=0.000 n=10)
Find/preload=100/by_address-8       0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=10) ¹
Find/preload=100/by_path-8          0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=10) ¹
Find/preload=100/ambiguous-8        2.000 ± 0%     3.000 ± 0%   +50.00% (p=0.000 n=10)
Find/preload=1000/by_address-8      0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=10) ¹
Find/preload=1000/by_path-8         0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=10) ¹
Find/preload=1000/ambiguous-8       2.000 ± 0%     3.000 ± 0%   +50.00% (p=0.000 n=10)
Find/preload=1000000/by_address-8   0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=10) ¹
Find/preload=1000000/by_path-8      0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=10) ¹
Find/preload=1000000/ambiguous-8    2.000 ± 0%     3.000 ± 0%   +50.00% (p=0.000 n=10)
geomean                                        ²                +21.97%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean
```

Updates #16874
2025-09-26 18:29:20 +02:00
..
testdata cmd/geth: remove unlock commandline flag (#30737) 2024-11-15 10:15:15 +01:00
account_cache.go accounts/keystore: use map instead of slice to keep all accounts 2025-09-26 18:29:20 +02:00
account_cache_test.go accounts/keystore: introduce accountCache.add and accountCache.find benchmarks 2025-09-26 18:24:11 +02:00
file_cache.go all: use github.com/deckarep/golang-set/v2 (generic set) (#26159) 2022-11-14 15:16:52 +01:00
key.go all: replace uses of ioutil with io and os (#24869) 2022-05-16 11:59:35 +02:00
keystore.go accounts/keystore: use ticker to avoid timer allocations (#32732) 2025-09-24 19:47:55 -06:00
keystore_fuzzing_test.go tests/fuzzers: move fuzzers into native packages (#28467) 2023-11-14 14:34:29 +01:00
keystore_test.go accounts/keystore: fix typos in comments (#29336) 2024-03-27 13:16:29 +01:00
passphrase.go accounts/keystore: update links to documenation (#32194) 2025-07-14 09:15:18 +02:00
passphrase_test.go accounts: run tests in parallel (#28544) 2023-12-04 14:55:06 +01:00
plain.go accounts/keystore: delete the redundant keystore in filename (#17930) 2018-11-29 12:04:56 +02:00
plain_test.go accounts: run tests in parallel (#28544) 2023-12-04 14:55:06 +01:00
presale.go accounts/keystore: use github.com/google/uuid (#22217) 2021-02-26 15:28:34 +01:00
wallet.go accounts/keystore: add missing function doc for SignText (#21914) 2020-11-30 09:03:24 +01:00
watch.go accounts, cmd: fix typos (#28526) 2023-11-20 08:44:05 +01:00
watch_fallback.go accounts/keystore: faster tests (#25827) 2022-10-12 10:53:01 +02:00