8 ms·
MiniVM: A zero-dependency cross-language runtime on par with LuaJIT and C
- iainmerrick 5y agoThis looks like a fun project, but claiming that it “beats luajit” is very premature based on the benchmarks listed. The README lists only an allocation stress test (fast allocs are good, to be sure) and recursive Fibonacci (always good fun to try but doesn’t resemble real usage). Looks like it doesn’t have a JIT compiler, and without that I’d put a lot of money against it ever being as fast as LuaJIT, V8, the JVM, etc etc in the general case. Edit to add: on reading more closely, there’s more benchmark-gaming going on here than I realised. On the second benchmark, it says: As you can see, minivm (no JIT) is a hair slower than Node JS (JIT) but beats luajit --joff by a fair margin (no JIT). They already noted that Node has a longer startup time, so that obviously dominates in a benchmark that’s only a few milliseconds long; and disabling the JIT in LuaJIT doesn’t tell us much.
- CalChris 5y agoFibonacci is the poster child of hotspots. A JIT should be crushing an interpreter at Fibonacci as it is simple to generate good code for. Yes, it's just a microbenchmark; so you shouldn't make SPEC type conclusions from it. But you shouldn't dismiss it either.
- isaacimagine 5y agoPersonally, I find it pretty impressive that it performs as well as these runtimes despite not having a JIT compiler. I'm pretty sure Shaw's written more benchmarks, but as the README explains, it's really hard to tell what the performance characteristics of a language are without writing a larger application. So far the largest applications written with MiniVM are Paka[0], a self-hosted language similar to Lua that targets MiniVM; os49[1], an operating system built on Paka/MiniVM in the spirit of lisp machines; and xori[2], an online playground for the language. Edit: to address your edit, the the Tree benchmark starts small, but grows exponentially (the graph is logarithmic). The final runs of the benchmark take about 15 seconds to run, and are run 10 times per language, at which point startup time is no longer a large factor. The fib test compares both luajit with the JIT on and JIT off: as MiniVM is not JIT'd, I think that this is a fair comparison to make. [0]: https://github.com/FastVM/paka https://github.com/FastVM/paka [1]: https://github.com/ShawSumma/os49 https://github.com/ShawSumma/os49 [2]: https://fastvm.github.io/xori https://fastvm.github.io/xori
- iainmerrick 5y agoSure, it’s a nice piece of work! I just don’t think you should claim it’s very fast unless you can show that it actually is very fast. The current benchmarks look like toy benchmarks. (I don’t mean that in a derogatory way -- they’re exactly the kind of tiny programs you need to build and test while bringing up a new VM.)
- mst 5y agoThe disclaimer at the top of the benchmark section - https://github.com/FastVM/minivm#benchmarks https://github.com/FastVM/minivm#benchmarks - seemed pretty reasonable to me. It's very clear that microbenchmarks don't tell the whole story, and that whole application benchmarks to give more real results will be desirable later. I mean, I agree these benchmarks don't mean that much except for being a useful way to verify that the core code is working and reasonably performant - but they're up front about that fact so calling it 'benchmark-gaming' seems a trifle unfair.
- 4984 5y agoMiniVM will JIT eventually. Right now the biggest barrier is the VM snapshots. Currently MiniVM its own stack and heap and can snapshot them from any point in the program. One thing to note about MiniVM is that it has very strong types. V8 has many steps to go through when it needs to perform something like addition (weak typing). MiniVM supports only 4 basic types. MiniVM's reference frontend Paka is fully self hosted, It is currently the only large MiniVM program.
- samatman 5y agoThat sounds like a solid basis for adding asymmetric coroutines as well, is that something you're thinking about adding? I like the philosophy and gumption shown by this project but coroutines aren't a feature I'd lightly give up. Digging what I see so far!
- 4984 5y agoMiniVM uses coroutines already, every 1000 branches the vm will return to the scheduler. The Build system can accept CFLAGS+=-DVM_BRANCH_DEFER which makes MiniVM able to run part of bytecode up-to N instructions. It is not exposed on the insruction level yet tho. Would be quite easy to add.
- exikyut 5y agoIt might be interesting to be able to tell the VM to resume into code for exactly N instructions or branches. I can see instrumentation/debugging tooling using this with N=1 for example.
- samatman 5y agoAh thank you, that's very promising! It was the kind of thing I wanted to hear, I write Lua every day and four data types didn't appear to support 'thread'. I might just kick the tires on this over the weekend.
- rgrmrts 5y agoThat’s cool. Have you played around with that number? I’m curious how you pick that constant and what the trade offs are
- eps 5y agoAnything beats something on some benchmarks. That's not a very captivating pitch point. Good stuff otherwise. Very nice project.
- 4984 5y agoThank you for having a look. The reason MiniVM is so benchmark-oriented in its presentation is that MiniVM is ver benchmark-oriented in my workflow. I have tried to find a faster interpreter without JIT. It is hard to find benchmarks that don't just say that MiniVM is 2-10x faster than the interpreted languages.
- iainmerrick 5y agoYeah, it’s a bit disappointing how slow most non-JIT interpreters are -- I think JIT understandably took the wind out of their sails. The state of the art as I understand it (but maybe you’ve figured out something faster?) is threaded interpreters. I suggest looking at Forth implementations, which is where I think most interpreter innovation has happened.
- isaacimagine 5y agoSo I was talking about threaded code with Shaw the other day, and he gave me a long explanation about the various tradeoffs being made and the resulting techniques used in MiniVM. As I understand it, MiniVM is basically threaded code, with cached opcodes. Essentially, it uses precomputed gotos: // classical computed goto goto *x[y[z]] // MiniVM precomputed goto goto *a[z] This optimization makes MiniVM a tad faster than raw computed gotos, because it's essentially threaded code with some optimizations. He can probably expand on this; if you want to hear the full really interesting explanation yourself, he's pretty active in the Paka Discord[0]. [0]: https://discord.gg/UyvxuC5W5q https://discord.gg/UyvxuC5W5q
- aardvark179 5y agoThat feels like the sort of optimisation that wins in small cases and loses in larger ones. The jump array is going to be 4 or 8 times the size of the opcode array so will put a lot more pressure on the caches as your programs get larger.
- tonyg 5y agoHard to tell whether it supports proper tail calls or not. The readme mentions Scheme, so it's not impossible I suppose! One of the nice things about wasm is the formal semantics; a project like this could benefit from one of those, too.
- 4984 5y agoAuthor here, Tail calls can be implemented in MiniVM bytecode. There was once an instruction that performed a tail call, It bit-rotted due to not being used. MiniVM is moving very quickly currently. A lot still needs to be done, like JIT and optimizers. Eventually a formal specification of MiniVM should happen, just not yet as things are changing too fast.
- user-the-name 5y agoSpeaking of wasm, any idea how feasible it would be to convert wasm to minivm bytecode and use it as a wasm runner?
- deleted 5y ago[deleted]
- fastkebab 5y ago
- wruza 5y ago4984, there is a roadmap, but I couldn't find info on strategic goals of this project or where it supposed to be a best fit. Can you please share your views on this? Also, is there an API to connect to to emit VM bytecodes? Or an instruction set reference. I'm asking out of pure fan-like interest in this field and because I see lack of simple-enough (for me) tools for language building. Don't take me serious or anything like that, I'm not a pro. I tried to dig to llvm few years ago, but so c++ much boilerplate, meh. I know I can just transpile to Lua or js with sourcemaps, but it's zero fun. Projects like this remind me of 8080 asm times.
- 4984 5y agoThere is no roadmap. There are no written long term goals. This project came out of need for a faster virtual machine for my Paka language. There is an api to emit vm bytecode, but it's not in C, it is from Paka. Roadmap could go like this tho: More Core Opcodes (Bit Ops, Closures), Split overloaded opcodes. More frontends. Optimizer, Dynamic Compiler and JIT.
- childintime 5y agoThis is a nice Forth-style bytecode interpreter, that is easy to generate code for. Close to WASM, but much simpler. The thing that attracts me most is that the bytecode format can be easily translated to a more compact format, allowing it serve as an intermediate format for _many_ homegrown instruction sets. So a GCC or LLVM backend needs to be on the roadmap. Code generation is a blast. Only the number of registers should be user-configurable to make this useful.
- kgeist 5y ago>Built on a register-based ISA that beats luajit—with the JIT on—in some benchmarks This is interesting, considering that LuaJIT in interpreter mode is famous for being faster than simple JITs. Worth looking into what the differences are.
- isaacimagine 5y agoFrom a discussion with Shaw: - Simpler more consistent type system - MiniVM has more larger instructions to take advantage of - MiniVM precomputes and caches jumps where possible - Calling functions is more efficient, less overhead - Has a custom GC/allocator which allows for some optimizations - Fixed-sized arrays vs Lua's dynamic tables - Not a JIT, so faster startup times (no tracing)
- kaba0 5y agoWouldn’t a templated interpreter be perhaps faster (eg. what is used by the JVM)?
- mst 5y agoThis example seems a bit pessimised: head_of_loop: load_var x push_int 1000 less_than jump_if_false :end_of_loop load_var x increment store_var x end_of_loop: I'd more expect a stack engine to be something like something like - load_var x head_of_loop: jump_lt_keep 1000 :end_of_loop increment end_of_loop: store_var x in the sense that anything that can figure out how to populate and use r0 would likely also be able to figure out it can wrap the load/store calls around the outside of the loop. (I'm far from an expert on stack versus register VMs though so I may be missing several somethings here)
- tln 5y agoThat would be a nice optimization. Here's what Python does 0 LOAD_FAST 0 (x) 2 LOAD_CONST 1 (1000) 4 COMPARE_OP 0 (<) 6 POP_JUMP_IF_FALSE 18 8 LOAD_FAST 0 (x) 10 LOAD_CONST 2 (1) 12 BINARY_ADD 14 STORE_FAST 0 (x) 16 JUMP_ABSOLUTE 0 18 LOAD_CONST 0 (None) Python's VM is very slow, but note only one arg per op and the compare + jump are not fused. BTW, if we're considering "advanced" optimization might as well elide the whole loop :) EG https://godbolt.org/z/n3GznqKsM https://godbolt.org/z/n3GznqKsM test(int): push rbp mov rbp, rsp mov DWORD PTR [rbp-4], edi jmp .L2 .L3: add DWORD PTR [rbp-4], 1 .L2: cmp DWORD PTR [rbp-4], 999 jle .L3 nop pop rbp ret
- mst 5y agoWell, sure, but I was thinking along the lines of "reserving a spot on the stack and reserving a register seem like about the same level of clever", especially thinking about how mixed register/stack argument passing works in even relatively stupid C compilers. Eliding the entire loop seems relatively similarly clever for either architecture, but would've counted as cheating in the frame I was pondering from. Still neat to see even if x86 asm continues to make my eyes cross ;)
- exikyut 5y ago
- celeritascelery 5y agoIs there any documentation on the bytecode? I would be interested to see their design choices.
- isaacimagine 5y agoCheck the code, there's a big list of opcodes with pretty descriptive names. The code is effectively the documentation for now.
- sitkack 5y agoHow does this compare to the techniques used in the wasm3 (name, not description)? https://github.com/wasm3/wasm3 https://github.com/wasm3/wasm3 architecture of M3 the interpreter core of wasm3 https://github.com/wasm3/wasm3/blob/main/docs/Interpreter.md https://github.com/wasm3/wasm3/blob/main/docs/Interpreter.md https://github.com/wasm3/wasm3/blob/main/docs/Performance.md https://github.com/wasm3/wasm3/blob/main/docs/Performance.md
- 4984 5y agoI have looked into Meta Machines. They are not what I want. Meta Machines can be almost as fast, but i just prefer the cache locality of local functions. Wasm3 is slower than MiniVM when you don't count the GC. Wasm3 is very cool!
- sitkack 5y agoHow do the architectures of the two interpreters differ? Not trying to challenge your system but learn. Can Wasm be translated to run on MiniVM? If MiniVM is pure C it can run on Wasm.
- adontz 5y agoDoes it support any kind of debugging?
- djwatson24 5y agoBinary trees is a classic GC benchmark - ideally a GC'd language should be able to do better than a malloc/free implementation in C (tree.c) since it can release the whole tree in bulk (like what the ptree.c does). Also note that tree.c speeds up substantially if you link with a better malloc like jemalloc or tcmalloc.
- steeleduncan 5y agoThe Readme mentions that they've experimented with a Lua frontend. I can't find any reference to this in the repo. Is this a private experiment, or is it available somewhere?
- ford_o 5y agoHow does this compare to MIR?
- rurban 5y agoMir has a better compiler, and a jit. This has a better op and data layout, and a fast interpreter. Apples and oranges
- thayne 5y agoLooks like an interesting project, but I can't find any documentation on how to actually use it (or on the paka language either). Is it just still in early stages and documentation hasn't been written yet?
- slightknack 5y agoI've written some documentation for it in the past, but had to scrap it because things tend to move so quickly that it's better to read the code and ask Shaw than use incorrect documentation. As things stabilize, these issues will be resolved, but it's very bleeding-edge right now.
- rahimiali 5y agoThe main interpreter loop looks a lot like python’s. It uses the same pre processor tricks and jump tables to help both the reader and the compiler to make sense of the flow. It’s also near that integers are unboxed, at the expensive of their top two bits being reserved as a type hint.
- jng 5y agoNot only the reader and the compiler, that kind of threaded code really helps performance thanks to leveraging the CPU's branch target buffer to obtain great branch prediction of interpreted code, and that is one of the killer aspects to get current CPUs to perform well.
- shuffel 5y agoHow are strings represented in the VM? Is there a benchmark for string operations?
- shuffel 5y agoStrings appear to be represented as arrays with elements which are 4 bytes in size, with 30 numeric bits for each character: https://github.com/FastVM/minivm/blob/4906256b2dc16e227cad7ec8c1986fda1ce52b18/vm/type.h#L23-L32 https://github.com/FastVM/minivm/blob/4906256b2dc16e227cad7e... https://github.com/FastVM/minivm/blob/4906256b2dc16e227cad7ec8c1986fda1ce52b18/vm/vm.c#L293-L304 https://github.com/FastVM/minivm/blob/4906256b2dc16e227cad7e...