I wanted to get more experience writing Rust so I decided to build an interpreter in Rust and attempt to make it just as fast as another one I made in C. In this post I describe my journey to make it run fast. Most of it will probably be super obvious if you're experienced with Rust, but hopefully some of it is useful to more people. For example, I feel it's just too easy to call `.clone()` to store an owned value instead of storing a reference and introducing the correct lifetime constraints.
Overall it was (is!) a fun project and working in Rust has been a very nice experience. Let me know if you have any suggestions please.
There's three levels when on the ownership ladder:
1. Taking ownership of an object: this means your have mutability and 'static lifetime. This often implies cloning though.
2. Having the object behind a ref-counted pointer (Rc or Arc in multi-trhreaded scenario) this way you have 'static but not mutability (unless you're using interior mutability with RefCell/Mutex but now you have to think about deadlock and all)
3. Taking shared references (&) to your object. It's the most performant but now you have to deal with lifetimes so it's not always worth it.
Rust beginners often jump from 1. to 3. (Or don't because “too tedious”) but 2. is a sweet spot that works in many cases and should not be overlooked!
That’s ‘partial evaluation’ - I have a Ruby JIT that works like this in Java.
Seeing just how slow looking up variables by their name is, I have a feeling this probably amounts to the biggest performance improvement that the bytecode + VM brings to the table (it's ~3 times faster than the AST walker at the time of writing). Especially as there do not seem to be that many cache misses (at least on my hardware) already, sub .2%.
Anyway, thank you for your $.02!
Thank you for writing this and sharing this.
I began writing a multithreaded interpreter. I just use strings for instructions to begin with. Adding bytecode support would be straightforward.
Then I began writing a compiler for a high level language and the codegen targeting the interpreter. The language looks similar to JavaScript and I plan to add multithreading support that the interpreter supports.
Code is here https://GitHub.com/samsquire/multiversion-concurrency-contro...
There was a very interesting article about Deegen a generated Lua interpreter.
Java's template interpreter is interesting too.
HolyC is interesting since it compiles at runtime and then runs the code.