Functional Challenges in Working with Virtual Machine Kill

I wrote a C virtual machine that has a call table populated with pointers to functions that provide functionality to VM opcodes. When the virtual machine is started, it first interprets the program, creating an array of indexes corresponding to the corresponding function in the call table for the provided operation code. Then it goes through the array, calling each function until it reaches the end.

Each instruction is extremely small, usually one line. Ideal for embedding. The problem is that the compiler does not know when any of the instructions of the virtual machine will be called, as was accepted at run time, so it cannot embed them. Overhead function calls and passing arguments kill the performance of my virtual machine. Any ideas on how to get around this?

+4
source share
2 answers

Below are some options for reducing overhead:

  • Declare functions as fastcall (or something similar) to reduce the overhead of passing arguments
  • Use a large switch case instead of a function pointer table (the compiler will be optimized for the jump table and you will remove the overhead from the actual function call)
  • Copy all the code for the virtual machine procedure in one place so that it can be run sequentially, and not return to the interpreter after each instruction.

In the end, you'll move on to JIT compilation, online profiling and re-optimization, as well as any other awesome stuff.

+5
source

There are many good techniques that you might want to learn. Here are two that I know:

  • Built-in caching - Essentially, searching for what continues to be received, and moving from a vtable search to simply adding a bunch of if statements that are sent to a statically known location. This method was used for a significant effect in the Self language and is one of the main JVM optimizations.

  • Trace Compiling one version of the polymorphic distribution part for each type, which can be used, but delay compilation until the code is run many times. Mozilla TraceMonkey In this case, the JavaScript interpreter uses this to get huge performance gains in many cases.

Hope this helps!

+2
source

Source: https://habr.com/ru/post/1335764/


All Articles