Use TipSetKey as key in cache and return copies
This commit is contained in:
parent
5caf0bd394
commit
b515f14970
@ -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) {
|
||||
if entry, ok := sm.execTraceCache.Get(ts); ok {
|
||||
return entry.cid, entry.invocTrace, nil
|
||||
tsKey := ts.Key()
|
||||
|
||||
{
|
||||
// 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
|
||||
@ -138,7 +146,20 @@ func (sm *StateManager) ExecutionTrace(ctx context.Context, ts *types.TipSet) (c
|
||||
return cid.Undef, nil, err
|
||||
}
|
||||
|
||||
sm.execTraceCache.Add(ts, tipSetCacheEntry{st, invocTrace})
|
||||
sm.execTraceCache.Add(tsKey, tipSetCacheEntry{st, makeDeepCopy(invocTrace)})
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -144,7 +144,8 @@ type StateManager struct {
|
||||
|
||||
// We keep a small cache for calls to ExecutionTrace which helps improve
|
||||
// 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
|
||||
@ -154,8 +155,8 @@ type treeCache struct {
|
||||
}
|
||||
|
||||
type tipSetCacheEntry struct {
|
||||
cid cid.Cid
|
||||
invocTrace []*api.InvocResult
|
||||
postStateRoot cid.Cid
|
||||
invocTrace []*api.InvocResult
|
||||
}
|
||||
|
||||
func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder, us UpgradeSchedule, beacon beacon.Schedule, metadataDs dstore.Batching, msgIndex index.MsgIndex) (*StateManager, error) {
|
||||
@ -197,7 +198,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 {
|
||||
return nil, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user