mirror of https://github.com/ethereum/go-ethereum
Merge pull request #1883 from obscuren/jit-vm-optimisations
core/vm: JIT segmentationpull/1840/head
commit
58d0752fdd
@ -0,0 +1,107 @@ |
||||
package vm |
||||
|
||||
import ( |
||||
"math/big" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/logger" |
||||
"github.com/ethereum/go-ethereum/logger/glog" |
||||
) |
||||
|
||||
// optimeProgram optimises a JIT program creating segments out of program
|
||||
// instructions. Currently covered are multi-pushes and static jumps
|
||||
func optimiseProgram(program *Program) { |
||||
var load []instruction |
||||
|
||||
var ( |
||||
statsJump = 0 |
||||
statsPush = 0 |
||||
) |
||||
|
||||
if glog.V(logger.Debug) { |
||||
glog.Infof("optimising %x\n", program.Id[:4]) |
||||
tstart := time.Now() |
||||
defer func() { |
||||
glog.Infof("optimised %x done in %v with JMP: %d PSH: %d\n", program.Id[:4], time.Since(tstart), statsJump, statsPush) |
||||
}() |
||||
} |
||||
|
||||
/* |
||||
code := Parse(program.code) |
||||
for _, test := range [][]OpCode{ |
||||
[]OpCode{PUSH, PUSH, ADD}, |
||||
[]OpCode{PUSH, PUSH, SUB}, |
||||
[]OpCode{PUSH, PUSH, MUL}, |
||||
[]OpCode{PUSH, PUSH, DIV}, |
||||
} { |
||||
matchCount := 0 |
||||
MatchFn(code, test, func(i int) bool { |
||||
matchCount++ |
||||
return true |
||||
}) |
||||
fmt.Printf("found %d match count on: %v\n", matchCount, test) |
||||
} |
||||
*/ |
||||
|
||||
for i := 0; i < len(program.instructions); i++ { |
||||
instr := program.instructions[i].(instruction) |
||||
|
||||
switch { |
||||
case instr.op.IsPush(): |
||||
load = append(load, instr) |
||||
case instr.op.IsStaticJump(): |
||||
if len(load) == 0 { |
||||
continue |
||||
} |
||||
// if the push load is greater than 1, finalise that
|
||||
// segment first
|
||||
if len(load) > 2 { |
||||
seg, size := makePushSeg(load[:len(load)-1]) |
||||
program.instructions[i-size-1] = seg |
||||
statsPush++ |
||||
} |
||||
// create a segment consisting of a pre determined
|
||||
// jump, destination and validity.
|
||||
seg := makeStaticJumpSeg(load[len(load)-1].data, program) |
||||
program.instructions[i-1] = seg |
||||
statsJump++ |
||||
|
||||
load = nil |
||||
default: |
||||
// create a new N pushes segment
|
||||
if len(load) > 1 { |
||||
seg, size := makePushSeg(load) |
||||
program.instructions[i-size] = seg |
||||
statsPush++ |
||||
} |
||||
load = nil |
||||
} |
||||
} |
||||
} |
||||
|
||||
// makePushSeg creates a new push segment from N amount of push instructions
|
||||
func makePushSeg(instrs []instruction) (pushSeg, int) { |
||||
var ( |
||||
data []*big.Int |
||||
gas = new(big.Int) |
||||
) |
||||
|
||||
for _, instr := range instrs { |
||||
data = append(data, instr.data) |
||||
gas.Add(gas, instr.gas) |
||||
} |
||||
|
||||
return pushSeg{data, gas}, len(instrs) |
||||
} |
||||
|
||||
// makeStaticJumpSeg creates a new static jump segment from a predefined
|
||||
// destination (PUSH, JUMP).
|
||||
func makeStaticJumpSeg(to *big.Int, program *Program) jumpSeg { |
||||
gas := new(big.Int) |
||||
gas.Add(gas, _baseCheck[PUSH1].gas) |
||||
gas.Add(gas, _baseCheck[JUMP].gas) |
||||
|
||||
contract := &Contract{Code: program.code} |
||||
pos, err := jump(program.mapping, program.destinations, contract, to) |
||||
return jumpSeg{pos, err, gas} |
||||
} |
@ -0,0 +1,68 @@ |
||||
// Copyright 2014 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package vm |
||||
|
||||
// Parse parses all opcodes from the given code byte slice. This function
|
||||
// performs no error checking and may return non-existing opcodes.
|
||||
func Parse(code []byte) (opcodes []OpCode) { |
||||
for pc := uint64(0); pc < uint64(len(code)); pc++ { |
||||
op := OpCode(code[pc]) |
||||
|
||||
switch op { |
||||
case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32: |
||||
a := uint64(op) - uint64(PUSH1) + 1 |
||||
pc += a |
||||
opcodes = append(opcodes, PUSH) |
||||
case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16: |
||||
opcodes = append(opcodes, DUP) |
||||
case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16: |
||||
opcodes = append(opcodes, SWAP) |
||||
default: |
||||
opcodes = append(opcodes, op) |
||||
} |
||||
} |
||||
|
||||
return opcodes |
||||
} |
||||
|
||||
// MatchFn searcher for match in the given input and calls matcheFn if it finds
|
||||
// an appropriate match. matcherFn yields the starting position in the input.
|
||||
// MatchFn will continue to search for a match until it reacher the end of the
|
||||
// buffer or if matcherFn return false.
|
||||
func MatchFn(input, match []OpCode, matcherFn func(int) bool) { |
||||
// short circuit if either input or match is empty or if the match is
|
||||
// greater than the input
|
||||
if len(input) == 0 || len(match) == 0 || len(match) > len(input) { |
||||
return |
||||
} |
||||
|
||||
main: |
||||
for i, op := range input[:len(input)+1-len(match)] { |
||||
// match first opcode and continue search
|
||||
if op == match[0] { |
||||
for j := 1; j < len(match); j++ { |
||||
if input[i+j] != match[j] { |
||||
continue main |
||||
} |
||||
} |
||||
// check for abort instruction
|
||||
if !matcherFn(i) { |
||||
return |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,84 @@ |
||||
// Copyright 2014 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package vm |
||||
|
||||
import "testing" |
||||
|
||||
type matchTest struct { |
||||
input []OpCode |
||||
match []OpCode |
||||
matches int |
||||
} |
||||
|
||||
func TestMatchFn(t *testing.T) { |
||||
tests := []matchTest{ |
||||
matchTest{ |
||||
[]OpCode{PUSH1, PUSH1, MSTORE, JUMP}, |
||||
[]OpCode{PUSH1, MSTORE}, |
||||
1, |
||||
}, |
||||
matchTest{ |
||||
[]OpCode{PUSH1, PUSH1, MSTORE, JUMP}, |
||||
[]OpCode{PUSH1, MSTORE, PUSH1}, |
||||
0, |
||||
}, |
||||
matchTest{ |
||||
[]OpCode{}, |
||||
[]OpCode{PUSH1}, |
||||
0, |
||||
}, |
||||
} |
||||
|
||||
for i, test := range tests { |
||||
var matchCount int |
||||
MatchFn(test.input, test.match, func(i int) bool { |
||||
matchCount++ |
||||
return true |
||||
}) |
||||
if matchCount != test.matches { |
||||
t.Errorf("match count failed on test[%d]: expected %d matches, got %d", i, test.matches, matchCount) |
||||
} |
||||
} |
||||
} |
||||
|
||||
type parseTest struct { |
||||
base OpCode |
||||
size int |
||||
output OpCode |
||||
} |
||||
|
||||
func TestParser(t *testing.T) { |
||||
tests := []parseTest{ |
||||
parseTest{PUSH1, 32, PUSH}, |
||||
parseTest{DUP1, 16, DUP}, |
||||
parseTest{SWAP1, 16, SWAP}, |
||||
parseTest{MSTORE, 1, MSTORE}, |
||||
} |
||||
|
||||
for _, test := range tests { |
||||
for i := 0; i < test.size; i++ { |
||||
code := append([]byte{byte(byte(test.base) + byte(i))}, make([]byte, i+1)...) |
||||
output := Parse(code) |
||||
if len(output) == 0 { |
||||
t.Fatal("empty output") |
||||
} |
||||
if output[0] != test.output { |
||||
t.Error("%v failed: expected %v but got %v", test.base+OpCode(i), output[0]) |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,44 @@ |
||||
package vm |
||||
|
||||
import "math/big" |
||||
|
||||
type jumpSeg struct { |
||||
pos uint64 |
||||
err error |
||||
gas *big.Int |
||||
} |
||||
|
||||
func (j jumpSeg) do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) ([]byte, error) { |
||||
if !contract.UseGas(j.gas) { |
||||
return nil, OutOfGasError |
||||
} |
||||
if j.err != nil { |
||||
return nil, j.err |
||||
} |
||||
*pc = j.pos |
||||
return nil, nil |
||||
} |
||||
func (s jumpSeg) halts() bool { return false } |
||||
func (s jumpSeg) Op() OpCode { return 0 } |
||||
|
||||
type pushSeg struct { |
||||
data []*big.Int |
||||
gas *big.Int |
||||
} |
||||
|
||||
func (s pushSeg) do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) ([]byte, error) { |
||||
// Use the calculated gas. When insufficient gas is present, use all gas and return an
|
||||
// Out Of Gas error
|
||||
if !contract.UseGas(s.gas) { |
||||
return nil, OutOfGasError |
||||
} |
||||
|
||||
for _, d := range s.data { |
||||
stack.push(new(big.Int).Set(d)) |
||||
} |
||||
*pc += uint64(len(s.data)) |
||||
return nil, nil |
||||
} |
||||
|
||||
func (s pushSeg) halts() bool { return false } |
||||
func (s pushSeg) Op() OpCode { return 0 } |
Loading…
Reference in new issue