From 54498a3504ceee03221e67eed26a6ffa5e9ef77e Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Thu, 22 Feb 2024 22:28:19 +0800 Subject: [PATCH] core: precompute precompiles in create evm instead of compute it each time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old time/op new time/op delta SimpleLoop/staticcall-identity-100M-10 134ms ± 1% 131ms ± 0% -2.60% (p=0.000 n=17+18) SimpleLoop/call-identity-100M-10 158ms ± 1% 157ms ± 2% -1.00% (p=0.000 n=20+20) SimpleLoop/loop-100M-10 200ms ± 1% 197ms ± 1% -1.43% (p=0.000 n=16+19) SimpleLoop/call-nonexist-100M-10 504ms ± 0% 508ms ± 0% +0.82% (p=0.000 n=16+20) SimpleLoop/call-EOA-100M-10 147ms ± 2% 148ms ± 4% ~ (p=0.060 n=20+20) SimpleLoop/call-reverting-100M-10 330ms ± 0% 329ms ± 0% -0.30% (p=0.003 n=18+16) name old alloc/op new alloc/op delta SimpleLoop/staticcall-identity-100M-10 21.9MB ± 0% 21.9MB ± 0% ~ (p=0.219 n=20+20) SimpleLoop/call-identity-100M-10 21.5MB ± 0% 21.5MB ± 0% ~ (p=0.140 n=20+20) SimpleLoop/loop-100M-10 2.73kB ±13% 2.70kB ±14% ~ (p=0.764 n=20+20) SimpleLoop/call-nonexist-100M-10 102MB ± 0% 102MB ± 0% ~ (p=0.723 n=20+20) SimpleLoop/call-EOA-100M-10 23.9MB ± 0% 23.9MB ± 0% ~ (p=0.797 n=20+19) SimpleLoop/call-reverting-100M-10 240MB ± 0% 240MB ± 0% ~ (p=0.409 n=18+20) name old allocs/op new allocs/op delta SimpleLoop/staticcall-identity-100M-10 685k ± 0% 685k ± 0% ~ (p=0.146 n=20+20) SimpleLoop/call-identity-100M-10 671k ± 0% 671k ± 0% ~ (p=0.221 n=20+20) SimpleLoop/loop-100M-10 26.6 ± 5% 26.5 ± 6% ~ (p=1.000 n=20+20) SimpleLoop/call-nonexist-100M-10 2.24M ± 0% 2.24M ± 0% ~ (p=0.973 n=20+20) SimpleLoop/call-EOA-100M-10 746k ± 0% 746k ± 0% ~ (p=0.947 n=20+20) SimpleLoop/call-reverting-100M-10 5.00M ± 0% 5.00M ± 0% ~ (p=0.225 n=18+20) --- core/vm/evm.go | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/core/vm/evm.go b/core/vm/evm.go index 16cc854908..22073bee5e 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -38,20 +38,7 @@ type ( ) func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { - var precompiles map[common.Address]PrecompiledContract - switch { - case evm.chainRules.IsCancun: - precompiles = PrecompiledContractsCancun - case evm.chainRules.IsBerlin: - precompiles = PrecompiledContractsBerlin - case evm.chainRules.IsIstanbul: - precompiles = PrecompiledContractsIstanbul - case evm.chainRules.IsByzantium: - precompiles = PrecompiledContractsByzantium - default: - precompiles = PrecompiledContractsHomestead - } - p, ok := precompiles[addr] + p, ok := evm.precompiles[addr] return p, ok } @@ -121,6 +108,8 @@ type EVM struct { // available gas is calculated in gasCall* according to the 63/64 rule and later // applied in opCall*. callGasTemp uint64 + + precompiles map[common.Address]PrecompiledContract } // NewEVM returns a new EVM. The returned EVM is not thread safe and should @@ -137,13 +126,30 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig blockCtx.BlobBaseFee = new(big.Int) } } + + chainRules := chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time) + var precompiles map[common.Address]PrecompiledContract + switch { + case chainRules.IsCancun: + precompiles = PrecompiledContractsCancun + case chainRules.IsBerlin: + precompiles = PrecompiledContractsBerlin + case chainRules.IsIstanbul: + precompiles = PrecompiledContractsIstanbul + case chainRules.IsByzantium: + precompiles = PrecompiledContractsByzantium + default: + precompiles = PrecompiledContractsHomestead + } + evm := &EVM{ Context: blockCtx, TxContext: txCtx, StateDB: statedb, Config: config, chainConfig: chainConfig, - chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time), + chainRules: chainRules, + precompiles: precompiles, } evm.interpreter = NewEVMInterpreter(evm) return evm