Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f46fe4996b |
@@ -4,7 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
|
||||||
|
lru "github.com/hashicorp/golang-lru/v2"
|
||||||
|
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
@@ -27,8 +28,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ChainIndex struct {
|
type ChainIndex struct {
|
||||||
indexCacheLk sync.Mutex
|
indexCache *lru.ARCCache[types.TipSetKey, *lbEntry]
|
||||||
indexCache map[types.TipSetKey]*lbEntry
|
|
||||||
|
|
||||||
loadTipSet loadTipSetFunc
|
loadTipSet loadTipSetFunc
|
||||||
|
|
||||||
@@ -37,8 +37,9 @@ type ChainIndex struct {
|
|||||||
type loadTipSetFunc func(context.Context, types.TipSetKey) (*types.TipSet, error)
|
type loadTipSetFunc func(context.Context, types.TipSetKey) (*types.TipSet, error)
|
||||||
|
|
||||||
func NewChainIndex(lts loadTipSetFunc) *ChainIndex {
|
func NewChainIndex(lts loadTipSetFunc) *ChainIndex {
|
||||||
|
sc, _ := lru.NewARC[types.TipSetKey, *lbEntry](DefaultChainIndexCacheSize)
|
||||||
return &ChainIndex{
|
return &ChainIndex{
|
||||||
indexCache: make(map[types.TipSetKey]*lbEntry, DefaultChainIndexCacheSize),
|
indexCache: sc,
|
||||||
loadTipSet: lts,
|
loadTipSet: lts,
|
||||||
skipLength: 20,
|
skipLength: 20,
|
||||||
}
|
}
|
||||||
@@ -59,11 +60,9 @@ func (ci *ChainIndex) GetTipsetByHeight(ctx context.Context, from *types.TipSet,
|
|||||||
return nil, xerrors.Errorf("failed to round down: %w", err)
|
return nil, xerrors.Errorf("failed to round down: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ci.indexCacheLk.Lock()
|
|
||||||
defer ci.indexCacheLk.Unlock()
|
|
||||||
cur := rounded.Key()
|
cur := rounded.Key()
|
||||||
for {
|
for {
|
||||||
lbe, ok := ci.indexCache[cur]
|
lbe, ok := ci.indexCache.Get(cur)
|
||||||
if !ok {
|
if !ok {
|
||||||
fc, err := ci.fillCache(ctx, cur)
|
fc, err := ci.fillCache(ctx, cur)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -137,7 +136,7 @@ func (ci *ChainIndex) fillCache(ctx context.Context, tsk types.TipSetKey) (*lbEn
|
|||||||
targetHeight: skipTarget.Height(),
|
targetHeight: skipTarget.Height(),
|
||||||
target: skipTarget.Key(),
|
target: skipTarget.Key(),
|
||||||
}
|
}
|
||||||
ci.indexCache[tsk] = lbe
|
ci.indexCache.Add(tsk, lbe)
|
||||||
|
|
||||||
return lbe, nil
|
return lbe, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user