Skip to content

Commit

Permalink
feat: precompiled contract poc (#172)
Browse files Browse the repository at this point in the history
* feat: App level precompile

* chore: Rename **2 variables to **Copied
  • Loading branch information
dudong2 authored Sep 11, 2024
1 parent 531d7ff commit d2486fb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
31 changes: 31 additions & 0 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,37 @@ func (store *Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types
return NewStore(tracekv.NewStore(store, w, tc))
}

// 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
cacheCopied := make(map[string]*cValue, len(store.cache))
for key, val := range store.cache {
valueCopied := make([]byte, len(val.value))
copy(valueCopied, val.value)
cacheCopied[key] = &cValue{
value: valueCopied,
dirty: val.dirty,
}
}

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

return &Store{
cache: cacheCopied,
unsortedCache: unsortedCacheCopied,
sortedCache: store.sortedCache.Copy(),
parent: store.parent,
}
}

//----------------------------------------
// Iteration

Expand Down
20 changes: 20 additions & 0 deletions store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,23 @@ func (cms Store) GetKVStore(key types.StoreKey) types.KVStore {
}
return store.(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
storesCopied := make(map[types.StoreKey]types.CacheWrap, len(cms.stores))
for key, store := range cms.stores {
if store, ok := store.(*cachekv.Store); ok {
storesCopied[key] = store.Copy()
}
}

return Store{
db: cms.db.Copy(),
stores: storesCopied,
keys: cms.keys,
traceWriter: cms.traceWriter,
traceContext: cms.traceContext,
}
}
6 changes: 5 additions & 1 deletion store/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ type MultiStore interface {
// From MultiStore.CacheMultiStore()....
type CacheMultiStore interface {
MultiStore
Write() // Writes operations to underlying KVStore
Write() // Writes operations to underlying KVStore
Copy() CacheMultiStore // deep copy of CacheMultiStore
}

// CommitMultiStore is an interface for a MultiStore without cache capabilities.
Expand Down Expand Up @@ -244,6 +245,9 @@ type CacheKVStore interface {

// Writes operations to underlying KVStore
Write()

// deep copy of CacheKVStore
Copy() CacheKVStore
}

// CommitKVStore is an interface for MultiStore.
Expand Down

0 comments on commit d2486fb

Please sign in to comment.