Skip to content

Commit

Permalink
CacheKV speedups
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon committed Jan 16, 2024
1 parent 55b53a1 commit 8b4f1b9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
7 changes: 3 additions & 4 deletions store/cachekv/search_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cachekv
import (
"strconv"
"testing"

"github.com/cosmos/cosmos-sdk/store/cachekv/internal"
)

func BenchmarkLargeUnsortedMisses(b *testing.B) {
Expand Down Expand Up @@ -36,9 +34,10 @@ func generateStore() *Store {
cache[key] = &cValue{}
}

return &Store{
store := &Store{
cache: cache,
unsortedCache: unsorted,
sortedCache: internal.NewBTree(),
}
store.resetSortedCache()
return store
}
16 changes: 12 additions & 4 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Store struct {
mtx sync.Mutex
cache map[string]*cValue
unsortedCache map[string]struct{}
sortedCache internal.BTree // always ascending sorted
sortedCache *internal.BTree // always ascending sorted
parent types.KVStore
}

Expand All @@ -39,7 +39,7 @@ func NewStore(parent types.KVStore) *Store {
return &Store{
cache: make(map[string]*cValue),
unsortedCache: make(map[string]struct{}),
sortedCache: internal.NewBTree(),
sortedCache: nil,
parent: parent,
}
}
Expand Down Expand Up @@ -93,13 +93,18 @@ func (store *Store) Delete(key []byte) {
store.setCacheValue(key, nil, true)
}

func (store *Store) resetSortedCache() {
newTree := internal.NewBTree()
store.sortedCache = &newTree
}

// Implements Cachetypes.KVStore.
func (store *Store) Write() {
store.mtx.Lock()
defer store.mtx.Unlock()

if len(store.cache) == 0 && len(store.unsortedCache) == 0 {
store.sortedCache = internal.NewBTree()
store.resetSortedCache()
return
}

Expand Down Expand Up @@ -140,7 +145,7 @@ func (store *Store) Write() {
for key := range store.unsortedCache {
delete(store.unsortedCache, key)
}
store.sortedCache = internal.NewBTree()
store.resetSortedCache()
}

// CacheWrap implements CacheWrapper.
Expand Down Expand Up @@ -169,6 +174,9 @@ func (store *Store) ReverseIterator(start, end []byte) types.Iterator {
func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator {
store.mtx.Lock()
defer store.mtx.Unlock()
if store.sortedCache == nil {
store.resetSortedCache()
}

store.dirtyItems(start, end)
isoSortedCache := store.sortedCache.Copy()
Expand Down
2 changes: 1 addition & 1 deletion store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewStore(
}

func newCacheMultiStoreFromCMS(cms Store) Store {
stores := make(map[types.StoreKey]types.CacheWrapper)
stores := make(map[types.StoreKey]types.CacheWrapper, len(cms.stores))
for k, v := range cms.stores {
stores[k] = v
}
Expand Down

0 comments on commit 8b4f1b9

Please sign in to comment.