Skip to content

Commit b715333

Browse files
authored
deps: bump hashicorp/golang-lru to v2 and move from ARC to 2Q cache (#175)
1 parent e5c769f commit b715333

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ retract v0.1.0
88
require (
99
github.com/celestiaorg/go-libp2p-messenger v0.2.0
1010
github.com/gogo/protobuf v1.3.2
11-
github.com/hashicorp/golang-lru v1.0.2
11+
github.com/hashicorp/golang-lru/v2 v2.0.7
1212
github.com/ipfs/go-datastore v0.6.0
1313
github.com/ipfs/go-log/v2 v2.5.1
1414
github.com/libp2p/go-libp2p v0.33.2
@@ -35,7 +35,6 @@ require (
3535
github.com/google/gopacket v1.1.19 // indirect
3636
github.com/google/pprof v0.0.0-20240207164012-fb44976bdcd5 // indirect
3737
github.com/google/uuid v1.5.0 // indirect
38-
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
3938
github.com/huin/goupnp v1.3.0 // indirect
4039
github.com/ipfs/go-cid v0.4.1 // indirect
4140
github.com/jackpal/go-nat-pmp v1.0.2 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,6 @@ github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA
330330
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
331331
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
332332
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
333-
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
334-
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
335333
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
336334
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
337335
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=

store/height_indexer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package store
33
import (
44
"context"
55

6-
lru "github.com/hashicorp/golang-lru"
6+
lru "github.com/hashicorp/golang-lru/v2"
77
"github.com/ipfs/go-datastore"
88

99
"github.com/celestiaorg/go-header"
@@ -14,12 +14,12 @@ import (
1414
// Hash.
1515
type heightIndexer[H header.Header[H]] struct {
1616
ds datastore.Batching
17-
cache *lru.ARCCache
17+
cache *lru.TwoQueueCache[uint64, header.Hash]
1818
}
1919

2020
// newHeightIndexer creates new heightIndexer.
2121
func newHeightIndexer[H header.Header[H]](ds datastore.Batching, indexCacheSize int) (*heightIndexer[H], error) {
22-
cache, err := lru.NewARC(indexCacheSize)
22+
cache, err := lru.New2Q[uint64, header.Hash](indexCacheSize)
2323
if err != nil {
2424
return nil, err
2525
}
@@ -33,7 +33,7 @@ func newHeightIndexer[H header.Header[H]](ds datastore.Batching, indexCacheSize
3333
// HashByHeight loads a header hash corresponding to the given height.
3434
func (hi *heightIndexer[H]) HashByHeight(ctx context.Context, h uint64) (header.Hash, error) {
3535
if v, ok := hi.cache.Get(h); ok {
36-
return v.(header.Hash), nil
36+
return v, nil
3737
}
3838

3939
val, err := hi.ds.Get(ctx, heightKey(h))

store/store.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"sync/atomic"
88
"time"
99

10-
lru "github.com/hashicorp/golang-lru"
10+
lru "github.com/hashicorp/golang-lru/v2"
1111
"github.com/ipfs/go-datastore"
1212
"github.com/ipfs/go-datastore/namespace"
1313
logging "github.com/ipfs/go-log/v2"
@@ -31,7 +31,7 @@ type Store[H header.Header[H]] struct {
3131
// underlying KV store
3232
ds datastore.Batching
3333
// adaptive replacement cache of headers
34-
cache *lru.ARCCache
34+
cache *lru.TwoQueueCache[string, H]
3535
// metrics collection instance
3636
metrics *metrics
3737

@@ -89,7 +89,7 @@ func newStore[H header.Header[H]](ds datastore.Batching, opts ...Option) (*Store
8989
return nil, fmt.Errorf("header/store: store creation failed: %w", err)
9090
}
9191

92-
cache, err := lru.NewARC(params.StoreCacheSize)
92+
cache, err := lru.New2Q[string, H](params.StoreCacheSize)
9393
if err != nil {
9494
return nil, fmt.Errorf("failed to create index cache: %w", err)
9595
}
@@ -198,7 +198,7 @@ func (s *Store[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, erro
198198
func (s *Store[H]) Get(ctx context.Context, hash header.Hash) (H, error) {
199199
var zero H
200200
if v, ok := s.cache.Get(hash.String()); ok {
201-
return v.(H), nil
201+
return v, nil
202202
}
203203
// check if the requested header is not yet written on disk
204204
if h := s.pending.Get(hash); !h.IsZero() {

0 commit comments

Comments
 (0)