From b715333014738bc7d3669cd1b4aecd8b8b6f0f3c Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Wed, 22 May 2024 18:05:22 +0200 Subject: [PATCH] deps: bump hashicorp/golang-lru to v2 and move from ARC to 2Q cache (#175) --- go.mod | 3 +-- go.sum | 2 -- store/height_indexer.go | 8 ++++---- store/store.go | 8 ++++---- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index c8ef733a..688587e9 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 0864af68..070cd9f9 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/store/height_indexer.go b/store/height_indexer.go index 7ff8306e..bd534dc4 100644 --- a/store/height_indexer.go +++ b/store/height_indexer.go @@ -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" @@ -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 } @@ -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)) diff --git a/store/store.go b/store/store.go index da7cfd3a..2abf3544 100644 --- a/store/store.go +++ b/store/store.go @@ -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" @@ -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 @@ -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) } @@ -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() {