Skip to content

Commit

Permalink
chore: Rename **2 variables to **Copied
Browse files Browse the repository at this point in the history
  • Loading branch information
dudong2 committed Sep 11, 2024
1 parent fce86b1 commit 746c27b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
20 changes: 10 additions & 10 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,31 +166,31 @@ func (store *Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types
}

// Copy implements deep copy of CacheKVStore
// TODO(dudong2): frequent calls to deep copy are a big bottleneck for performance, so need to benchmark
func (store *Store) Copy() types.CacheKVStore {
store.mtx.Lock()
defer store.mtx.Unlock()

// deep copy of cValue
// TODO(dudong2): deep copy? or shallow copy?
cache2 := make(map[string]*cValue, len(store.cache))
cacheCopied := make(map[string]*cValue, len(store.cache))
for key, val := range store.cache {
value2 := make([]byte, len(val.value))
copy(value2, val.value)
cache2[key] = &cValue{
value: value2,
valueCopied := make([]byte, len(val.value))
copy(valueCopied, val.value)
cacheCopied[key] = &cValue{
value: valueCopied,
dirty: val.dirty,
}
}

// unsortedCache only track the key
unsortedCache2 := make(map[string]struct{}, len(store.unsortedCache))
unsortedCacheCopied := make(map[string]struct{}, len(store.unsortedCache))
for key := range store.unsortedCache {
unsortedCache2[key] = struct{}{}
unsortedCacheCopied[key] = struct{}{}
}

return &Store{
cache: cache2,
unsortedCache: unsortedCache2,
cache: cacheCopied,
unsortedCache: unsortedCacheCopied,
sortedCache: store.sortedCache.Copy(),
parent: store.parent,
}
Expand Down
7 changes: 4 additions & 3 deletions store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,19 @@ func (cms Store) GetKVStore(key types.StoreKey) types.KVStore {
}

// Copy returns an deep copy of CacheMultiStore
// TODO(dudong2): frequent calls to deep copy are a big bottleneck for performance, so need to benchmark
func (cms Store) Copy() types.CacheMultiStore {
// deep copy of CacheKVStore underlying CacheMultiStore
stores2 := make(map[types.StoreKey]types.CacheWrap, len(cms.stores))
storesCopied := make(map[types.StoreKey]types.CacheWrap, len(cms.stores))
for key, store := range cms.stores {
if store, ok := store.(*cachekv.Store); ok {
stores2[key] = store.Copy()
storesCopied[key] = store.Copy()
}
}

return Store{
db: cms.db.Copy(),
stores: stores2,
stores: storesCopied,
keys: cms.keys,
traceWriter: cms.traceWriter,
traceContext: cms.traceContext,
Expand Down

0 comments on commit 746c27b

Please sign in to comment.