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> |
||
---|---|---|
.. | ||
cache | ||
cachekv | ||
cachemulti | ||
dbadapter | ||
gaskv | ||
iavl | ||
internal | ||
mem | ||
prefix | ||
rootmulti | ||
tracekv | ||
transient | ||
types | ||
README.md | ||
firstlast.go | ||
reexport.go | ||
store.go |
README.md
Store
CacheKV
cachekv.Store
is a wrapper KVStore
which provides buffered writing / cached reading functionalities over the underlying KVStore
.
type Store struct {
cache map[string]cValue
parent types.KVStore
}
Get
Store.Get()
checks Store.cache
first in order to find if there is any cached value associated with the key. If the value exists, the function returns it. If not, the function calls Store.parent.Get()
, sets the key-value pair to the Store.cache
, and returns it.
Set
Store.Set()
sets the key-value pair to the Store.cache
. cValue
has the field dirty bool
which indicates whether the cached value is different from the underlying value. When Store.Set()
cache new pair, the cValue.dirty
is set true so when Store.Write()
is called it can be written to the underlying store.
Iterator
Store.Iterator()
have to traverse on both caches items and the original items. In Store.iterator()
, two iterators are generated for each of them, and merged. memIterator
is essentially a slice of the KVPair
s, used for cached items. mergeIterator
is a combination of two iterators, where traverse happens ordered on both iterators.
CacheMulti
cachemulti.Store
is a wrapper MultiStore
which provides buffered writing / cached reading functionalities over the underlying MutliStore
type Store struct {
db types.CacheKVStore
stores map[types.StoreKey] types.CacheWrap
}
cachemulti.Store
branches all substores in its constructor and hold them in Store.stores
. Store.GetKVStore()
returns the store from Store.stores
, and Store.Write()
recursively calls CacheWrap.Write()
on the substores.
DBAdapter
dbadapter.Store
is a adapter for dbm.DB
making it fulfilling the KVStore
interface.
type Store struct {
dbm.DB
}
dbadapter.Store
embeds dbm.DB
, so most of the KVStore
interface functions are implemented. The other functions(mostly miscellaneous) are manually implemented.
IAVL
iavl.Store
is a base-layer self-balancing merkle tree. It is guaranteed that
- Get & set operations are
O(log n)
, wheren
is the number of elements in the tree - Iteration efficiently returns the sorted elements within the range
- Each tree version is immutable and can be retrieved even after a commit(depending on the pruning settings)
Specification and implementation of IAVL tree can be found in [https://github.com/tendermint/iavl].
GasKV
gaskv.Store
is a wrapper KVStore
which provides gas consuming functionalities over the underlying KVStore
.
type Store struct {
gasMeter types.GasMeter
gasConfig types.GasConfig
parent types.KVStore
}
When each KVStore
methods are called, gaskv.Store
automatically consumes appropriate amount of gas depending on the Store.gasConfig
.
Prefix
prefix.Store
is a wrapper KVStore
which provides automatic key-prefixing functionalities over the underlying KVStore
.
type Store struct {
parent types.KVStore
prefix []byte
}
When Store.{Get, Set}()
is called, the store forwards the call to its parent, with the key prefixed with the Store.prefix
.
When Store.Iterator()
is called, it does not simply prefix the Store.prefix
, since it does not work as intended. In that case, some of the elements are traversed even they are not starting with the prefix.
RootMulti
rootmulti.Store
is a base-layer MultiStore
where multiple KVStore
can be mounted on it and retrieved via object-capability keys. The keys are memory addresses, so it is impossible to forge the key unless an object is a valid owner(or a receiver) of the key, according to the object capability principles.
TraceKV
tracekv.Store
is a wrapper KVStore
which provides operation tracing functionalities over the underlying KVStore
.
type Store struct {
parent types.KVStore
writer io.Writer
context types.TraceContext
}
When each KVStore
methods are called, tracekv.Store
automatically logs traceOperation
to the Store.writer
.
type traceOperation struct {
Operation operation
Key string
Value string
Metadata map[string]interface{}
}
traceOperation.Metadata
is filled with Store.context
when it is not nil. TraceContext
is a map[string]interface{}
.
Transient
transient.Store
is a base-layer KVStore
which is automatically discarded at the end of the block.
type Store struct {
dbadapter.Store
}
Store.Store
is a dbadapter.Store
with a dbm.NewMemDB()
. All KVStore
methods are reused. When Store.Commit()
is called, new dbadapter.Store
is assigned, discarding previous reference and making it garbage collected.