cosmos-sdk/store/cachekv/store.go

250 lines
6.3 KiB
Go
Raw Normal View History

2019-02-01 17:03:09 -08:00
package cachekv
import (
"bytes"
"io"
"sort"
"sync"
"time"
2019-02-01 17:03:09 -08:00
dbm "github.com/tendermint/tm-db"
2019-02-01 17:03:09 -08:00
"github.com/cosmos/cosmos-sdk/internal/conv"
"github.com/cosmos/cosmos-sdk/store/listenkv"
2019-02-01 17:03:09 -08:00
"github.com/cosmos/cosmos-sdk/store/tracekv"
"github.com/cosmos/cosmos-sdk/store/types"
2020-06-18 11:12:44 -07:00
"github.com/cosmos/cosmos-sdk/telemetry"
"github.com/cosmos/cosmos-sdk/types/kv"
2019-02-01 17:03:09 -08:00
)
// If value is nil but deleted is false, it means the parent doesn't have the
// key. (No need to delete upon Write())
type cValue struct {
value []byte
deleted bool
dirty bool
}
// Store wraps an in-memory cache around an underlying types.KVStore.
type Store struct {
mtx sync.Mutex
cache map[string]*cValue
unsortedCache map[string]struct{}
store/cachekv: use typed types/kv.List instead of container/list.List (#8811) Reduces CPU burn by using a typed List to avoid the expensive type assertions from using an interface. This is the only option for now until Go makes generics generally available. The change brings time spent on the time assertion cummulatively to: 580ms down from 6.88s Explanation: The type assertions were showing up heavily in profiles: * Before this commit ```shell Total: 42.18s ROUTINE ======================== github.com/cosmos/cosmos-sdk/store/cachekv.newMemIterator in /Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/store/cachekv/memiterator.go 14.01s 18.87s (flat, cum) 44.74% of Total . . 17: items []*kv.Pair . . 18: ascending bool . . 19:} . . 20: . . 21:func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator { . 620ms 22: itemsInDomain := make([]*kv.Pair, 0, items.Len()) . . 23: . . 24: var entered bool . . 25: 510ms 870ms 26: for e := items.Front(); e != nil; e = e.Next() { 6.85s 6.88s 27: item := e.Value.(*kv.Pair) 5.71s 8.19s 28: if !dbm.IsKeyInDomain(item.Key, start, end) { 120ms 120ms 29: if entered { . . 30: break . . 31: } . . 32: . . 33: continue . . 34: } . . 35: 820ms 980ms 36: itemsInDomain = append(itemsInDomain, item) . . 37: entered = true . . 38: } . . 39: . 1.21s 40: return &memIterator{ . . 41: start: start, . . 42: end: end, . . 43: items: itemsInDomain, . . 44: ascending: ascending, . . 45: } ``` and given that the list only uses that type, it is only right to lift the code from container/list.List, and only modify Element.Value's type. For emphasis, the code is basically just a retrofit of container/list/list.go but with a typing, and we'll keep it as is until perhaps Go1.17 or Go1.18 or when everyone uses Go1.17+ after generics have landed. * After this commit ```shell Total: 45.25s ROUTINE ======================== github.com/cosmos/cosmos-sdk/store/cachekv.newMemIterator in /Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/store/cachekv/memiterator.go 4.84s 6.77s (flat, cum) 14.96% of Total . . 16: items []*kv.Pair . . 17: ascending bool . . 18:} . . 19: . . 20:func newMemIterator(start, end []byte, items *kv.List, ascending bool) *memIterator { . 330ms 21: itemsInDomain := make([]*kv.Pair, 0, items.Len()) . . 22: . . 23: var entered bool . . 24: 60ms 160ms 25: for e := items.Front(); e != nil; e = e.Next() { 580ms 580ms 26: item := e.Value 3.68s 4.78s 27: if !dbm.IsKeyInDomain(item.Key, start, end) { 80ms 80ms 28: if entered { . . 29: break . . 30: } . . 31: . . 32: continue . . 33: } . . 34: 440ms 580ms 35: itemsInDomain = append(itemsInDomain, item) . . 36: entered = true . . 37: } . . 38: . 260ms 39: return &memIterator{ . . 40: start: start, . . 41: end: end, . . 42: items: itemsInDomain, . . 43: ascending: ascending, . . 44: } ``` Fixes #8810
2021-03-08 09:16:23 -08:00
sortedCache *kv.List // always ascending sorted
parent types.KVStore
2019-02-01 17:03:09 -08:00
}
var _ types.CacheKVStore = (*Store)(nil)
// NewStore creates a new Store object
2019-02-01 17:03:09 -08:00
func NewStore(parent types.KVStore) *Store {
return &Store{
cache: make(map[string]*cValue),
unsortedCache: make(map[string]struct{}),
store/cachekv: use typed types/kv.List instead of container/list.List (#8811) Reduces CPU burn by using a typed List to avoid the expensive type assertions from using an interface. This is the only option for now until Go makes generics generally available. The change brings time spent on the time assertion cummulatively to: 580ms down from 6.88s Explanation: The type assertions were showing up heavily in profiles: * Before this commit ```shell Total: 42.18s ROUTINE ======================== github.com/cosmos/cosmos-sdk/store/cachekv.newMemIterator in /Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/store/cachekv/memiterator.go 14.01s 18.87s (flat, cum) 44.74% of Total . . 17: items []*kv.Pair . . 18: ascending bool . . 19:} . . 20: . . 21:func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator { . 620ms 22: itemsInDomain := make([]*kv.Pair, 0, items.Len()) . . 23: . . 24: var entered bool . . 25: 510ms 870ms 26: for e := items.Front(); e != nil; e = e.Next() { 6.85s 6.88s 27: item := e.Value.(*kv.Pair) 5.71s 8.19s 28: if !dbm.IsKeyInDomain(item.Key, start, end) { 120ms 120ms 29: if entered { . . 30: break . . 31: } . . 32: . . 33: continue . . 34: } . . 35: 820ms 980ms 36: itemsInDomain = append(itemsInDomain, item) . . 37: entered = true . . 38: } . . 39: . 1.21s 40: return &memIterator{ . . 41: start: start, . . 42: end: end, . . 43: items: itemsInDomain, . . 44: ascending: ascending, . . 45: } ``` and given that the list only uses that type, it is only right to lift the code from container/list.List, and only modify Element.Value's type. For emphasis, the code is basically just a retrofit of container/list/list.go but with a typing, and we'll keep it as is until perhaps Go1.17 or Go1.18 or when everyone uses Go1.17+ after generics have landed. * After this commit ```shell Total: 45.25s ROUTINE ======================== github.com/cosmos/cosmos-sdk/store/cachekv.newMemIterator in /Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/store/cachekv/memiterator.go 4.84s 6.77s (flat, cum) 14.96% of Total . . 16: items []*kv.Pair . . 17: ascending bool . . 18:} . . 19: . . 20:func newMemIterator(start, end []byte, items *kv.List, ascending bool) *memIterator { . 330ms 21: itemsInDomain := make([]*kv.Pair, 0, items.Len()) . . 22: . . 23: var entered bool . . 24: 60ms 160ms 25: for e := items.Front(); e != nil; e = e.Next() { 580ms 580ms 26: item := e.Value 3.68s 4.78s 27: if !dbm.IsKeyInDomain(item.Key, start, end) { 80ms 80ms 28: if entered { . . 29: break . . 30: } . . 31: . . 32: continue . . 33: } . . 34: 440ms 580ms 35: itemsInDomain = append(itemsInDomain, item) . . 36: entered = true . . 37: } . . 38: . 260ms 39: return &memIterator{ . . 40: start: start, . . 41: end: end, . . 42: items: itemsInDomain, . . 43: ascending: ascending, . . 44: } ``` Fixes #8810
2021-03-08 09:16:23 -08:00
sortedCache: kv.NewList(),
parent: parent,
2019-02-01 17:03:09 -08:00
}
}
// GetStoreType implements Store.
2019-02-01 17:03:09 -08:00
func (store *Store) GetStoreType() types.StoreType {
return store.parent.GetStoreType()
}
// Get implements types.KVStore.
2019-02-01 17:03:09 -08:00
func (store *Store) Get(key []byte) (value []byte) {
store.mtx.Lock()
defer store.mtx.Unlock()
defer telemetry.MeasureSince(time.Now(), "store", "cachekv", "get")
2019-02-01 17:03:09 -08:00
types.AssertValidKey(key)
store/cachekv: reduce allocation with []byte -> string in map keys (#9275) Uses internal/conv throughout store/kv which shows performance gains. Benchmark for store/cachekv: name old time/op new time/op delta CacheKVStoreIterator500-8 23.4µs ± 1% 23.3µs ± 1% ~ (p=0.095 n=5+5) CacheKVStoreIterator1000-8 46.7µs ± 1% 46.2µs ± 0% -0.96% (p=0.008 n=5+5) CacheKVStoreIterator10000-8 457µs ± 1% 455µs ± 1% ~ (p=1.000 n=5+5) CacheKVStoreIterator50000-8 2.59ms ± 2% 2.47ms ± 1% -4.64% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 7.33ms ± 3% 6.91ms ± 1% -5.75% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 423ns ± 1% 391ns ± 2% -7.41% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 267ns ± 3% 264ns ± 2% ~ (p=0.595 n=5+5) name old alloc/op new alloc/op delta CacheKVStoreIterator500-8 5.18kB ± 0% 5.18kB ± 0% ~ (all equal) CacheKVStoreIterator1000-8 9.29kB ± 0% 9.29kB ± 0% ~ (p=0.079 n=4+5) CacheKVStoreIterator10000-8 85.2kB ± 0% 84.9kB ± 0% -0.30% (p=0.008 n=5+5) CacheKVStoreIterator50000-8 468kB ± 1% 458kB ± 0% -2.17% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 1.16MB ± 1% 1.10MB ± 0% -5.34% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 222B ± 1% 214B ± 0% -3.78% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 51.0B ± 0% 51.0B ± 0% ~ (all equal) name old allocs/op new allocs/op delta CacheKVStoreIterator500-8 13.0 ± 0% 13.0 ± 0% ~ (all equal) CacheKVStoreIterator1000-8 13.0 ± 0% 13.0 ± 0% ~ (all equal) CacheKVStoreIterator10000-8 51.0 ± 0% 43.0 ± 0% -15.69% (p=0.008 n=5+5) CacheKVStoreIterator50000-8 1.22k ± 4% 0.94k ± 1% -23.04% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 6.48k ± 4% 4.85k ± 1% -25.12% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 5.00 ± 0% 4.00 ± 0% -20.00% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) Benchmark for x/auth/keeper: name old time/op new time/op delta AccountMapperGetAccountFound-8 1.27µs ± 3% 1.26µs ± 1% ~ (p=0.270 n=5+5) AccountMapperSetAccount-8 3.53µs ± 0% 3.44µs ± 1% -2.59% (p=0.008 n=5+5) name old alloc/op new alloc/op delta AccountMapperGetAccountFound-8 440B ± 0% 440B ± 0% ~ (all equal) AccountMapperSetAccount-8 2.13kB ± 0% 2.08kB ± 0% -2.31% (p=0.008 n=5+5) name old allocs/op new allocs/op delta AccountMapperGetAccountFound-8 10.0 ± 0% 10.0 ± 0% ~ (all equal) AccountMapperSetAccount-8 42.0 ± 0% 38.0 ± 0% -9.52% (p=0.008 n=5+5) Fixes #9274
2021-05-06 06:33:01 -07:00
cacheValue, ok := store.cache[conv.UnsafeBytesToStr(key)]
2019-02-01 17:03:09 -08:00
if !ok {
value = store.parent.Get(key)
store.setCacheValue(key, value, false, false)
} else {
value = cacheValue.value
}
return value
}
// Set implements types.KVStore.
2019-02-01 17:03:09 -08:00
func (store *Store) Set(key []byte, value []byte) {
store.mtx.Lock()
defer store.mtx.Unlock()
defer telemetry.MeasureSince(time.Now(), "store", "cachekv", "set")
2019-02-01 17:03:09 -08:00
types.AssertValidKey(key)
types.AssertValidValue(value)
store.setCacheValue(key, value, false, true)
}
// Has implements types.KVStore.
2019-02-01 17:03:09 -08:00
func (store *Store) Has(key []byte) bool {
value := store.Get(key)
return value != nil
}
// Delete implements types.KVStore.
2019-02-01 17:03:09 -08:00
func (store *Store) Delete(key []byte) {
store.mtx.Lock()
defer store.mtx.Unlock()
defer telemetry.MeasureSince(time.Now(), "store", "cachekv", "delete")
2019-02-01 17:03:09 -08:00
types.AssertValidKey(key)
store.setCacheValue(key, nil, true, true)
}
// Implements Cachetypes.KVStore.
func (store *Store) Write() {
store.mtx.Lock()
defer store.mtx.Unlock()
defer telemetry.MeasureSince(time.Now(), "store", "cachekv", "write")
2019-02-01 17:03:09 -08:00
// We need a copy of all of the keys.
// Not the best, but probably not a bottleneck depending.
keys := make([]string, 0, len(store.cache))
2019-02-01 17:03:09 -08:00
for key, dbValue := range store.cache {
if dbValue.dirty {
keys = append(keys, key)
}
}
sort.Strings(keys)
// TODO: Consider allowing usage of Batch, which would allow the write to
// at least happen atomically.
for _, key := range keys {
cacheValue := store.cache[key]
2019-08-19 09:06:27 -07:00
switch {
case cacheValue.deleted:
2019-02-01 17:03:09 -08:00
store.parent.Delete([]byte(key))
2019-08-19 09:06:27 -07:00
case cacheValue.value == nil:
2019-02-01 17:03:09 -08:00
// Skip, it already doesn't exist in parent.
2019-08-19 09:06:27 -07:00
default:
2019-02-01 17:03:09 -08:00
store.parent.Set([]byte(key), cacheValue.value)
}
}
// Clear the cache
store.cache = make(map[string]*cValue)
store.unsortedCache = make(map[string]struct{})
store/cachekv: use typed types/kv.List instead of container/list.List (#8811) Reduces CPU burn by using a typed List to avoid the expensive type assertions from using an interface. This is the only option for now until Go makes generics generally available. The change brings time spent on the time assertion cummulatively to: 580ms down from 6.88s Explanation: The type assertions were showing up heavily in profiles: * Before this commit ```shell Total: 42.18s ROUTINE ======================== github.com/cosmos/cosmos-sdk/store/cachekv.newMemIterator in /Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/store/cachekv/memiterator.go 14.01s 18.87s (flat, cum) 44.74% of Total . . 17: items []*kv.Pair . . 18: ascending bool . . 19:} . . 20: . . 21:func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator { . 620ms 22: itemsInDomain := make([]*kv.Pair, 0, items.Len()) . . 23: . . 24: var entered bool . . 25: 510ms 870ms 26: for e := items.Front(); e != nil; e = e.Next() { 6.85s 6.88s 27: item := e.Value.(*kv.Pair) 5.71s 8.19s 28: if !dbm.IsKeyInDomain(item.Key, start, end) { 120ms 120ms 29: if entered { . . 30: break . . 31: } . . 32: . . 33: continue . . 34: } . . 35: 820ms 980ms 36: itemsInDomain = append(itemsInDomain, item) . . 37: entered = true . . 38: } . . 39: . 1.21s 40: return &memIterator{ . . 41: start: start, . . 42: end: end, . . 43: items: itemsInDomain, . . 44: ascending: ascending, . . 45: } ``` and given that the list only uses that type, it is only right to lift the code from container/list.List, and only modify Element.Value's type. For emphasis, the code is basically just a retrofit of container/list/list.go but with a typing, and we'll keep it as is until perhaps Go1.17 or Go1.18 or when everyone uses Go1.17+ after generics have landed. * After this commit ```shell Total: 45.25s ROUTINE ======================== github.com/cosmos/cosmos-sdk/store/cachekv.newMemIterator in /Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/store/cachekv/memiterator.go 4.84s 6.77s (flat, cum) 14.96% of Total . . 16: items []*kv.Pair . . 17: ascending bool . . 18:} . . 19: . . 20:func newMemIterator(start, end []byte, items *kv.List, ascending bool) *memIterator { . 330ms 21: itemsInDomain := make([]*kv.Pair, 0, items.Len()) . . 22: . . 23: var entered bool . . 24: 60ms 160ms 25: for e := items.Front(); e != nil; e = e.Next() { 580ms 580ms 26: item := e.Value 3.68s 4.78s 27: if !dbm.IsKeyInDomain(item.Key, start, end) { 80ms 80ms 28: if entered { . . 29: break . . 30: } . . 31: . . 32: continue . . 33: } . . 34: 440ms 580ms 35: itemsInDomain = append(itemsInDomain, item) . . 36: entered = true . . 37: } . . 38: . 260ms 39: return &memIterator{ . . 40: start: start, . . 41: end: end, . . 42: items: itemsInDomain, . . 43: ascending: ascending, . . 44: } ``` Fixes #8810
2021-03-08 09:16:23 -08:00
store.sortedCache = kv.NewList()
2019-02-01 17:03:09 -08:00
}
// CacheWrap implements CacheWrapper.
2019-02-01 17:03:09 -08:00
func (store *Store) CacheWrap() types.CacheWrap {
return NewStore(store)
}
// CacheWrapWithTrace implements the CacheWrapper interface.
func (store *Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
return NewStore(tracekv.NewStore(store, w, tc))
}
// CacheWrapWithListeners implements the CacheWrapper interface.
func (store *Store) CacheWrapWithListeners(storeKey types.StoreKey, listeners []types.WriteListener) types.CacheWrap {
return NewStore(listenkv.NewStore(store, storeKey, listeners))
}
2019-02-01 17:03:09 -08:00
//----------------------------------------
// Iteration
// Iterator implements types.KVStore.
2019-02-01 17:03:09 -08:00
func (store *Store) Iterator(start, end []byte) types.Iterator {
return store.iterator(start, end, true)
}
// ReverseIterator implements types.KVStore.
2019-02-01 17:03:09 -08:00
func (store *Store) ReverseIterator(start, end []byte) types.Iterator {
return store.iterator(start, end, false)
}
func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator {
store.mtx.Lock()
defer store.mtx.Unlock()
2019-02-01 17:03:09 -08:00
var parent, cache types.Iterator
if ascending {
parent = store.parent.Iterator(start, end)
} else {
parent = store.parent.ReverseIterator(start, end)
}
store.dirtyItems(start, end)
cache = newMemIterator(start, end, store.sortedCache, ascending)
2019-02-01 17:03:09 -08:00
return newCacheMergeIterator(parent, cache, ascending)
}
// Constructs a slice of dirty items, to use w/ memIterator.
func (store *Store) dirtyItems(start, end []byte) {
unsorted := make([]*kv.Pair, 0)
2019-02-01 17:03:09 -08:00
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
n := len(store.unsortedCache)
for key := range store.unsortedCache {
if dbm.IsKeyInDomain(conv.UnsafeStrToBytes(key), start, end) {
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
cacheValue := store.cache[key]
unsorted = append(unsorted, &kv.Pair{Key: []byte(key), Value: cacheValue.value})
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
}
}
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
if len(unsorted) == n { // This pattern allows the Go compiler to emit the map clearing idiom for the entire map.
for key := range store.unsortedCache {
delete(store.unsortedCache, key)
2019-02-01 17:03:09 -08:00
}
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
} else { // Otherwise, normally delete the unsorted keys from the map.
for _, kv := range unsorted {
delete(store.unsortedCache, conv.UnsafeBytesToStr(kv.Key))
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
}
2019-02-01 17:03:09 -08:00
}
sort.Slice(unsorted, func(i, j int) bool {
return bytes.Compare(unsorted[i].Key, unsorted[j].Key) < 0
2019-02-01 17:03:09 -08:00
})
for e := store.sortedCache.Front(); e != nil && len(unsorted) != 0; {
uitem := unsorted[0]
store/cachekv: use typed types/kv.List instead of container/list.List (#8811) Reduces CPU burn by using a typed List to avoid the expensive type assertions from using an interface. This is the only option for now until Go makes generics generally available. The change brings time spent on the time assertion cummulatively to: 580ms down from 6.88s Explanation: The type assertions were showing up heavily in profiles: * Before this commit ```shell Total: 42.18s ROUTINE ======================== github.com/cosmos/cosmos-sdk/store/cachekv.newMemIterator in /Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/store/cachekv/memiterator.go 14.01s 18.87s (flat, cum) 44.74% of Total . . 17: items []*kv.Pair . . 18: ascending bool . . 19:} . . 20: . . 21:func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator { . 620ms 22: itemsInDomain := make([]*kv.Pair, 0, items.Len()) . . 23: . . 24: var entered bool . . 25: 510ms 870ms 26: for e := items.Front(); e != nil; e = e.Next() { 6.85s 6.88s 27: item := e.Value.(*kv.Pair) 5.71s 8.19s 28: if !dbm.IsKeyInDomain(item.Key, start, end) { 120ms 120ms 29: if entered { . . 30: break . . 31: } . . 32: . . 33: continue . . 34: } . . 35: 820ms 980ms 36: itemsInDomain = append(itemsInDomain, item) . . 37: entered = true . . 38: } . . 39: . 1.21s 40: return &memIterator{ . . 41: start: start, . . 42: end: end, . . 43: items: itemsInDomain, . . 44: ascending: ascending, . . 45: } ``` and given that the list only uses that type, it is only right to lift the code from container/list.List, and only modify Element.Value's type. For emphasis, the code is basically just a retrofit of container/list/list.go but with a typing, and we'll keep it as is until perhaps Go1.17 or Go1.18 or when everyone uses Go1.17+ after generics have landed. * After this commit ```shell Total: 45.25s ROUTINE ======================== github.com/cosmos/cosmos-sdk/store/cachekv.newMemIterator in /Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/store/cachekv/memiterator.go 4.84s 6.77s (flat, cum) 14.96% of Total . . 16: items []*kv.Pair . . 17: ascending bool . . 18:} . . 19: . . 20:func newMemIterator(start, end []byte, items *kv.List, ascending bool) *memIterator { . 330ms 21: itemsInDomain := make([]*kv.Pair, 0, items.Len()) . . 22: . . 23: var entered bool . . 24: 60ms 160ms 25: for e := items.Front(); e != nil; e = e.Next() { 580ms 580ms 26: item := e.Value 3.68s 4.78s 27: if !dbm.IsKeyInDomain(item.Key, start, end) { 80ms 80ms 28: if entered { . . 29: break . . 30: } . . 31: . . 32: continue . . 33: } . . 34: 440ms 580ms 35: itemsInDomain = append(itemsInDomain, item) . . 36: entered = true . . 37: } . . 38: . 260ms 39: return &memIterator{ . . 40: start: start, . . 41: end: end, . . 42: items: itemsInDomain, . . 43: ascending: ascending, . . 44: } ``` Fixes #8810
2021-03-08 09:16:23 -08:00
sitem := e.Value
comp := bytes.Compare(uitem.Key, sitem.Key)
switch comp {
case -1:
unsorted = unsorted[1:]
store.sortedCache.InsertBefore(uitem, e)
case 1:
e = e.Next()
case 0:
unsorted = unsorted[1:]
e.Value = uitem
e = e.Next()
}
}
for _, kvp := range unsorted {
store.sortedCache.PushBack(kvp)
}
2019-02-01 17:03:09 -08:00
}
//----------------------------------------
// etc
// Only entrypoint to mutate store.cache.
func (store *Store) setCacheValue(key, value []byte, deleted bool, dirty bool) {
store/cachekv: reduce allocation with []byte -> string in map keys (#9275) Uses internal/conv throughout store/kv which shows performance gains. Benchmark for store/cachekv: name old time/op new time/op delta CacheKVStoreIterator500-8 23.4µs ± 1% 23.3µs ± 1% ~ (p=0.095 n=5+5) CacheKVStoreIterator1000-8 46.7µs ± 1% 46.2µs ± 0% -0.96% (p=0.008 n=5+5) CacheKVStoreIterator10000-8 457µs ± 1% 455µs ± 1% ~ (p=1.000 n=5+5) CacheKVStoreIterator50000-8 2.59ms ± 2% 2.47ms ± 1% -4.64% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 7.33ms ± 3% 6.91ms ± 1% -5.75% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 423ns ± 1% 391ns ± 2% -7.41% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 267ns ± 3% 264ns ± 2% ~ (p=0.595 n=5+5) name old alloc/op new alloc/op delta CacheKVStoreIterator500-8 5.18kB ± 0% 5.18kB ± 0% ~ (all equal) CacheKVStoreIterator1000-8 9.29kB ± 0% 9.29kB ± 0% ~ (p=0.079 n=4+5) CacheKVStoreIterator10000-8 85.2kB ± 0% 84.9kB ± 0% -0.30% (p=0.008 n=5+5) CacheKVStoreIterator50000-8 468kB ± 1% 458kB ± 0% -2.17% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 1.16MB ± 1% 1.10MB ± 0% -5.34% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 222B ± 1% 214B ± 0% -3.78% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 51.0B ± 0% 51.0B ± 0% ~ (all equal) name old allocs/op new allocs/op delta CacheKVStoreIterator500-8 13.0 ± 0% 13.0 ± 0% ~ (all equal) CacheKVStoreIterator1000-8 13.0 ± 0% 13.0 ± 0% ~ (all equal) CacheKVStoreIterator10000-8 51.0 ± 0% 43.0 ± 0% -15.69% (p=0.008 n=5+5) CacheKVStoreIterator50000-8 1.22k ± 4% 0.94k ± 1% -23.04% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 6.48k ± 4% 4.85k ± 1% -25.12% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 5.00 ± 0% 4.00 ± 0% -20.00% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) Benchmark for x/auth/keeper: name old time/op new time/op delta AccountMapperGetAccountFound-8 1.27µs ± 3% 1.26µs ± 1% ~ (p=0.270 n=5+5) AccountMapperSetAccount-8 3.53µs ± 0% 3.44µs ± 1% -2.59% (p=0.008 n=5+5) name old alloc/op new alloc/op delta AccountMapperGetAccountFound-8 440B ± 0% 440B ± 0% ~ (all equal) AccountMapperSetAccount-8 2.13kB ± 0% 2.08kB ± 0% -2.31% (p=0.008 n=5+5) name old allocs/op new allocs/op delta AccountMapperGetAccountFound-8 10.0 ± 0% 10.0 ± 0% ~ (all equal) AccountMapperSetAccount-8 42.0 ± 0% 38.0 ± 0% -9.52% (p=0.008 n=5+5) Fixes #9274
2021-05-06 06:33:01 -07:00
store.cache[conv.UnsafeBytesToStr(key)] = &cValue{
2019-02-01 17:03:09 -08:00
value: value,
deleted: deleted,
dirty: dirty,
}
if dirty {
store/cachekv: reduce allocation with []byte -> string in map keys (#9275) Uses internal/conv throughout store/kv which shows performance gains. Benchmark for store/cachekv: name old time/op new time/op delta CacheKVStoreIterator500-8 23.4µs ± 1% 23.3µs ± 1% ~ (p=0.095 n=5+5) CacheKVStoreIterator1000-8 46.7µs ± 1% 46.2µs ± 0% -0.96% (p=0.008 n=5+5) CacheKVStoreIterator10000-8 457µs ± 1% 455µs ± 1% ~ (p=1.000 n=5+5) CacheKVStoreIterator50000-8 2.59ms ± 2% 2.47ms ± 1% -4.64% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 7.33ms ± 3% 6.91ms ± 1% -5.75% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 423ns ± 1% 391ns ± 2% -7.41% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 267ns ± 3% 264ns ± 2% ~ (p=0.595 n=5+5) name old alloc/op new alloc/op delta CacheKVStoreIterator500-8 5.18kB ± 0% 5.18kB ± 0% ~ (all equal) CacheKVStoreIterator1000-8 9.29kB ± 0% 9.29kB ± 0% ~ (p=0.079 n=4+5) CacheKVStoreIterator10000-8 85.2kB ± 0% 84.9kB ± 0% -0.30% (p=0.008 n=5+5) CacheKVStoreIterator50000-8 468kB ± 1% 458kB ± 0% -2.17% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 1.16MB ± 1% 1.10MB ± 0% -5.34% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 222B ± 1% 214B ± 0% -3.78% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 51.0B ± 0% 51.0B ± 0% ~ (all equal) name old allocs/op new allocs/op delta CacheKVStoreIterator500-8 13.0 ± 0% 13.0 ± 0% ~ (all equal) CacheKVStoreIterator1000-8 13.0 ± 0% 13.0 ± 0% ~ (all equal) CacheKVStoreIterator10000-8 51.0 ± 0% 43.0 ± 0% -15.69% (p=0.008 n=5+5) CacheKVStoreIterator50000-8 1.22k ± 4% 0.94k ± 1% -23.04% (p=0.008 n=5+5) CacheKVStoreIterator100000-8 6.48k ± 4% 4.85k ± 1% -25.12% (p=0.008 n=5+5) CacheKVStoreGetNoKeyFound-8 5.00 ± 0% 4.00 ± 0% -20.00% (p=0.008 n=5+5) CacheKVStoreGetKeyFound-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) Benchmark for x/auth/keeper: name old time/op new time/op delta AccountMapperGetAccountFound-8 1.27µs ± 3% 1.26µs ± 1% ~ (p=0.270 n=5+5) AccountMapperSetAccount-8 3.53µs ± 0% 3.44µs ± 1% -2.59% (p=0.008 n=5+5) name old alloc/op new alloc/op delta AccountMapperGetAccountFound-8 440B ± 0% 440B ± 0% ~ (all equal) AccountMapperSetAccount-8 2.13kB ± 0% 2.08kB ± 0% -2.31% (p=0.008 n=5+5) name old allocs/op new allocs/op delta AccountMapperGetAccountFound-8 10.0 ± 0% 10.0 ± 0% ~ (all equal) AccountMapperSetAccount-8 42.0 ± 0% 38.0 ± 0% -9.52% (p=0.008 n=5+5) Fixes #9274
2021-05-06 06:33:01 -07:00
store.unsortedCache[conv.UnsafeBytesToStr(key)] = struct{}{}
}
2019-02-01 17:03:09 -08:00
}