Use TipSetKey as key in cache and return copies

This commit is contained in:
Fridrik Asmundsson 2023-03-23 13:14:49 +00:00 committed by Maciej Witowski
parent 5caf0bd394
commit b515f14970
2 changed files with 29 additions and 7 deletions

View File

@ -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
}

View File

@ -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
}