vm: optimize SWAP instruction, refactor common code
Add `Swap` method to the Stack and use it for both SWAP and XSWAP. Avoid element popping and pushing (and associated accounting costs). 1.4M->1.5M 100K block import test before: real 3m51,885s user 5m54,744s sys 0m38,444s After: real 3m44,292s user 5m43,494s sys 0m34,741s
This commit is contained in:
parent
b78896f2e1
commit
2627628387
4 changed files with 115 additions and 30 deletions
|
@ -2,6 +2,7 @@ package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
@ -371,6 +372,23 @@ func (s *Stack) IterBack(f func(*Element)) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Swap swaps two elements on the stack without popping and pushing them.
|
||||||
|
func (s *Stack) Swap(n1, n2 int) error {
|
||||||
|
if n1 < 0 || n2 < 0 {
|
||||||
|
return errors.New("negative index")
|
||||||
|
}
|
||||||
|
if n1 >= s.len || n2 >= s.len {
|
||||||
|
return errors.New("too big index")
|
||||||
|
}
|
||||||
|
if n1 == n2 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
a := s.Peek(n1)
|
||||||
|
b := s.Peek(n2)
|
||||||
|
a.value, b.value = b.value, a.value
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// popSigElements pops keys or signatures from the stack as needed for
|
// popSigElements pops keys or signatures from the stack as needed for
|
||||||
// CHECKMULTISIG.
|
// CHECKMULTISIG.
|
||||||
func (s *Stack) popSigElements() ([][]byte, error) {
|
func (s *Stack) popSigElements() ([][]byte, error) {
|
||||||
|
|
|
@ -226,22 +226,36 @@ func TestSwapElemValues(t *testing.T) {
|
||||||
s.PushVal(2)
|
s.PushVal(2)
|
||||||
s.PushVal(4)
|
s.PushVal(4)
|
||||||
|
|
||||||
a := s.Peek(0)
|
assert.NoError(t, s.Swap(0, 1))
|
||||||
b := s.Peek(1)
|
|
||||||
|
|
||||||
// [ 4 ] -> a
|
|
||||||
// [ 2 ] -> b
|
|
||||||
|
|
||||||
aval := a.value
|
|
||||||
bval := b.value
|
|
||||||
a.value = bval
|
|
||||||
b.value = aval
|
|
||||||
|
|
||||||
// [ 2 ] -> a
|
|
||||||
// [ 4 ] -> b
|
|
||||||
|
|
||||||
assert.Equal(t, int64(2), s.Pop().BigInt().Int64())
|
assert.Equal(t, int64(2), s.Pop().BigInt().Int64())
|
||||||
assert.Equal(t, int64(4), s.Pop().BigInt().Int64())
|
assert.Equal(t, int64(4), s.Pop().BigInt().Int64())
|
||||||
|
|
||||||
|
s.PushVal(1)
|
||||||
|
s.PushVal(2)
|
||||||
|
s.PushVal(3)
|
||||||
|
s.PushVal(4)
|
||||||
|
|
||||||
|
assert.NoError(t, s.Swap(1, 3))
|
||||||
|
assert.Equal(t, int64(4), s.Pop().BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(1), s.Pop().BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(2), s.Pop().BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(3), s.Pop().BigInt().Int64())
|
||||||
|
|
||||||
|
s.PushVal(1)
|
||||||
|
s.PushVal(2)
|
||||||
|
s.PushVal(3)
|
||||||
|
s.PushVal(4)
|
||||||
|
|
||||||
|
assert.Error(t, s.Swap(-1, 0))
|
||||||
|
assert.Error(t, s.Swap(0, -3))
|
||||||
|
assert.Error(t, s.Swap(0, 4))
|
||||||
|
assert.Error(t, s.Swap(5, 0))
|
||||||
|
|
||||||
|
assert.NoError(t, s.Swap(1, 1))
|
||||||
|
assert.Equal(t, int64(4), s.Pop().BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(3), s.Pop().BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(2), s.Pop().BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(1), s.Pop().BigInt().Int64())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPopSigElements(t *testing.T) {
|
func TestPopSigElements(t *testing.T) {
|
||||||
|
|
23
pkg/vm/vm.go
23
pkg/vm/vm.go
|
@ -510,10 +510,10 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
|
||||||
v.estack.Push(v.estack.Dup(0))
|
v.estack.Push(v.estack.Dup(0))
|
||||||
|
|
||||||
case opcode.SWAP:
|
case opcode.SWAP:
|
||||||
a := v.estack.Pop()
|
err := v.estack.Swap(1, 0)
|
||||||
b := v.estack.Pop()
|
if err != nil {
|
||||||
v.estack.Push(a)
|
panic(err.Error())
|
||||||
v.estack.Push(b)
|
}
|
||||||
|
|
||||||
case opcode.TUCK:
|
case opcode.TUCK:
|
||||||
a := v.estack.Dup(0)
|
a := v.estack.Dup(0)
|
||||||
|
@ -587,18 +587,9 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
|
||||||
|
|
||||||
case opcode.XSWAP:
|
case opcode.XSWAP:
|
||||||
n := int(v.estack.Pop().BigInt().Int64())
|
n := int(v.estack.Pop().BigInt().Int64())
|
||||||
if n < 0 {
|
err := v.estack.Swap(n, 0)
|
||||||
panic("XSWAP: invalid length")
|
if err != nil {
|
||||||
}
|
panic(err.Error())
|
||||||
|
|
||||||
// Swap values of elements instead of reordering stack elements.
|
|
||||||
if n > 0 {
|
|
||||||
a := v.estack.Peek(n)
|
|
||||||
b := v.estack.Peek(0)
|
|
||||||
aval := a.value
|
|
||||||
bval := b.value
|
|
||||||
a.value = bval
|
|
||||||
b.value = aval
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case opcode.XTUCK:
|
case opcode.XTUCK:
|
||||||
|
|
|
@ -2409,6 +2409,68 @@ func TestCHECKMULTISIGGood(t *testing.T) {
|
||||||
assert.Equal(t, true, vm.estack.Pop().Bool())
|
assert.Equal(t, true, vm.estack.Pop().Bool())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSWAPGood(t *testing.T) {
|
||||||
|
prog := makeProgram(opcode.SWAP)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.estack.PushVal(4)
|
||||||
|
runVM(t, vm)
|
||||||
|
assert.Equal(t, 2, vm.estack.Len())
|
||||||
|
assert.Equal(t, int64(2), vm.estack.Pop().BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(4), vm.estack.Pop().BigInt().Int64())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSWAPBad1(t *testing.T) {
|
||||||
|
prog := makeProgram(opcode.SWAP)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(4)
|
||||||
|
checkVMFailed(t, vm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSWAPBad2(t *testing.T) {
|
||||||
|
prog := makeProgram(opcode.SWAP)
|
||||||
|
vm := load(prog)
|
||||||
|
checkVMFailed(t, vm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestXSWAPGood(t *testing.T) {
|
||||||
|
prog := makeProgram(opcode.XSWAP)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(1)
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.estack.PushVal(3)
|
||||||
|
vm.estack.PushVal(4)
|
||||||
|
vm.estack.PushVal(5)
|
||||||
|
vm.estack.PushVal(3)
|
||||||
|
runVM(t, vm)
|
||||||
|
assert.Equal(t, 5, vm.estack.Len())
|
||||||
|
assert.Equal(t, int64(2), vm.estack.Pop().BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(4), vm.estack.Pop().BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(3), vm.estack.Pop().BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(5), vm.estack.Pop().BigInt().Int64())
|
||||||
|
assert.Equal(t, int64(1), vm.estack.Pop().BigInt().Int64())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestXSWAPBad1(t *testing.T) {
|
||||||
|
prog := makeProgram(opcode.XSWAP)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(1)
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.estack.PushVal(-1)
|
||||||
|
checkVMFailed(t, vm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestXSWAPBad2(t *testing.T) {
|
||||||
|
prog := makeProgram(opcode.XSWAP)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(1)
|
||||||
|
vm.estack.PushVal(2)
|
||||||
|
vm.estack.PushVal(3)
|
||||||
|
vm.estack.PushVal(4)
|
||||||
|
vm.estack.PushVal(4)
|
||||||
|
checkVMFailed(t, vm)
|
||||||
|
}
|
||||||
|
|
||||||
func makeProgram(opcodes ...opcode.Opcode) []byte {
|
func makeProgram(opcodes ...opcode.Opcode) []byte {
|
||||||
prog := make([]byte, len(opcodes)+1) // RET
|
prog := make([]byte, len(opcodes)+1) // RET
|
||||||
for i := 0; i < len(opcodes); i++ {
|
for i := 0; i < len(opcodes); i++ {
|
||||||
|
|
Loading…
Reference in a new issue