core/vm: handwrite push2 opcode

This commit is contained in:
Marius van der Wijden 2025-02-26 12:05:19 +01:00
parent f485e213b6
commit 5841899bd5
2 changed files with 47 additions and 0 deletions

View file

@ -17,6 +17,7 @@
package vm
import (
"encoding/binary"
"math"
"github.com/ethereum/go-ethereum/common"
@ -971,6 +972,23 @@ func opPush1(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]by
return nil, nil
}
// opPush1 is a specialized version of pushN
func opPush2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var (
codeLen = uint64(len(scope.Contract.Code))
integer = new(uint256.Int)
)
if *pc+2 < codeLen {
scope.Stack.push(integer.SetUint64(uint64(binary.BigEndian.Uint16(scope.Contract.Code[*pc : *pc+2]))))
} else if *pc+1 < codeLen {
scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc]) << 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 *EVMInterpreter, scope *ScopeContext) ([]byte, error) {

View file

@ -972,3 +972,32 @@ func TestPush(t *testing.T) {
}
}
}
func BenchmarkPush(b *testing.B) {
var (
code = common.FromHex("0011223344556677889900aabbccddeeff0102030405060708090a0b0c0d0e0ff1e1d1c1b1a19181716151413121")
push32 = makePush(2, 2)
scope = &ScopeContext{
Memory: nil,
Stack: newstack(),
Contract: &Contract{
Code: code,
},
}
pc = new(uint64)
)
b.Run("makePush", func(b *testing.B) {
for i := 0; i < b.N; i++ {
push32(pc, nil, scope)
scope.Stack.pop()
}
})
b.Run("push", func(b *testing.B) {
for i := 0; i < b.N; i++ {
opPush2(pc, nil, scope)
scope.Stack.pop()
}
})
}