94 points · by ingve · 9y ago · 43 comments
I know that Truffle is based on partial evaluation; how close are you to Futamura #2? That'd relieve you of the burden of having to care about what an "interpreter stack frame" even looks like. OTOH I can confirm that the same kind of speculation in optimization has to occur during self-application.
It really is a thorny problem, isn't it?
Is it? Objective-C is a dynamic language (AOT-compiled) that doesn't have these features, and it is possible to write very high performance code with it.
Objective C I believe does a globally cached method lookup for every message send (!) and so can't inline through message sends (!), and since inlining is the mother of all optimisations I would imagine this would severely limit performance if you tried to use a lot of message sends in your inner loops. We should actually think about doing that experiment to see what the cost would be.
In a language like Ruby almost all operators, even basic arithmetic, are dynamic method calls, so you can't avoid using message sends anywhere. I think if you tried to do that in Objective C things might grind to a halt.
Objective C also lacks many dynamic language features which are the ones solved through speculative optimisation, such as integer overflow, access to frames as objects, and so on.
I'm not an expert on Object C though so happy to be corrected.
I do :-) Wrote about a book about it, in fact.
> but I think it's generally possible to write very high performance code in Objective C... by not using the dynamic language features of it such as message sends.
Yes, that's a common misconception...with a grain of truth. In my experience, you get the best performance by judiciously mixing dynamic and static features. And yes, that means eschewing some dynamic features in some inner loops (The 97:3 rule applies). However, you can also often gain significant performance by hiding behind a polymorphic dispatch.
For example, I reimplemented Apple's binary plist parsers+generators in Objective-C (from C) for a significant speed boost: the polymorphic implementation allowed me to put in override points for things such as lazy loading, and interface-based (de-)serialization removes the need for a generic intermediate representation. Compared to those advantages, the cost of message-sends is negligible (and optimizable if it becomes a problem).
> Objective C I believe does a globally cached method lookup for every message send(!)
Yes. It's quite fast and despite what people fret about rarely a problem.
> and so can't inline through message sends (!),
If it does become a problem (measure, measure, measure!), there are techniques to avoid the lookup: IMP-cache, convert to C function call, convert to inline function, convert to Macro.
> since inlining is the mother of all optimisations
Hmm...the mother of all optimizations is measuring and removing unnecessary code. Then comes eliminating/reducing and "sequentializing" memory access.
Very few of these can be automated.
Inlining is nice, too.
Having different syntaxes on top of the same VM is nice and all the JVM languages show that there is room for a good variety and for different paradigms.
On the other hand, the choice of a VM sets some constraints while providing important features. Think for instance at the differences between the languages running on the JVM and the languages running on the BEAM (Erlang's VM).
I think there is a lot of untapped potential for innovative ideas at the VM-level but for some reason most of the effort goes into proposing new syntaxes that reuse the same concepts all the other languages are using.
With some exceptions though: Java VM does not support tail call optimization, which limits how you implement loop constructs.
I intend to take a more experimental direction with Zeta. It won't have an FFI, for instance. It will only provide a small set of minimalist APIs. The VM will be intentionally designed to avoid code rot and breaking changes.
Another thing I would like to experiment with is transparent compilation of pixel shaders to run on GPUs, the ability to run code in any language running on Zeta (given some restrictions) on both the CPU and GPU. I believe I have found a way to make this work, based on my type-specialization research.
Edit 16 minutes later: I just tried your benchmark with my VM-like language (https://github.com/akkartik/mu), and the time taken was almost identical. Interesting exercise! Here's the Mu and Plush/0 programs side by side:
$ cat fib.mu
def fib n:num -> result:num [
local-scope
load-ingredients
base-case?:bool <- lesser-or-equal n, 1
return-if base-case?, n
n <- subtract n, 1
fib-n-1:num <- fib n
n <- subtract n, 1
fib-n-2:num <- fib n
result <- add fib-n-1, fib-n-2
]
def main [
local-scope
x:num <- fib 29
$print x, 10/newline
]
$ cat benchmarks/fib29.pls
#language "lang/plush/0"
var fib = function (n)
{
if (n < 2)
return n;
return fib(n-1) + fib(n-2);
};
var r = fib(29);
print(r);JavaScript has massive bitrot issues. The HTML DOM is huge and constantly changing. I have had my own web apps break multiple times over the years. As for Python and C, if you stick to the core language, and minimize dependencies, you might be Ok. The problem is that the more dependencies you have, if any one of them break, they can render your program broken... And if you're not there to fix it, your program remains broken forever.
My argument for reducing API surface and keeping APIs low-level and minimalist is that the smaller an API, the more difficult it is to implement it wrong. It's easier for two implementations to implement a smaller API and have the same behavior. It's also easier to test small APIs for conformance, etc.
How do you plan to do that?
The outside world will be accessible through a set of APIs that are small, have limited surface area. For instance, sound output will be done by writing raw samples. Graphics will be done with pixel shaders (functions which given coordinates return a pixel color). New APIs will be added over time, new versions of APIs will be added, but the older APIs will remain functional.
There will eventually be a package manager, and it will have immutable package versioning. That is, you can publish v5 of libtern, and I can depend on it. You can later publish v6 of libtern, but you can't change v5, so you can't break my code which depends on v5 when you publish new versions of your lib.
Obvious caveat: Zeta will have no FFI. You won't be able to immediately use all the libraries you could use in another system. I still think that building a huge amount of code that doesn't break could turn out to be a huge advantage. We won't try to satisfy every use case out there... But I think there are a lot of interesting and useful programs which don't need much more than basic keyboard and mouse input, audio and video output, network and file access (think photoshop, audio editing software, IDEs, etc). We will probably end up adding APIs for other things like MIDI, etc later on.
EDIT: removed it. It was probably left over from old code, previous VM's I've written: https://github.com/maximecb/zetavm/commit/7ef746f06113530468...