Depends on the benchmark - somewhere JIT and AOT are on par, somewhere JIT is much faster than AOT, somewhere AOT is somewhat faster than JIT.
What kind of benchmark did you write? If it was some number crunching then AOT most likely would be slower - though without looking at benchmark it is impossible to say why.
Most common problems are:
lack of type information (i.e. AOT is unable to figure out which concrete type is flowing through some important variables);
poor inlining decisions (i.e. AOT is very conservative with inlining to keep the code size under control - JIT is much more liberal).
boxing of primitive values (i.e. sometimes AOT compiler makes poor decisions around which values should be boxed - most commonly due to the first two issues)
JIT and AOT compiler are using largely the same compilation toolchain - it's just that JIT uses type feedback and speculative optimizations and AOT uses global type flow analysis (TFA) and non-speculative optimizations.
I took a brief look at this, the main issue AOT compiler stumbles upon here is for (var b in bodies) loop here. If that code is rewritten to use old-school loop like other loops in the same function that removes majority of the difference.
for(int nb = 0; nb < bodies.length; nb++) {
final b = bodies[nb];
// ...
}
The reason why original loop causes problem is a bit convoluted - the original code omits types in many places (Dart 1 style), which means among other things that NBodySystem.bodies is dynamically typed. Our AOT compiler tries to compensate for lack of type information and actually manages to infer that bodies is always going to be a List<Body> - however this information does not survive the trip through for (var e in bodies) construct and e does not get any more precise type inferred than its static type (which is dynamic). I have filed an issue to try and figure out if we could do better.
One could ask: well, can I just change var bodies to List<Body> bodies, would that solve the issue? It does solve part of the performance problem, but not entirely - another issue with for(var e in smth) is that we don't do a good job inlining here.
Final bit of difference comes from the fact that JIT tracks the length of an array stored in Body._data field and uses this information to eliminate bounds checks. There is an issue open for AOT to address this - but we have not done it yet.
Actually for AOT you could just rewrite the Body class code like this:
and it would eliminate the indirection through _data array entirely. AOT compiler is capable of fully unboxing double fields so it does not need the Body._data = Float64Array(...) trick. The reason why AOT does it and JIT does not - is because it uses non-nullability information inferred by TFA to make field unboxing decisions, JIT only has access to speculative profiling information which is not good enough for this [speculative field unboxing is possible but complicates implementation too much]. Given Dart 2 type system we could non-speculatively unbox all fields of type double and use a value from NaN-space to represent null, but we just decided to wait for NNBD. But anyway, I digress.
So with a bit of profiling and couple lines of fixes you can bring performance of this benchmark to JIT numbers.
That said, is this benchmark really representative of the workload your application has to deal with?
1
u/coldoil Jun 18 '20
I was expecting it to be slower. I wasn't expecting it to run at a third of the speed.