How to make a fast dynamic language interpreter
Posted by pizlonator 8 hours ago
Comments
Comment by pansa2 3 hours ago
Unlike the Zef article, which describes implementation techniques, the Wren page also shows ways in which language design can contribute to performance.
In particular, Wren gives up dynamic object shapes, which enables copy-down inheritance and substantially simplifies (and hence accelerates) method lookup. Personally I think that’s a good trade-off - how often have you really needed to add a method to a class after construction?
Comment by versteegen 33 minutes ago
Comment by psychoslave 2 hours ago
On the other side, having a type holding a closed set of applicable functions is somehow questioning.
There are languages out there that allows to define arbitrary functions and then use them as a methods with dot notation on any variable matching the type of the first argument, including Nim (with macros), Scala (with implicit classes and type classes), Kotlin (with extension functions) and Rust (with traits).
Comment by jiusanzhou 2 hours ago
Comment by grg0 7 hours ago
I also like how, according to Github, the repo is 99.7% HTML and 0.3% C++. A testament to the interpreter's size, I guess?
Comment by pizlonator 7 hours ago
But yeah the interpreter is very small
Comment by injidup 3 hours ago
Comment by electroly 3 hours ago
Comment by tiffanyh 5 hours ago
Comment by pizlonator 5 hours ago
There are many runtimes that I could have included but didn’t.
Also, it’s quite impressive how much faster PUC Lua is than QuickJS and Python
Comment by raincole 4 hours ago
(I suppose the quick in QuickJS means "quick for a pure interpreter without JIT compilation or something...)
Comment by pizlonator 4 hours ago
So like that’s wild
Comment by zephen 5 hours ago
Python's execution time is mostly spent looking up stuff. I don't think lua is quite as dynamic.
Comment by pizlonator 5 hours ago
Comment by zephen 4 hours ago
But in Python, everything is an object, which is why, as I said, it spends much of its time looking things up. And things like bindings for closures are late, so that's more lookups as well.
In lua, many things aren't objects, and, for example, you can add two numbers without looking anything up. Another issue, of course, when you do that, is that you could conceivably overflow an integer, but that can't happen in Python either.
The Python interpreter has some fast paths for specific object types, but it is really limited in the optimizations it can do, because there simply aren't any unboxed types.
Comment by psychoslave 1 hour ago
Comment by zephen 42 minutes ago
Comment by boulos 6 hours ago
Comment by pizlonator 6 hours ago
It was materially useful in this project.
- Caught multiple memory safety issues in a nice deterministic way, so designing the object model was easier than it would have been otherwise.
- C++ with accurate GC is a really great programming model. I feel like it speeds me up by 1.5x relative to normal C++, and maybe like 1.2x relative to other GC’d languages (because C++’s APIs are so rich and the lambdas/templates and class system is so mature).
But I’m biased in multiple ways
- I made Fil-C++
- I’ve been programming in C++ for like 35ish years now
Comment by vlovich123 4 hours ago
Comment by pizlonator 4 hours ago
> happen to know C++ really well
That’s my bias yeah. But C++ is good for more than just perf. If you need access to low level APIs, or libraries that happen to be exposed as C/C++ API, or you need good support for dynamic linking and separate compilation - then C++ (or C) are a great choice
Comment by vlovich123 3 hours ago
The syntax and ownership rules can take some getting used to but after doing it I start to wonder how I ever enjoyed the masochism of the rule of 5 magic incantation that no one else ever followed and writing the class definition twice. + the language gaining complexity constantly without ever paying back tech debt or solving real problems.
Comment by valorzard 2 hours ago
Comment by Futurmix 7 hours ago