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, Config: config,
chainConfig: chainConfig, chainConfig: chainConfig,
chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time), chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time),
jumpDests: newGlobalJumpDests(), jumpDests: globalJumpDests,
arena: newArena(), arena: newArena(),
} }
evm.precompiles = activePrecompiledContracts(evm.chainRules) evm.precompiles = activePrecompiledContracts(evm.chainRules)

View file

@ -17,11 +17,14 @@
package vm package vm
import ( import (
"sync"
"github.com/ethereum/go-ethereum/common" "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. // JumpDestCache represents the cache of jumpdest analysis results.
type JumpDestCache interface { type JumpDestCache interface {
// Load retrieves the cached jumpdest analysis for the given code hash. // 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 j[codeHash] = vec
} }
// globalJumpDestCache is a global cache of JUMPDEST bitmaps. // globalJumpDests is a process-global LRU of JUMPDEST bitmaps, shared across
var globalJumpDestCache sync.Map // every EVM instance and keyed by the immutable contract code hash.
var globalJumpDests = &lruJumpDests{cache: lru.NewCache[common.Hash, BitVec](globalJumpDestCacheSize)}
type globalJumpDests struct{} type lruJumpDests struct {
cache *lru.Cache[common.Hash, BitVec]
// newGlobalJumpDests returns a JumpDestCache backed by the process-global
// sync.Map. All callers share the same backing map.
func newGlobalJumpDests() JumpDestCache {
return globalJumpDests{}
} }
func (globalJumpDests) Load(codeHash common.Hash) (BitVec, bool) { func (j *lruJumpDests) Load(codeHash common.Hash) (BitVec, bool) {
v, ok := globalJumpDestCache.Load(codeHash) return j.cache.Get(codeHash)
if !ok {
return nil, false
}
return v.(BitVec), true
} }
func (globalJumpDests) Store(codeHash common.Hash, vec BitVec) { func (j *lruJumpDests) Store(codeHash common.Hash, vec BitVec) {
globalJumpDestCache.Store(codeHash, vec) j.cache.Add(codeHash, vec)
} }