Skip to content

Commit

Permalink
feat: Add small cache to execution traces
Browse files Browse the repository at this point in the history
This PR adds a small cache to calls to ExecutionTrace which helps
 improve performance for node operators like exchanges and block
explorers.

If items is in cache calls to this function will be 2-3x faster.

Fixes: #10504
  • Loading branch information
fridrik01 committed Mar 20, 2023
1 parent 2b3a86e commit 3738433
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions chain/stmgr/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,17 @@ 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
}

var invocTrace []*api.InvocResult
st, err := sm.ExecutionTraceWithMonitor(ctx, ts, &InvocationTracer{trace: &invocTrace})
if err != nil {
return cid.Undef, nil, err
}

sm.execTraceCache.Add(ts, tipSetCacheEntry{st, invocTrace})

return st, invocTrace, nil
}
20 changes: 19 additions & 1 deletion chain/stmgr/stmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"sync"

lru "github.com/hashicorp/golang-lru/v2"
"github.com/ipfs/go-cid"
dstore "github.com/ipfs/go-datastore"
cbor "github.com/ipfs/go-ipld-cbor"
Expand Down Expand Up @@ -38,6 +39,8 @@ import (
const LookbackNoLimit = api.LookbackNoLimit
const ReceiptAmtBitwidth = 3

const execTraceCacheSize = 16

var log = logging.Logger("statemgr")

type StateManagerAPI interface {
Expand Down Expand Up @@ -136,6 +139,10 @@ type StateManager struct {
tsExec Executor
tsExecMonitor ExecMonitor
beacon beacon.Schedule

// 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]
}

// Caches a single state tree
Expand All @@ -144,6 +151,11 @@ type treeCache struct {
tree *state.StateTree
}

type tipSetCacheEntry struct {
cid cid.Cid
invocTrace []*api.InvocResult
}

func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder, us UpgradeSchedule, beacon beacon.Schedule, metadataDs dstore.Batching) (*StateManager, error) {
// If we have upgrades, make sure they're in-order and make sense.
if err := us.Validate(); err != nil {
Expand Down Expand Up @@ -183,6 +195,11 @@ func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder,
}
}

execTraceCache, err := lru.NewARC[*types.TipSet, tipSetCacheEntry](execTraceCacheSize)
if err != nil {
return nil, err
}

return &StateManager{
networkVersions: networkVersions,
latestVersion: lastVersion,
Expand All @@ -198,7 +215,8 @@ func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder,
root: cid.Undef,
tree: nil,
},
compWait: make(map[string]chan struct{}),
compWait: make(map[string]chan struct{}),
execTraceCache: execTraceCache,
}, nil
}

Expand Down

0 comments on commit 3738433

Please sign in to comment.