trie/utils: bound point cache size (#352)

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
This commit is contained in:
Ignacio Hagopian 2024-01-30 06:53:43 -03:00 committed by Guillaume Ballet
parent 4c4de3102a
commit b7a19b4a4d

View file

@ -18,9 +18,9 @@ package utils
import ( import (
"encoding/binary" "encoding/binary"
"sync"
"github.com/crate-crypto/go-ipa/bandersnatch/fr" "github.com/crate-crypto/go-ipa/bandersnatch/fr"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-verkle" "github.com/ethereum/go-verkle"
"github.com/holiman/uint256" "github.com/holiman/uint256"
) )
@ -31,6 +31,8 @@ const (
NonceLeafKey = 2 NonceLeafKey = 2
CodeKeccakLeafKey = 3 CodeKeccakLeafKey = 3
CodeSizeLeafKey = 4 CodeSizeLeafKey = 4
maxPointCacheByteSize = 100 << 20
) )
var ( var (
@ -47,28 +49,26 @@ var (
) )
type PointCache struct { type PointCache struct {
cache map[string]*verkle.Point cache *lru.Cache[string, *verkle.Point]
lock sync.RWMutex
} }
func NewPointCache() *PointCache { func NewPointCache() *PointCache {
// Each verkle.Point is 96 bytes.
verklePointSize := 96
capacity := maxPointCacheByteSize / verklePointSize
return &PointCache{ return &PointCache{
cache: make(map[string]*verkle.Point), cache: lru.NewCache[string, *verkle.Point](capacity),
} }
} }
func (pc *PointCache) GetTreeKeyHeader(addr []byte) *verkle.Point { func (pc *PointCache) GetTreeKeyHeader(addr []byte) *verkle.Point {
pc.lock.RLock() point, ok := pc.cache.Get(string(addr))
point, ok := pc.cache[string(addr)]
pc.lock.RUnlock()
if ok { if ok {
return point return point
} }
point = EvaluateAddressPoint(addr) point = EvaluateAddressPoint(addr)
pc.lock.Lock() pc.cache.Add(string(addr), point)
pc.cache[string(addr)] = point
pc.lock.Unlock()
return point return point
} }