Skip to content

Commit 93a18c5

Browse files
qianbinholiman
authored andcommitted
core/vm: reduce overhead in instructions-benchmark (ethereum#24860)
* core/vm: reduce footprint of OP benchmark * core/vm: for opBenchmark, add code to detect inputs mutation * Update core/vm/instructions_test.go Co-authored-by: Martin Holst Swende <[email protected]> * core/vm: opBenchmark, stop timer before sanity-test code Co-authored-by: Martin Holst Swende <[email protected]>
1 parent f2e82fe commit 93a18c5

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

core/vm/instructions_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,26 +284,33 @@ func opBenchmark(bench *testing.B, op executionFunc, args ...string) {
284284
var (
285285
env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
286286
stack = newstack()
287+
scope = &ScopeContext{nil, stack, nil}
287288
evmInterpreter = NewEVMInterpreter(env, env.Config)
288289
)
289290

290291
env.interpreter = evmInterpreter
291292
// convert args
292-
byteArgs := make([][]byte, len(args))
293+
intArgs := make([]*uint256.Int, len(args))
293294
for i, arg := range args {
294-
byteArgs[i] = common.Hex2Bytes(arg)
295+
intArgs[i] = new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
295296
}
296297
pc := uint64(0)
297298
bench.ResetTimer()
298299
for i := 0; i < bench.N; i++ {
299-
for _, arg := range byteArgs {
300-
a := new(uint256.Int)
301-
a.SetBytes(arg)
302-
stack.push(a)
300+
for _, arg := range intArgs {
301+
stack.push(arg)
303302
}
304-
op(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
303+
op(&pc, evmInterpreter, scope)
305304
stack.pop()
306305
}
306+
bench.StopTimer()
307+
308+
for i, arg := range args {
309+
want := new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
310+
if have := intArgs[i]; !want.Eq(have) {
311+
bench.Fatalf("input #%d mutated, have %x want %x", i, have, want)
312+
}
313+
}
307314
}
308315

309316
func BenchmarkOpAdd64(b *testing.B) {

0 commit comments

Comments
 (0)