Add cap of 16k contracts

This commit is contained in:
Sina Mahmoodi 2026-04-30 08:47:14 +00:00
parent 729249ae48
commit 3f49929091
2 changed files with 15 additions and 19 deletions

View file

@ -142,7 +142,7 @@ func NewEVM(blockCtx BlockContext, statedb StateDB, chainConfig *params.ChainCon
Config: config,
chainConfig: chainConfig,
chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time),
jumpDests: newGlobalJumpDests(),
jumpDests: globalJumpDests,
arena: newArena(),
}
evm.precompiles = activePrecompiledContracts(evm.chainRules)

View file

@ -17,11 +17,14 @@
package vm
import (
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
)
// globalJumpDestCacheSize caps the global cache. Worst case ~48MB (24KB code
// → ~3KB bitmap × 16384 entries).
const globalJumpDestCacheSize = 16384
// JumpDestCache represents the cache of jumpdest analysis results.
type JumpDestCache interface {
// Load retrieves the cached jumpdest analysis for the given code hash.
@ -50,25 +53,18 @@ func (j mapJumpDests) Store(codeHash common.Hash, vec BitVec) {
j[codeHash] = vec
}
// globalJumpDestCache is a global cache of JUMPDEST bitmaps.
var globalJumpDestCache sync.Map
// globalJumpDests is a process-global LRU of JUMPDEST bitmaps, shared across
// every EVM instance and keyed by the immutable contract code hash.
var globalJumpDests = &lruJumpDests{cache: lru.NewCache[common.Hash, BitVec](globalJumpDestCacheSize)}
type globalJumpDests struct{}
// newGlobalJumpDests returns a JumpDestCache backed by the process-global
// sync.Map. All callers share the same backing map.
func newGlobalJumpDests() JumpDestCache {
return globalJumpDests{}
type lruJumpDests struct {
cache *lru.Cache[common.Hash, BitVec]
}
func (globalJumpDests) Load(codeHash common.Hash) (BitVec, bool) {
v, ok := globalJumpDestCache.Load(codeHash)
if !ok {
return nil, false
}
return v.(BitVec), true
func (j *lruJumpDests) Load(codeHash common.Hash) (BitVec, bool) {
return j.cache.Get(codeHash)
}
func (globalJumpDests) Store(codeHash common.Hash, vec BitVec) {
globalJumpDestCache.Store(codeHash, vec)
func (j *lruJumpDests) Store(codeHash common.Hash, vec BitVec) {
j.cache.Add(codeHash, vec)
}