cosmos-sdk/x/bank
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
..
atlas x/auth: atlas manifest (#8140) 2020-12-10 17:02:29 +01:00
client bank: name and symbol metadata fields (#8677) 2021-02-24 20:06:04 -03:00
exported Move and update codec.MarshalAny functions to codec.Marshaler interface (#8080) 2020-12-08 09:27:08 +00:00
keeper [bank]: add balance tracking events (#8656) 2021-02-25 18:15:02 +00:00
legacy x{stake,slash,gov,distrib} In-place Store Migrations (#8504) 2021-02-25 10:43:31 +00:00
simulation various linter fixes (#8666) 2021-02-23 08:46:01 +00:00
spec [bank]: add balance tracking events (#8656) 2021-02-25 18:15:02 +00:00
types store/cachekv, x/bank/types: algorithmically fix pathologically slow code (#8719) 2021-02-27 07:26:22 -08:00
app_test.go [Bank] Remove the unsafe balance changing API (#8473) 2021-02-17 18:20:33 +00:00
bench_test.go [Bank] Remove the unsafe balance changing API (#8473) 2021-02-17 18:20:33 +00:00
handler.go Refactor x/evidence to ADR-031 (#7538) 2020-10-16 16:05:25 +00:00
handler_test.go tendermint: update to rc3 (#6892) 2020-08-14 13:58:53 -04:00
module.go x{stake,slash,gov,distrib} In-place Store Migrations (#8504) 2021-02-25 10:43:31 +00:00