From 1fa9e8187037c31a9b3ad8dafa2af991b4332a39 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Tue, 24 Sep 2024 18:56:33 +0800 Subject: [PATCH] core/vm: reduce overhead in instructions-benchmark (#24860) --- core/vm/instructions_test.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 000a22b2d8..c095e8b9f3 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -291,26 +291,33 @@ func opBenchmark(bench *testing.B, op executionFunc, args ...string) { var ( env = NewEVM(BlockContext{}, TxContext{}, nil, nil, params.TestChainConfig, Config{}) stack = newstack() + scope = &ScopeContext{nil, stack, nil} evmInterpreter = NewEVMInterpreter(env, env.vmConfig) ) env.interpreter = evmInterpreter // convert args - byteArgs := make([][]byte, len(args)) + intArgs := make([]*uint256.Int, len(args)) for i, arg := range args { - byteArgs[i] = common.Hex2Bytes(arg) + intArgs[i] = new(uint256.Int).SetBytes(common.Hex2Bytes(arg)) } pc := uint64(0) bench.ResetTimer() for i := 0; i < bench.N; i++ { - for _, arg := range byteArgs { - a := new(uint256.Int) - a.SetBytes(arg) - stack.push(a) + for _, arg := range intArgs { + stack.push(arg) } - op(&pc, evmInterpreter, &ScopeContext{nil, stack, nil}) + op(&pc, evmInterpreter, scope) stack.pop() } + bench.StopTimer() + + for i, arg := range args { + want := new(uint256.Int).SetBytes(common.Hex2Bytes(arg)) + if have := intArgs[i]; !want.Eq(have) { + bench.Fatalf("input #%d mutated, have %x want %x", i, have, want) + } + } } func BenchmarkOpAdd64(b *testing.B) {