From 176ecd4c3bab58652b866efa78e90c45e49add6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 26 Jan 2022 15:39:58 +0100 Subject: [PATCH] mpool: Cache state nonces --- chain/messagepool/messagepool.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/chain/messagepool/messagepool.go b/chain/messagepool/messagepool.go index 5d1857bf2..76647e331 100644 --- a/chain/messagepool/messagepool.go +++ b/chain/messagepool/messagepool.go @@ -173,10 +173,17 @@ type MessagePool struct { sigValCache *lru.TwoQueueCache + nonceCache *lru.Cache + evtTypes [3]journal.EventType journal journal.Journal } +type nonceCacheKey struct { + tsk types.TipSetKey + addr address.Address +} + type msgSet struct { msgs map[uint64]*types.SignedMessage nextNonce uint64 @@ -361,6 +368,7 @@ func (ms *msgSet) toSlice() []*types.SignedMessage { func New(ctx context.Context, api Provider, ds dtypes.MetadataDS, us stmgr.UpgradeSchedule, netName dtypes.NetworkName, j journal.Journal) (*MessagePool, error) { cache, _ := lru.New2Q(build.BlsSignatureCacheSize) verifcache, _ := lru.New2Q(build.VerifSigCacheSize) + noncecache, _ := lru.New(256) cfg, err := loadConfig(ctx, ds) if err != nil { @@ -386,6 +394,7 @@ func New(ctx context.Context, api Provider, ds dtypes.MetadataDS, us stmgr.Upgra pruneCooldown: make(chan struct{}, 1), blsSigCache: cache, sigValCache: verifcache, + nonceCache: noncecache, changes: lps.New(50), localMsgs: namespace.Wrap(ds, datastore.NewKey(localMsgsDs)), api: api, @@ -1016,11 +1025,23 @@ func (mp *MessagePool) getStateNonce(ctx context.Context, addr address.Address, done := metrics.Timer(ctx, metrics.MpoolGetNonceDuration) defer done() + nk := nonceCacheKey{ + tsk: ts.Key(), + addr: addr, + } + + n, ok := mp.nonceCache.Get(nk) + if ok { + return n.(uint64), nil + } + act, err := mp.api.GetActorAfter(addr, ts) if err != nil { return 0, err } + mp.nonceCache.Add(nk, act.Nonce) + return act.Nonce, nil }