Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Clear cacheKV RAM if its too large #15767

Merged
merged 4 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (baseapp) [#15023](https://github.com/cosmos/cosmos-sdk/pull/15023) & [#15213](https://github.com/cosmos/cosmos-sdk/pull/15213) Add `MessageRouter` interface to baseapp and pass it to authz, gov and groups instead of concrete type.
* (simtestutil) [#15305](https://github.com/cosmos/cosmos-sdk/pull/15305) Add `AppStateFnWithExtendedCb` with callback function to extend rawState.
* (x/consensus) [#15553](https://github.com/cosmos/cosmos-sdk/pull/15553) Migrate consensus module to use collections
* (store/cachekv) [#15767](https://github.com/cosmos/cosmos-sdk/pull/15767) Reduce peak RAM usage during and after InitGenesis

### State Machine Breaking

Expand Down
59 changes: 38 additions & 21 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ func (store *Store) Delete(key []byte) {
store.setCacheValue(key, nil, true)
}

type cEntry struct {
key string
val *cValue
}
ValarDragon marked this conversation as resolved.
Show resolved Hide resolved

func (store *Store) deleteCaches() {
ValarDragon marked this conversation as resolved.
Show resolved Hide resolved
if len(store.cache) > 100_000 {
Copy link
Contributor

@elias-orijtech elias-orijtech May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In light of #15767 (comment) consider making this branch unconditional, that is, never use the cache clearing idiom.

The clearing was introduced as part of dbb9923, so I wonder how much performance is gained by the clearing itself. If not much, that's another reason to stop clearing.

// Cache is too large. We likely did something linear time
ValarDragon marked this conversation as resolved.
Show resolved Hide resolved
// (e.g. Epoch block, Genesis block, etc). Free the old caches from memory, and let them get re-allocated.
// TODO: In a future CacheKV redesign, such linear workloads should get into a different cache instantiation.
store.cache = make(map[string]*cValue)
store.unsortedCache = make(map[string]struct{})
} else {
// Clear the cache using the map clearing idiom
// and not allocating fresh objects.
// Please see https://bencher.orijtech.com/perfclinic/mapclearing/
for key := range store.cache {
delete(store.cache, key)
}
Comment on lines +108 to +110

Check warning

Code scanning / CodeQL

Iteration over map

Iteration over map may be a possible source of non-determinism
for key := range store.unsortedCache {
delete(store.unsortedCache, key)
}
Comment on lines +111 to +113

Check warning

Code scanning / CodeQL

Iteration over map

Iteration over map may be a possible source of non-determinism
}
store.sortedCache = internal.NewBTree()
}

// Implements Cachetypes.KVStore.
func (store *Store) Write() {
store.mtx.Lock()
Expand All @@ -104,43 +130,34 @@ func (store *Store) Write() {
}

// 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))
// Not the best. To reduce RAM pressure, we copy the values as well
// and clear out the old caches right after the copy.
sortedCache := make([]cEntry, 0, len(store.cache))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an observation -- I assume this slice is often oversized, as it ideally match the number of dirty entries in the store.cache. That may be worth tuning in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, the entire CacheKV store should just be rewritten entirely. We perenially run into performance issues due to some wack part of its design.

Its very overly complicated, for little reason


for key, dbValue := range store.cache {
if dbValue.dirty {
keys = append(keys, key)
sortedCache = append(sortedCache, cEntry{key, dbValue})
}
}

sort.Strings(keys)
store.deleteCaches()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method would read easier if resetting the caches happened before assigning into sortedCache, even if you need to store len(store.cache) first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand, you can only reset the caches, once you've grabbed a copy of the data?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think after the Write call, the whole cache store is discarded usually?

Copy link
Contributor Author

@ValarDragon ValarDragon Apr 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting. If so:

  • This is still a win, for getting the data discarded earlier. (rather than being held with the underlying DB's writes as well)
  • We should just always clear everything, rather than the old capacity preservation code then

ValarDragon marked this conversation as resolved.
Show resolved Hide resolved
sort.Slice(sortedCache, func(i, j int) bool {
return sortedCache[i].key < sortedCache[j].key
})

// TODO: Consider allowing usage of Batch, which would allow the write to
// at least happen atomically.
for _, key := range keys {
for _, obj := range sortedCache {
// We use []byte(key) instead of conv.UnsafeStrToBytes because we cannot
// be sure if the underlying store might do a save with the byteslice or
// not. Once we get confirmation that .Delete is guaranteed not to
// save the byteslice, then we can assume only a read-only copy is sufficient.
cacheValue := store.cache[key]
if cacheValue.value != nil {
if obj.val.value != nil {
// It already exists in the parent, hence update it.
store.parent.Set([]byte(key), cacheValue.value)
store.parent.Set([]byte(obj.key), obj.val.value)
} else {
store.parent.Delete([]byte(key))
store.parent.Delete([]byte(obj.key))
}
}

// Clear the cache using the map clearing idiom
// and not allocating fresh objects.
// Please see https://bencher.orijtech.com/perfclinic/mapclearing/
for key := range store.cache {
delete(store.cache, key)
}
for key := range store.unsortedCache {
delete(store.unsortedCache, key)
}
store.sortedCache = internal.NewBTree()
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
}

// CacheWrap implements CacheWrapper.
Expand Down