From 6d6a7334768de4c8bd0d8c1f82bfada3c7bbfdc2 Mon Sep 17 00:00:00 2001 From: ucwong Date: Thu, 10 Apr 2025 17:14:08 +0800 Subject: [PATCH] optimized push2 --- core/vm/instructions.go | 17 +++++++++++++++++ core/vm/jump_table.go | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index d2ab5c4ca3..9ac845a345 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -1216,6 +1216,23 @@ func opPush1(pc *uint64, interpreter *CVMInterpreter, scope *ScopeContext) ([]by return nil, nil } +// opPush2 is a specialized version of pushN +func opPush2(pc *uint64, interpreter *CVMInterpreter, scope *ScopeContext) ([]byte, error) { + var ( + codeLen = uint64(len(scope.Contract.Code)) + integer = new(uint256.Int) + ) + if *pc+2 < codeLen { + scope.Stack.push(integer.SetBytes2(scope.Contract.Code[*pc+1 : *pc+3])) + } else if *pc+1 < codeLen { + scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc+1]) << 8)) + } else { + scope.Stack.push(integer.Clear()) + } + *pc += 2 + return nil, nil +} + // make push instruction function func makePush(size uint64, pushByteSize int) executionFunc { return func(pc *uint64, interpreter *CVMInterpreter, scope *ScopeContext) ([]byte, error) { diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go index 1d23ff435c..59b6704e72 100644 --- a/core/vm/jump_table.go +++ b/core/vm/jump_table.go @@ -501,7 +501,7 @@ func newFrontierInstructionSet() JumpTable { validateStack: makeStackFunc(0, 1), }, PUSH2: { - execute: makePush(2, 2), + execute: opPush2, gasCost: gasPush, validateStack: makeStackFunc(0, 1), },