core: precompute precompiles in create evm instead of compute it each time

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)
This commit is contained in:
cuiweixie 2024-02-22 22:28:19 +08:00
parent b9ca38b735
commit 54498a3504

View file

@ -38,20 +38,7 @@ type (
) )
func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) {
var precompiles map[common.Address]PrecompiledContract p, ok := evm.precompiles[addr]
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]
return p, ok return p, ok
} }
@ -121,6 +108,8 @@ type EVM struct {
// available gas is calculated in gasCall* according to the 63/64 rule and later // available gas is calculated in gasCall* according to the 63/64 rule and later
// applied in opCall*. // applied in opCall*.
callGasTemp uint64 callGasTemp uint64
precompiles map[common.Address]PrecompiledContract
} }
// NewEVM returns a new EVM. The returned EVM is not thread safe and should // 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) 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{ evm := &EVM{
Context: blockCtx, Context: blockCtx,
TxContext: txCtx, TxContext: txCtx,
StateDB: statedb, StateDB: statedb,
Config: config, Config: config,
chainConfig: chainConfig, chainConfig: chainConfig,
chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time), chainRules: chainRules,
precompiles: precompiles,
} }
evm.interpreter = NewEVMInterpreter(evm) evm.interpreter = NewEVMInterpreter(evm)
return evm return evm