Skip to content

Commit

Permalink
deps: bump hashicorp/golang-lru to v2 and move from ARC to 2Q cache (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored May 22, 2024
1 parent e5c769f commit b715333
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ retract v0.1.0
require (
github.com/celestiaorg/go-libp2p-messenger v0.2.0
github.com/gogo/protobuf v1.3.2
github.com/hashicorp/golang-lru v1.0.2
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-log/v2 v2.5.1
github.com/libp2p/go-libp2p v0.33.2
Expand All @@ -35,7 +35,6 @@ require (
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20240207164012-fb44976bdcd5 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,6 @@ github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
Expand Down
8 changes: 4 additions & 4 deletions store/height_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package store
import (
"context"

lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/ipfs/go-datastore"

"github.com/celestiaorg/go-header"
Expand All @@ -14,12 +14,12 @@ import (
// Hash.
type heightIndexer[H header.Header[H]] struct {
ds datastore.Batching
cache *lru.ARCCache
cache *lru.TwoQueueCache[uint64, header.Hash]
}

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

val, err := hi.ds.Get(ctx, heightKey(h))
Expand Down
8 changes: 4 additions & 4 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync/atomic"
"time"

lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
logging "github.com/ipfs/go-log/v2"
Expand All @@ -31,7 +31,7 @@ type Store[H header.Header[H]] struct {
// underlying KV store
ds datastore.Batching
// adaptive replacement cache of headers
cache *lru.ARCCache
cache *lru.TwoQueueCache[string, H]
// metrics collection instance
metrics *metrics

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

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

0 comments on commit b715333

Please sign in to comment.