Use TipSetKey as key in cache and return copies
This commit is contained in:
parent
3738433abe
commit
c71680ded8
@ -128,8 +128,16 @@ func (sm *StateManager) ExecutionTraceWithMonitor(ctx context.Context, ts *types
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (sm *StateManager) ExecutionTrace(ctx context.Context, ts *types.TipSet) (cid.Cid, []*api.InvocResult, error) {
|
func (sm *StateManager) ExecutionTrace(ctx context.Context, ts *types.TipSet) (cid.Cid, []*api.InvocResult, error) {
|
||||||
if entry, ok := sm.execTraceCache.Get(ts); ok {
|
tsKey := ts.Key()
|
||||||
return entry.cid, entry.invocTrace, nil
|
|
||||||
|
{
|
||||||
|
// check if we have the trace for this tipset in the cache
|
||||||
|
sm.execTraceCacheLock.Lock()
|
||||||
|
defer sm.execTraceCacheLock.Unlock()
|
||||||
|
if entry, ok := sm.execTraceCache.Get(tsKey); ok {
|
||||||
|
// we have to make a deep copy since caller can modify the invocTrace
|
||||||
|
return entry.postStateRoot, makeDeepCopy(entry.invocTrace), nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var invocTrace []*api.InvocResult
|
var invocTrace []*api.InvocResult
|
||||||
@ -138,7 +146,20 @@ func (sm *StateManager) ExecutionTrace(ctx context.Context, ts *types.TipSet) (c
|
|||||||
return cid.Undef, nil, err
|
return cid.Undef, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sm.execTraceCache.Add(ts, tipSetCacheEntry{st, invocTrace})
|
sm.execTraceCache.Add(tsKey, tipSetCacheEntry{st, makeDeepCopy(invocTrace)})
|
||||||
|
|
||||||
return st, invocTrace, nil
|
return st, invocTrace, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeDeepCopy(invocTrace []*api.InvocResult) []*api.InvocResult {
|
||||||
|
c := make([]*api.InvocResult, len(invocTrace))
|
||||||
|
for i, ir := range invocTrace {
|
||||||
|
if ir == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tmp := *ir
|
||||||
|
c[i] = &tmp
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
@ -142,7 +142,8 @@ type StateManager struct {
|
|||||||
|
|
||||||
// We keep a small cache for calls to ExecutionTrace which helps improve
|
// We keep a small cache for calls to ExecutionTrace which helps improve
|
||||||
// performance for node operators like exchanges and block explorers
|
// performance for node operators like exchanges and block explorers
|
||||||
execTraceCache *lru.ARCCache[*types.TipSet, tipSetCacheEntry]
|
execTraceCache *lru.ARCCache[types.TipSetKey, tipSetCacheEntry]
|
||||||
|
execTraceCacheLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Caches a single state tree
|
// Caches a single state tree
|
||||||
@ -152,8 +153,8 @@ type treeCache struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type tipSetCacheEntry struct {
|
type tipSetCacheEntry struct {
|
||||||
cid cid.Cid
|
postStateRoot cid.Cid
|
||||||
invocTrace []*api.InvocResult
|
invocTrace []*api.InvocResult
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder, us UpgradeSchedule, beacon beacon.Schedule, metadataDs dstore.Batching) (*StateManager, error) {
|
func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder, us UpgradeSchedule, beacon beacon.Schedule, metadataDs dstore.Batching) (*StateManager, error) {
|
||||||
@ -195,7 +196,7 @@ func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
execTraceCache, err := lru.NewARC[*types.TipSet, tipSetCacheEntry](execTraceCacheSize)
|
execTraceCache, err := lru.NewARC[types.TipSetKey, tipSetCacheEntry](execTraceCacheSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user