*: use v2 LRU, fix #3322

Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2024-03-05 16:18:01 +03:00
parent 72ebcbec72
commit d65b1fd66d
4 changed files with 14 additions and 19 deletions

2
go.mod
View file

@ -10,7 +10,7 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.1
github.com/hashicorp/golang-lru v0.6.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/holiman/uint256 v1.2.4
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/mr-tron/base58 v1.2.0

4
go.sum
View file

@ -64,8 +64,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/hashicorp/golang-lru v0.6.0 h1:uL2shRDx7RTrOrTCUZEGP/wJUFiUI8QT6E7z5o8jga4=
github.com/hashicorp/golang-lru v0.6.0/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/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU=
github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=

View file

@ -4,7 +4,7 @@ import (
"fmt"
"sync"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -34,12 +34,12 @@ type HeaderHashes struct {
storedHeaderCount uint32
// Cache for accessed pages of header hashes.
cache *lru.Cache
cache *lru.Cache[uint32, []util.Uint256]
}
func (h *HeaderHashes) initGenesis(dao *dao.Simple, hash util.Uint256) {
h.dao = dao
h.cache, _ = lru.New(pagesCache) // Never errors for positive size.
h.cache, _ = lru.New[uint32, []util.Uint256](pagesCache) // Never errors for positive size.
h.previous = make([]util.Uint256, headerBatchCount)
h.latest = make([]util.Uint256, 0, headerBatchCount)
h.latest = append(h.latest, hash)
@ -48,7 +48,7 @@ func (h *HeaderHashes) initGenesis(dao *dao.Simple, hash util.Uint256) {
func (h *HeaderHashes) init(dao *dao.Simple) error {
h.dao = dao
h.cache, _ = lru.New(pagesCache) // Never errors for positive size.
h.cache, _ = lru.New[uint32, []util.Uint256](pagesCache) // Never errors for positive size.
currHeaderHeight, currHeaderHash, err := h.dao.GetCurrentHeaderHeight()
if err != nil {
@ -169,9 +169,8 @@ func (h *HeaderHashes) GetHeaderHash(i uint32) util.Uint256 {
// If it's not in the latest/previous, then it's in the cache or DB, those
// need no additional locks.
page := (i / headerBatchCount) * headerBatchCount
cache, ok := h.cache.Get(page)
hashes, ok := h.cache.Get(page)
if ok {
hashes := cache.([]util.Uint256)
return hashes[i-page]
}
hashes, err := h.dao.GetHeaderHashes(page)

View file

@ -11,7 +11,7 @@ import (
"math/big"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io"
@ -126,22 +126,18 @@ func NewPublicKeyFromString(s string) (*PublicKey, error) {
// keycache is a simple lru cache for P256 keys that avoids Y calculation overhead
// for known keys.
var keycache *lru.Cache
var keycache *lru.Cache[string, *PublicKey]
func init() {
// Less than 100K, probably enough for our purposes.
keycache, _ = lru.New(1024)
keycache, _ = lru.New[string, *PublicKey](1024)
}
// NewPublicKeyFromBytes returns a public key created from b using the given EC.
func NewPublicKeyFromBytes(b []byte, curve elliptic.Curve) (*PublicKey, error) {
var pubKey *PublicKey
cachedKey, ok := keycache.Get(string(b))
if ok {
pubKey = cachedKey.(*PublicKey)
if pubKey.Curve == curve {
return pubKey, nil
}
pubKey, ok := keycache.Get(string(b))
if ok && pubKey.Curve == curve {
return pubKey, nil
}
pubKey = new(PublicKey)
pubKey.Curve = curve