From b7a19b4a4dc9b4fe85010ecfdc3546a8c0b0d0e8 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Tue, 30 Jan 2024 06:53:43 -0300 Subject: [PATCH] trie/utils: bound point cache size (#352) Signed-off-by: Ignacio Hagopian --- trie/utils/verkle.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/trie/utils/verkle.go b/trie/utils/verkle.go index 3c581cee57..65f4c1fa2a 100644 --- a/trie/utils/verkle.go +++ b/trie/utils/verkle.go @@ -18,9 +18,9 @@ package utils import ( "encoding/binary" - "sync" "github.com/crate-crypto/go-ipa/bandersnatch/fr" + "github.com/ethereum/go-ethereum/common/lru" "github.com/ethereum/go-verkle" "github.com/holiman/uint256" ) @@ -31,6 +31,8 @@ const ( NonceLeafKey = 2 CodeKeccakLeafKey = 3 CodeSizeLeafKey = 4 + + maxPointCacheByteSize = 100 << 20 ) var ( @@ -47,28 +49,26 @@ var ( ) type PointCache struct { - cache map[string]*verkle.Point - lock sync.RWMutex + cache *lru.Cache[string, *verkle.Point] } func NewPointCache() *PointCache { + // Each verkle.Point is 96 bytes. + verklePointSize := 96 + capacity := maxPointCacheByteSize / verklePointSize return &PointCache{ - cache: make(map[string]*verkle.Point), + cache: lru.NewCache[string, *verkle.Point](capacity), } } func (pc *PointCache) GetTreeKeyHeader(addr []byte) *verkle.Point { - pc.lock.RLock() - point, ok := pc.cache[string(addr)] - pc.lock.RUnlock() + point, ok := pc.cache.Get(string(addr)) if ok { return point } point = EvaluateAddressPoint(addr) - pc.lock.Lock() - pc.cache[string(addr)] = point - pc.lock.Unlock() + pc.cache.Add(string(addr), point) return point }