r/FlutterDev • • Jun 17 '20

Article Flutter and Desktop Apps

https://medium.com/flutter/flutter-and-desktop-3a0dd0f8353e
125 Upvotes

49 comments sorted by

View all comments

Show parent comments

1

u/mraleph Jun 19 '20

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:

class Body {
  double x;
  double y;
  double z;
  double vx;
  double vy;
  double vz;
  double mass;

  Body({this.x, this.y, this.z, this.vx, this.vy, this.vz, this.mass});
}

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 19 '20

Thank you for looking at this, it's much appreciated. The closer AOT can get to JIT, the more viable Dart becomes for a wider range of application classes.

the original code omits types in many places (Dart 1 style)

I think letting Dart infer types by itself is pretty common in Dart 2 style as well, isn't it?

If letting Dart infer types is going to result in known performance problems at compile time, could there be value in having linter tips to alert folks to this? I suspect most people probably don't appreciate the impact on the final result.

That said, is this benchmark really representative of the workload your application has to deal with?

The actual workload is to do with linear constraint programming, not n-body problems, but, yes, it's computation heavy. The app has to work off-line which is why it's not as simple as just off-loading computation to a remote server.

1

u/mraleph Jun 19 '20

I think letting Dart infer types by itself is pretty common in Dart 2 style as well, isn't it?

It is common to omit types locally (on local variables and local functions), because these types are subject to the type inference built into the language. However here the type that was omitted is a type of a field without initializer. Fields types are only subject to type inference if there is an initialiser to infer the type from. So the field ends up being typed as dynamic.

could there be value in having linter tips to alert folks to this?

There is actaully an analyzer switch which would warn you about untyped variables: --no-implicit-dynamic. It can also be switched on in the analysis options by adding implicit-dynamic: false see here.

We have discussed on and off doing some static performance linting tooling - but no concrete plans so far.

1

u/coldoil Jun 21 '20

Many thanks, I've enabled that linter flag (and a few others).

1

u/mraleph Jun 21 '20

Glad to help! In future if you have any perf related questions don’t hesitate to reach out to me directly, either here or on Twitter, GitHub (same username on all of them) or at [email protected]