mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
trie/utils: bound point cache size (#352)
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
This commit is contained in:
parent
4c4de3102a
commit
b7a19b4a4d
1 changed files with 10 additions and 10 deletions
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue