Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add tipset -> Message lookup cache #10528

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions chain/store/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ type BlockMessages struct {

func (cs *ChainStore) BlockMsgsForTipset(ctx context.Context, ts *types.TipSet) ([]BlockMessages, error) {
// returned BlockMessages match block order in tipset
tsKey := ts.Key()
if entry, ok := cs.tsMsgCache.Get(tsKey); ok {
return entry, nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I grepped the code and I don't see any caller changing the returned slice from this function. However, nothing prevents a caller from overwriting the array represented by the returned slice so a correct implementation would be to return a copy here like we do in #10517. Thoughts?

}

applied := make(map[address.Address]uint64)

Expand Down Expand Up @@ -178,6 +182,8 @@ func (cs *ChainStore) BlockMsgsForTipset(ctx context.Context, ts *types.TipSet)
out = append(out, bm)
}

cs.tsMsgCache.Add(tsKey, out)

return out, nil
}

Expand Down
16 changes: 14 additions & 2 deletions chain/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var (

var DefaultTipSetCacheSize = 8192
var DefaultMsgMetaCacheSize = 2048
var DefaultTipSetMessageCacheSize = 1024

var ErrNotifeeDone = errors.New("notifee is done and should be removed")

Expand All @@ -66,6 +67,14 @@ func init() {
}
DefaultMsgMetaCacheSize = mmcs
}

if s := os.Getenv("LOTUS_CHAIN_TIPSET_MESSAGE_CACHE"); s != "" {
tsmcs, err := strconv.Atoi(s)
if err != nil {
log.Errorf("failed to parse 'LOTUS_CHAIN_TIPSET_MESSAGE_CACHE' env var: %s", err)
}
DefaultTipSetMessageCacheSize = tsmcs
}
}

// ReorgNotifee represents a callback that gets called upon reorgs.
Expand Down Expand Up @@ -120,8 +129,9 @@ type ChainStore struct {
reorgCh chan<- reorg
reorgNotifeeCh chan ReorgNotifee

mmCache *lru.ARCCache[cid.Cid, mmCids]
tsCache *lru.ARCCache[types.TipSetKey, *types.TipSet]
mmCache *lru.ARCCache[cid.Cid, mmCids]
tsCache *lru.ARCCache[types.TipSetKey, *types.TipSet]
tsMsgCache *lru.ARCCache[types.TipSetKey, []BlockMessages]

evtTypes [1]journal.EventType
journal journal.Journal
Expand All @@ -135,6 +145,7 @@ type ChainStore struct {
func NewChainStore(chainBs bstore.Blockstore, stateBs bstore.Blockstore, ds dstore.Batching, weight WeightFunc, j journal.Journal) *ChainStore {
c, _ := lru.NewARC[cid.Cid, mmCids](DefaultMsgMetaCacheSize)
tsc, _ := lru.NewARC[types.TipSetKey, *types.TipSet](DefaultTipSetCacheSize)
tsmc, _ := lru.NewARC[types.TipSetKey, []BlockMessages](DefaultTipSetMessageCacheSize)
if j == nil {
j = journal.NilJournal()
}
Expand All @@ -153,6 +164,7 @@ func NewChainStore(chainBs bstore.Blockstore, stateBs bstore.Blockstore, ds dsto
tipsets: make(map[abi.ChainEpoch][]cid.Cid),
mmCache: c,
tsCache: tsc,
tsMsgCache: tsmc,
cancelFn: cancel,
journal: j,
}
Expand Down