core: shard jumpdest cache into 8 buckets of 8MB

This commit is contained in:
Sina Mahmoodi 2026-05-11 09:57:16 +00:00
parent 5c4a89562d
commit 3ca07b6750

View file

@ -28,27 +28,40 @@ var (
jumpDestMissMeter = metrics.NewRegisteredMeter("chain/cache/jumpdest/miss", nil) jumpDestMissMeter = metrics.NewRegisteredMeter("chain/cache/jumpdest/miss", nil)
) )
// jumpDestCacheSize is the total memory budget granted to the jumpdest const (
// analysis cache. // jumpDestBuckets is the number of independent LRU shards. Code hashes
const jumpDestCacheSize = 128 * 1024 * 1024 // are dispatched by the low bits of the first byte to spread load across
// shards and reduce mutex contention from the parallel prefetcher.
jumpDestBuckets = 8
// jumpDestBucketSize is the per-shard byte budget. Total cache size is
// jumpDestBuckets * jumpDestBucketSize.
jumpDestBucketSize = 8 * 1024 * 1024
)
// JumpDestCache is a thread-safe, byte-bounded LRU of JUMPDEST analysis // JumpDestCache is a thread-safe, byte-bounded LRU of JUMPDEST analysis
// bitmaps. It is owned by BlockChain and shared across block processing and // bitmaps, sharded into independent buckets to reduce lock contention. It is
// prefetching, keyed by the immutable contract code hash. // owned by BlockChain and shared across block processing and prefetching,
// keyed by the immutable contract code hash.
type JumpDestCache struct { type JumpDestCache struct {
cache *lru.SizeConstrainedCache[common.Hash, vm.BitVec] buckets [jumpDestBuckets]struct {
dest *lru.SizeConstrainedCache[common.Hash, vm.BitVec]
}
} }
// NewJumpDestCache constructs the analysis cache. // NewJumpDestCache constructs the analysis cache.
func NewJumpDestCache() *JumpDestCache { func NewJumpDestCache() *JumpDestCache {
return &JumpDestCache{ c := new(JumpDestCache)
cache: lru.NewSizeConstrainedCache[common.Hash, vm.BitVec](jumpDestCacheSize), for i := range c.buckets {
c.buckets[i].dest = lru.NewSizeConstrainedCache[common.Hash, vm.BitVec](jumpDestBucketSize)
} }
return c
} }
// Load retrieves the cached jumpdest analysis for the given code hash. // Load retrieves the cached jumpdest analysis for the given code hash.
func (c *JumpDestCache) Load(hash common.Hash) (vm.BitVec, bool) { func (c *JumpDestCache) Load(hash common.Hash) (vm.BitVec, bool) {
v, ok := c.cache.Get(hash) bucket := &c.buckets[hash[0]&(jumpDestBuckets-1)]
v, ok := bucket.dest.Get(hash)
if ok { if ok {
jumpDestHitMeter.Mark(1) jumpDestHitMeter.Mark(1)
} else { } else {
@ -59,5 +72,6 @@ func (c *JumpDestCache) Load(hash common.Hash) (vm.BitVec, bool) {
// Store saves the jumpdest analysis for the given code hash. // Store saves the jumpdest analysis for the given code hash.
func (c *JumpDestCache) Store(hash common.Hash, b vm.BitVec) { func (c *JumpDestCache) Store(hash common.Hash, b vm.BitVec) {
c.cache.Add(hash, b) bucket := &c.buckets[hash[0]&(jumpDestBuckets-1)]
bucket.dest.Add(hash, b)
} }