cosmos-sdk/x
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
..
auth fix: per 8711 fix typo and minor command text edit for multi sign batch command (#8718) 2021-02-27 09:39:08 +00:00
authz keyring: remove hardcoded default passphrase on NewMnemonic (#8662) 2021-02-23 16:49:09 +00:00
bank store/cachekv, x/bank/types: algorithmically fix pathologically slow code (#8719) 2021-02-27 07:26:22 -08:00
capability Add in-place store migrations (#8485) 2021-02-10 17:49:31 +00:00
crisis various linter fixes (#8666) 2021-02-23 08:46:01 +00:00
distribution x{stake,slash,gov,distrib} In-place Store Migrations (#8504) 2021-02-25 10:43:31 +00:00
evidence various linter fixes (#8666) 2021-02-23 08:46:01 +00:00
feegrant keyring: remove hardcoded default passphrase on NewMnemonic (#8662) 2021-02-23 16:49:09 +00:00
genaccounts various linter fixes (#8666) 2021-02-23 08:46:01 +00:00
genutil various linter fixes (#8666) 2021-02-23 08:46:01 +00:00
gov x{stake,slash,gov,distrib} In-place Store Migrations (#8504) 2021-02-25 10:43:31 +00:00
ibc various linter fixes (#8666) 2021-02-23 08:46:01 +00:00
mint various linter fixes (#8666) 2021-02-23 08:46:01 +00:00
params various linter fixes (#8666) 2021-02-23 08:46:01 +00:00
simulation various linter fixes (#8666) 2021-02-23 08:46:01 +00:00
slashing x{stake,slash,gov,distrib} In-place Store Migrations (#8504) 2021-02-25 10:43:31 +00:00
staking [bank]: add balance tracking events (#8656) 2021-02-25 18:15:02 +00:00
upgrade gRPC gateway issue with colons in the account url path (#8649) 2021-02-22 11:30:45 +00:00
README.md typo fix (#8647) 2021-02-20 09:48:39 +00:00

README.md

List of Modules

Here are some production-grade modules that can be used in Cosmos SDK applications, along with their respective documentation:

  • Auth - Authentication of accounts and transactions for Cosmos SDK application.
  • Authz - Authorization for accounts to perform actions on behalf of other accounts.
  • Bank - Token transfer functionalities.
  • Capability - Object capability implementation.
  • Crisis - Halting the blockchain under certain circumstances (e.g. if an invariant is broken).
  • Distribution - Fee distribution, and staking token provision distribution.
  • Evidence - Evidence handling for double signing, misbehaviour, etc.
  • Governance - On-chain proposals and voting.
  • IBC - IBC protocol for transport, authentication and ordering.
  • IBC Transfer - Cross-chain fungible token transfer implementation through IBC.
  • Mint - Creation of new units of staking token.
  • Params - Globally available parameter store.
  • Slashing - Validator punishment mechanisms.
  • Staking - Proof-of-Stake layer for public blockchains.
  • Upgrade - Software upgrades handling and coordination.

To learn more about the process of building modules, visit the building modules reference documentation.