by cgoto89798 · 4y ago · 0 comments
It basically means you can do something like "goto opcode_table[*(++ip)];"
GCC offers it as a non-standard extension to C.
https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
FORTRAN has had it since 1957. But Pascal and C purged "evil computed GOTO" and only offered non-computed goto. Then Java etc. purged non-computed goto.I published a benchmark of various techniques of VM implementation today. It was conducted in C++ compliant C, but the results should be applicable to Rust. Just calling the function will always be the slowest due to the work the processor has to do for each instruction.
https://github.com/shadowofneptune/threaded-code-benchmark
Basically, you're better off with a massive switch statement, even if it's ugly in terms of program organization.
This paper https://www.cs.toronto.edu/~matz/dissertation/matzDissertati... (found the reference in https://coredumped.dev/2021/10/21/building-an-emacs-lisp-vm-...) suggests an interesting option which isn't portable, but a lot less work than a full JIT: generate just call and conditional branches. This was enough for them to basically fix all the branch mispredictions. I have yet to try it, but maybe someone else will feel inspired. The pain I ran into when trying is that a naively mapp'ed region will be very far from the code you are trying to call and you won't be able to use simple instructions. The fix is to try to allocate it explicitly closer.
PS: Another technique I have used in Rust (idea was from cloudflare) is to generate closures and have each closure tail call the next. It'll end up very similar to your continuation passing, but at least on my benchmarks it was _slightly_ worse than the naive dispatch.