diff --git a/chain/store/messages.go b/chain/store/messages.go index c39cb3f9b9d..90cf58f37e9 100644 --- a/chain/store/messages.go +++ b/chain/store/messages.go @@ -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 + } applied := make(map[address.Address]uint64) @@ -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 } diff --git a/chain/store/store.go b/chain/store/store.go index d7188a7bfd1..017f7aa6bcf 100644 --- a/chain/store/store.go +++ b/chain/store/store.go @@ -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") @@ -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. @@ -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 @@ -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() } @@ -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, }