Skip to content

Commit

Permalink
feat: chainstore: remove locking in index cache
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed May 12, 2023
1 parent bc60171 commit 1bbd2b7
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions chain/store/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"context"
"os"
"strconv"
"sync"

lru "github.com/hashicorp/golang-lru/v2"

"golang.org/x/xerrors"

Expand All @@ -27,8 +28,7 @@ func init() {
}

type ChainIndex struct {
indexCacheLk sync.Mutex
indexCache map[types.TipSetKey]*lbEntry
indexCache *lru.ARCCache[types.TipSetKey, *lbEntry]

loadTipSet loadTipSetFunc

Expand All @@ -37,8 +37,9 @@ type ChainIndex struct {
type loadTipSetFunc func(context.Context, types.TipSetKey) (*types.TipSet, error)

func NewChainIndex(lts loadTipSetFunc) *ChainIndex {
sc, _ := lru.NewARC[types.TipSetKey, *lbEntry](DefaultChainIndexCacheSize)
return &ChainIndex{
indexCache: make(map[types.TipSetKey]*lbEntry, DefaultChainIndexCacheSize),
indexCache: sc,
loadTipSet: lts,
skipLength: 20,
}
Expand All @@ -59,11 +60,9 @@ func (ci *ChainIndex) GetTipsetByHeight(ctx context.Context, from *types.TipSet,
return nil, xerrors.Errorf("failed to round down: %w", err)
}

ci.indexCacheLk.Lock()
defer ci.indexCacheLk.Unlock()
cur := rounded.Key()
for {
lbe, ok := ci.indexCache[cur]
lbe, ok := ci.indexCache.Get(cur)
if !ok {
fc, err := ci.fillCache(ctx, cur)
if err != nil {
Expand Down Expand Up @@ -137,7 +136,7 @@ func (ci *ChainIndex) fillCache(ctx context.Context, tsk types.TipSetKey) (*lbEn
targetHeight: skipTarget.Height(),
target: skipTarget.Key(),
}
ci.indexCache[tsk] = lbe
ci.indexCache.Add(tsk, lbe)

return lbe, nil
}
Expand Down

0 comments on commit 1bbd2b7

Please sign in to comment.