5 ms·
On the topic of toy JVMs, my Metascala project is a JVM implemented in ~4000 lines of Scala that is complete enough to interpret itself! - https://github.com/l
by lihaoyi 6y ago
On the topic of toy JVMs, my Metascala project is a JVM implemented in ~4000 lines of Scala that is complete enough to interpret itself!
- https://github.com/lihaoyi/Metascala https://github.com/lihaoyi/Metascala
Interpreting code in Metascala is about 100x slower than just running it, and interpreting code in Metascala interpreted by Metascala is about 10,000x slower than just running it. Not going to win any performance benchmarks, but it's a cool demonstration of how a JVM works.
All the runtime data structures, memory allocation and garbage collection, method dispatch logic, stack trace management, exceptions, inheritance, object layouts, etc. are all implemented in a relatively small amount of relatively simple code.
For example, here is the implementation of the heap, which allocates the VM's objects inside a big byte array and has a simple copying semispace garbage collector to clean them up:
- https://github.com/lihaoyi/Metascala/blob/master/src/main/scala/metascala/Heap.scala https://github.com/lihaoyi/Metascala/blob/master/src/main/sc...
- miguendes 6y agoThis is awesome! I have been working on a interpreter for a toy language and my next step would be study the JVM. I'll definitely check your project out
- rightbyte 6y agoI like the commented out print statements all over the place. It make the code seem alive somehow.
- z3t4 6y agoMost of the performance overhead is probably the print messages as those are likely sync.
- Sevaris 6y agoAs always, I'm impressed with the amount of work you do. Have you ever talked about how you manage to be so productive?
- elric 6y agoAnd here's a talk by Ben Evans about implementing a JVM in Rust. https://www.youtube.com/watch?v=7ECbwgkHdAE https://www.youtube.com/watch?v=7ECbwgkHdAE
- grishka 6y ago> and interpreting code in Metascala interpreted by Metascala It's JVMs all the way down