From 8faed04c97249c4d2671b96df3949b932b87fc3e Mon Sep 17 00:00:00 2001 From: qianbin Date: Thu, 12 May 2022 00:34:14 +0800 Subject: [PATCH 1/4] core/vm: reduce footprint of OP benchmark --- core/vm/instructions_test.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 36589a126957..10f15d25ccee 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -284,24 +284,23 @@ func opBenchmark(bench *testing.B, op executionFunc, args ...string) { var ( env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) stack = newstack() + scope = &ScopeContext{nil, stack, nil} evmInterpreter = NewEVMInterpreter(env, env.Config) ) 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() } } From 60bcef28ab13d86351335480da4d5265329d572f Mon Sep 17 00:00:00 2001 From: qianbin Date: Thu, 12 May 2022 15:26:02 +0800 Subject: [PATCH 2/4] core/vm: for opBenchmark, add code to detect inputs mutation --- core/vm/instructions_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 10f15d25ccee..d64907ac6cab 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -303,6 +303,13 @@ func opBenchmark(bench *testing.B, op executionFunc, args ...string) { op(&pc, evmInterpreter, scope) stack.pop() } + + for i, arg := range args { + origArg := new(uint256.Int).SetBytes(common.Hex2Bytes(arg)) + if !origArg.Eq(intArgs[i]) { + bench.Fatalf("input #%d mutated", i) + } + } } func BenchmarkOpAdd64(b *testing.B) { From 1e4f3141976e7655bf160166fd00078bbdd2d3a7 Mon Sep 17 00:00:00 2001 From: Qian Bin Date: Thu, 12 May 2022 15:56:16 +0800 Subject: [PATCH 3/4] Update core/vm/instructions_test.go Co-authored-by: Martin Holst Swende --- core/vm/instructions_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index d64907ac6cab..32959389f60a 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -305,9 +305,9 @@ func opBenchmark(bench *testing.B, op executionFunc, args ...string) { } for i, arg := range args { - origArg := new(uint256.Int).SetBytes(common.Hex2Bytes(arg)) - if !origArg.Eq(intArgs[i]) { - bench.Fatalf("input #%d mutated", i) + 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) } } } From 3a4b77d5d5c624190a40e7e0525aaa642d95c1d9 Mon Sep 17 00:00:00 2001 From: qianbin Date: Fri, 13 May 2022 15:59:58 +0800 Subject: [PATCH 4/4] core/vm: opBenchmark, stop timer before sanity-test code --- core/vm/instructions_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 32959389f60a..236cd929167b 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -303,10 +303,11 @@ func opBenchmark(bench *testing.B, op executionFunc, args ...string) { 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) { + if have := intArgs[i]; !want.Eq(have) { bench.Fatalf("input #%d mutated, have %x want %x", i, have, want) } }