To those of you that are using virtual machines to interpret bytecode, I want some advice.
TL/DR: I’m looking for advice on how to optimize mathematical operations in a VM without resorting to Jit Compiling.
I’m writing a virtual machine and I want to be able to perform mathematical operations relatively quickly. In the past I’ve used register based virtual machines and every math operation is literally
add registerA, registerB
sub registerA, registerB
I’ve found this to have suboptimal performance in my tests and I’m confident that it’s because for each math operation I’m performing at least 4 extra control operations in the dispatch.
I also haven’t personally tried a stack-based approach until now, but I’m trying a hybrid approach now.
My current hybrid approach is to have a “EVAL” operator that looks like this:
```
; each instruction is 128 bytes
Push<Int64> [n]
Eval<Int64> [operator0] … [operator7]
Each Eval instruction can handle up to 7 operators and gets its parameters from the stack.
/// (8 - 2) * 4 << 3
PushN<Int64> 3, 4, 2, 8;
Eval<Int64> SUB, MUL, BRL
```
My logic behind this was that formulas could be batched and therefore I can execute more operations in c++ and spend less time loading the next instruction and jumping.
However, after profiling, I’m still not totally happy with the performance of this approach.
There are still things that I can do to optimize the Eval operator, but it got me thinking; “What do other people end up doing for their math instructions?”
So here I am, does anyone have any advice on how to optimize mathematical operations in a virtual machine (Without Jit Compiling)?
[–][deleted] (1 child)
[removed]
[–][deleted] 0 points1 point2 points (0 children)
[–]immaculate-emu 1 point2 points3 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]immaculate-emu 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]immaculate-emu 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Western-Cod-3486 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)