The Ethereum The A virtual machine is distinct from other virtual devices. In Myself previous post I’ve It has been described in detail how it works, as well as some of its key features.
The Ethereum Virtual Machine (EVM), a simple yet powerful 256-bit encryption algorithm is now available. Turing Complete Virtual machines that anyone can run EVM Bytecode.
The The go-ethereum Project Two EVMs are included Implementations. Simple Direct bytecode virtual machine And You will be more sophisticated JIT-VM. In This Post I’ll Explain The differences between these two implementations. AlsoDescribe the features of JIT EVM and why it is faster than bytecode-EVM.
GoVirtual machine with ethereum bycode virtual codes
The The EVM internals are quite simple. There It is a single loop which will execute the statement at the instant. Program Counter (PC soon). Inside This The loop is Gas Each Instruction is calculated. Memory expands if necessary and executes the instruction if it succeeds. This Continue ) until the VM has been successfully closed or returned with an exception (e.g. Out The price of gas).
For Op = Contract[pc] {
If !It It is enoughGas(op) {
Return error("insufficient gas for op:"Or)
}
Change Op {
Case ...:
/* execute */
Case RETURN:
Return Memory[stack[-1]Stack[-2]]
}
pc++
}
At At The program counter increments at the end of each execution to execute the next instruction. The This process will continue until it is over.
The EVM offers an alternative method to obtain information Change The A counter-program can be achieved through something called jump-instructions (JUMP & JUMPY). Instead The program counters can be increased (pc ++), the EVM may jump to any position within the contract code. The EVM has two jump instructions. One The normal jump is described as “jump to X position” and a conditional jump that says “jump to position X if condition Y is true”. When such a jump occurs, you should always land on a jump target. If The If the program lands on a different instruction than the jump destination, it will crash.
Before Running Any Ethereum The EVM then goes through the code to find all possible hop destinations. It places them on the map that the program counter can reference to help them be found. Every The Each time that the EVM meets the jump instructions, the validity will be verified.
As As As you can see the code execution is simple and is easily interpreted using bytecodeVM. We Its simplicity can lead you to conclude it is dumb.
Welcome JIT virtual machine
The JIT-EVM allows you to execute EVM bytecodes in a novel way. Initially It This is slower than the bytecode virtual machine. Before Virtual Machines can execute any code if they are first authorized to do so. Compile The The JIT converts bytecode into the components it uses. Virtual Machine can understand.
The Initialization Execution and preparation are done in three steps
- We You Check to see if there’s a JIT program that can run with this hash code.H(C) It is used to identify the program
- If We find a program and execute it. Then we return the results.
- If If we don’t find any program, the bytecode will be executed. Y We In the background, compile a JIT program.
InitiallyI double-checked that the JIT had completed compilation and transferred the execution to JIT. All This occurred in runtime within the exact same loop. Go’s Atomic Package – UnfortunatelyIt It turned out to be slower than allowing bytecodeVM to run and that each sequence call was completed by using the JIT code after compilation.
By The JIT compiles bytecode into logical components. This Allows the JIT to analyze the code more precisely and optimize it where and when needed.
For One Example: By compiling multiple optimizations, I was able achieve an astonishing optimization. Push Operation in one instruction Let’s Take a look at the CALL instruction; The Call Requires 7 insert instructions. Call requires 7 insert instructions One One, I’ve optimized this by taking all 7 statements and adding all 7 values in a single chunk. NowHe is always there. Start Instead It executes one of the seven push commands by adding the static segment immediately to the VM stack. Now of course this only works for static values (i.e. Push 0x10) These However, they are often found in the code.
Also, I optimized the Static jump instructions. Static Jumps Jumps that land in the exact same place every time (i.e. push 0x1, jump) and never change under any circumstances. By PreBy determining which jumps can be considered statically, we can determine whether they are valid within contract limits. If We can also create new instructions that replace the existing jump instructions. Push Y jumpInstruction The mark is Valid. This This It saves the VM having to perform two instruction. It This saves the expense of having to verify the jump was allowed and do an expensive hash map search to confirm the jump position.
Next Steps
Full This Modell could also include stack- and memory analyses. This could allow large parts of code to be combined into individual instructions. Also I would like to be included Execution Symbolic Convert The JIT is now a JITVM Once Programs are already large enough that I believe this step is logical.
Conclution
EITHERYour JITVM is much smarter than bytecodeVM, but it is still far from perfect (if it ever was). There There This structure could have many more clever tricks, but they are not feasible right now. The Execution Time is limited by what is possible. “reasonably” fast. If If We have the tools to help you optimize your virtual machine.
More Code reading
Cross posted from – https://medium.com/@jeff.ethereum/go-ethereums-jit-evm-27ef88277520#.1ed9lj7dz