cosmos-sdk/x/bank/types
Emmanuel T Odeke dbb9923917
store/cachekv, x/bank/types: algorithmically fix pathologically slow code (#8719)
After continuously profiling InitGensis with 100K accounts, it showed
pathologically slow code, that was the result of a couple of patterns:
* Unconditional and not always necessary map lookups
* O(n^2) sdk.AccAddressFromBech32 retrievals when the code is expensive,
during a quicksort

The remedy involved 4 parts:
* O(n) sdk.AccAddressFromBech32 invocations, down from O(n^2) in the quicksort
* Only doing map lookups when the domain key check has passed
* Using a black magic compiler technique of the map clearing idiom
* Zero allocation []byte<->string conversion

With 100K accounts, this brings InitGenesis down to ~6min, instead of
20+min, it reduces the sort code from ~7sec down to 50ms.

Also some simple benchmark reflect the change:
```shell
name                    old time/op    new time/op    delta
SanitizeBalances500-8     19.3ms ±10%     1.5ms ± 5%  -92.46%  (p=0.000 n=20+20)
SanitizeBalances1000-8    41.9ms ± 8%     3.0ms ±12%  -92.92%  (p=0.000 n=20+20)

name                    old alloc/op   new alloc/op   delta
SanitizeBalances500-8     9.05MB ± 6%    0.56MB ± 0%  -93.76%  (p=0.000 n=20+18)
SanitizeBalances1000-8    20.2MB ± 3%     1.1MB ± 0%  -94.37%  (p=0.000 n=20+19)

name                    old allocs/op  new allocs/op  delta
SanitizeBalances500-8      72.4k ± 6%      4.5k ± 0%  -93.76%  (p=0.000 n=20+20)
SanitizeBalances1000-8      162k ± 3%        9k ± 0%  -94.40%  (p=0.000 n=20+20)
```

The CPU profiles show the radical change as per
https://github.com/cosmos/cosmos-sdk/issues/7766#issuecomment-786671734

Later on, we shall do more profiling and fixes but for now this brings
down the run-time for InitGenesis.

Fixes #7766

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
2021-02-27 07:26:22 -08:00
..
authz.pb.go x/authz: Add DelegateAuthorization, UndelegateAuthorization (#8472) 2021-02-20 06:57:57 +00:00
balance.go store/cachekv, x/bank/types: algorithmically fix pathologically slow code (#8719) 2021-02-27 07:26:22 -08:00
balance_test.go store/cachekv, x/bank/types: algorithmically fix pathologically slow code (#8719) 2021-02-27 07:26:22 -08:00
bank.pb.go bank: name and symbol metadata fields (#8677) 2021-02-24 20:06:04 -03:00
codec.go RegisterInterfaces registers service Msg type_urls (#7671) 2020-10-28 19:20:45 +00:00
errors.go x/bank: client denom metadata gRPC (#8317) 2021-01-18 18:15:16 +00:00
events.go [bank]: add balance tracking events (#8656) 2021-02-25 18:15:02 +00:00
expected_keepers.go Update x/auth to use Any (#6165) 2020-05-20 19:21:00 +00:00
genesis.go x/bank: balance and metadata validation (#8417) 2021-01-22 21:44:54 -03:00
genesis.pb.go Update gogo proto deps with v1.3.2 security fixes (#8350) 2021-01-15 19:45:34 +00:00
genesis_test.go auth: allow 0 coin accounts in genesis (#8714) 2021-02-26 15:41:48 +00:00
key.go Refactor store keys for variable-length addresses (#8363) 2021-02-01 13:17:44 +00:00
key_test.go Refactor store keys for variable-length addresses (#8363) 2021-02-01 13:17:44 +00:00
metadata.go bank: name and symbol metadata fields (#8677) 2021-02-24 20:06:04 -03:00
metadata_test.go bank: name and symbol metadata fields (#8677) 2021-02-24 20:06:04 -03:00
msgs.go Rosetta API implementation (#7695) 2021-01-21 09:33:02 +00:00
msgs_test.go Refactor store keys for variable-length addresses (#8363) 2021-02-01 13:17:44 +00:00
params.go Remove viper in Staking module. (#6631) 2020-07-08 22:00:34 +02:00
params_test.go types: update coin regex (#7027) 2020-08-14 09:09:53 +00:00
querier.go Change `address` from bytes to bech32 strings (#7242) 2020-09-25 10:25:37 +00:00
query.pb.go x/bank: client denom metadata gRPC (#8317) 2021-01-18 18:15:16 +00:00
query.pb.gw.go gRPC gateway issue with colons in the account url path (#8649) 2021-02-22 11:30:45 +00:00
send_authorization.go x/authz: Add DelegateAuthorization, UndelegateAuthorization (#8472) 2021-02-20 06:57:57 +00:00
supply.go x/bank: balance and metadata validation (#8417) 2021-01-22 21:44:54 -03:00
supply_test.go Merge PR #6010: Merge x/supply into x/bank 2020-04-20 11:22:12 -04:00
tx.pb.go Update gogo proto deps with v1.3.2 security fixes (#8350) 2021-01-15 19:45:34 +00:00