From 187a29555163d893718727d8dd20002e5fe3bd68 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Wed, 14 Aug 2024 13:04:49 +0200 Subject: [PATCH] core/vm: add micro-optimization --- common/types.go | 4 ++++ core/vm/instructions.go | 11 +++++------ core/vm/instructions_test.go | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/common/types.go b/common/types.go index fdb25f1b34..64a136e6ec 100644 --- a/common/types.go +++ b/common/types.go @@ -30,6 +30,7 @@ import ( "strings" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/holiman/uint256" "golang.org/x/crypto/sha3" ) @@ -248,6 +249,9 @@ func (a Address) Bytes() []byte { return a[:] } // Big converts an address to a big integer. func (a Address) Big() *big.Int { return new(big.Int).SetBytes(a[:]) } +// Big converts an address to a uint256. +func (a Address) Uint256() *uint256.Int { return new(uint256.Int).SetBytes20(a[:]) } + // Hex returns an EIP55-compliant hex string representation of the address. func (a Address) Hex() string { return string(a.checksumHex()) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 2e0f4c40ab..b5ea706847 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -251,7 +251,7 @@ func opKeccak256(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ( } func opAddress(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Address().Bytes())) + scope.Stack.push(scope.Contract.Address().Uint256()) return nil, nil } @@ -263,12 +263,12 @@ func opBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([] } func opOrigin(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Origin.Bytes())) + scope.Stack.push(interpreter.evm.Origin.Uint256()) return nil, nil } func opCaller(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Caller().Bytes())) + scope.Stack.push(scope.Contract.Caller().Uint256()) return nil, nil } @@ -463,7 +463,7 @@ func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ( } func opCoinbase(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Context.Coinbase.Bytes())) + scope.Stack.push(interpreter.evm.Context.Coinbase.Uint256()) return nil, nil } @@ -485,8 +485,7 @@ func opDifficulty(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) } func opRandom(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { - v := new(uint256.Int).SetBytes(interpreter.evm.Context.Random.Bytes()) - scope.Stack.push(v) + scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Context.Random.Bytes())) return nil, nil } diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index e17e913aa3..2c70ac8cc4 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -580,6 +580,25 @@ func BenchmarkOpMstore(bench *testing.B) { } } +func BenchmarkOpAddress(bench *testing.B) { + var ( + env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) + stack = newstack() + evmInterpreter = NewEVMInterpreter(env) + ) + + env.interpreter = evmInterpreter + pc := uint64(0) + contract := Contract{ + self: contractRef{addr: common.Address{}}, + } + + bench.ResetTimer() + for i := 0; i < bench.N; i++ { + opAddress(&pc, evmInterpreter, &ScopeContext{nil, stack, &contract}) + } +} + func TestOpTstore(t *testing.T) { var ( statedb, _ = state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)