Dont do locking using defer
This commit is contained in:
parent
f84f8a831a
commit
2e45f6f778
@ -130,15 +130,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) {
|
||||||
tsKey := ts.Key()
|
tsKey := ts.Key()
|
||||||
|
|
||||||
{
|
// check if we have the trace for this tipset in the cache
|
||||||
// check if we have the trace for this tipset in the cache
|
sm.execTraceCacheLock.Lock()
|
||||||
sm.execTraceCacheLock.Lock()
|
if entry, ok := sm.execTraceCache.Get(tsKey); ok {
|
||||||
defer sm.execTraceCacheLock.Unlock()
|
// we have to make a deep copy since caller can modify the invocTrace
|
||||||
if entry, ok := sm.execTraceCache.Get(tsKey); ok {
|
// and we don't want that to change what we store in cache
|
||||||
// we have to make a deep copy since caller can modify the invocTrace
|
invocTraceCopy := makeDeepCopy(entry.invocTrace)
|
||||||
return entry.postStateRoot, makeDeepCopy(entry.invocTrace), nil
|
sm.execTraceCacheLock.Unlock()
|
||||||
}
|
return entry.postStateRoot, invocTraceCopy, nil
|
||||||
}
|
}
|
||||||
|
sm.execTraceCacheLock.Unlock()
|
||||||
|
|
||||||
var invocTrace []*api.InvocResult
|
var invocTrace []*api.InvocResult
|
||||||
st, err := sm.ExecutionTraceWithMonitor(ctx, ts, &InvocationTracer{trace: &invocTrace})
|
st, err := sm.ExecutionTraceWithMonitor(ctx, ts, &InvocationTracer{trace: &invocTrace})
|
||||||
@ -146,7 +147,11 @@ func (sm *StateManager) ExecutionTrace(ctx context.Context, ts *types.TipSet) (c
|
|||||||
return cid.Undef, nil, err
|
return cid.Undef, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sm.execTraceCache.Add(tsKey, tipSetCacheEntry{st, makeDeepCopy(invocTrace)})
|
invocTraceCopy := makeDeepCopy(invocTrace)
|
||||||
|
|
||||||
|
sm.execTraceCacheLock.Lock()
|
||||||
|
sm.execTraceCache.Add(tsKey, tipSetCacheEntry{st, invocTraceCopy})
|
||||||
|
sm.execTraceCacheLock.Unlock()
|
||||||
|
|
||||||
return st, invocTrace, nil
|
return st, invocTrace, nil
|
||||||
}
|
}
|
||||||
|
@ -144,7 +144,9 @@ 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.TipSetKey, tipSetCacheEntry]
|
execTraceCache *lru.ARCCache[types.TipSetKey, tipSetCacheEntry]
|
||||||
|
// We need a lock while making the copy as to prevent other callers
|
||||||
|
// overwrite the cache while making the copy
|
||||||
execTraceCacheLock sync.Mutex
|
execTraceCacheLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user