Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Flutter web use Wasm instead of dart2js

Tags:

I spotted several articles about Wasm being faster then JS [1, 2]

I see the topic has been touched in this old (closed) issues [3, 4]

and this seemingly abandoned discussion on google groups [5];

the question is:

why flutter web doesn't use Wasm instead of dart2js ?

thank you for your time

(asked also on github)

like image 222
Francesco Iapicca Avatar asked Sep 22 '19 17:09

Francesco Iapicca


People also ask

Does flutter web use Wasm?

With that, you can now use WebAssembly in your Flutter Web applications and enhance their performance. All the code in this article is available on GitHub. I hope you enjoyed this tutorial!

Does Dart compile to Wasm?

Dart's support for compiling to Wasm is an early stage investigation and the compiler is incomplete, but we're experimenting to learn. We've always had an interest in Wasm as a compilation target for Dart, but its original form doesn't work well for languages with garbage collection.

Is flutter for web stable?

In addition, to replace JavaScript, Flutter uses Dart. And now, Flutter for the web has been released and is stable. This is wonderful news for web designers, who can now construct highly secure and appealing websites, in addition to being able to publish Flutter mobile apps to the web using the same codebase.


2 Answers

Vyacheslav Egorov (leading Dart AOT & JIT at Google) explains in this comment on Github that this is not clear at all that WASM is a better compilation target that JS.

It is without doubt that WASM is a better compilation target than JS for languages like C/C++ and Rust - languages where you manage your memory manually, where your calls are most often statically dispatched and primitive types are unboxed.

However if you start moving away from this kind of languages towards languages like Dart - which are full of dynamic behaviour, boxed primitive types and GC you will discover it becomes harder to claim with absolute certainty that WASM is a better compilation target than JS - because you would be forced to compile various runtime components (e.g. method dispatch implementation) into WASM - while in JS you get those things from underlying JS runtime and they are implemented natively and heavily optimised.

Another interesting thing to consider is that dart2js essentially benefits from two compilation steps - AOT compilation to JS and dynamic optimisation of this JS later by JS JIT. If AOT compiler fails to statically specialise some call site, there is still a chance that JS VM would manage to do that. You don't get such luxury with WASM.

There are a lot of other factors to consider (e.g. builtin libraries - do you want to implement your own array like structure in WASM with associated performance penalty, or do you just want to use heavily optimised native array?), etc, etc.

That said - I don't doubt that there are workloads and programs that would benefit from Dart targeting WASM. All I am saying is that expecting all Dart programs to magically get faster is incorrect.

like image 153
Xavier Avatar answered Oct 14 '22 22:10

Xavier


During last Flutter Team AMA on reddit Flutter Team member Todd Volkert said:

WebAssembly is something we’re currently evaluating to see what opportunities exist for integrating it with Flutter. Nothing specific to report at this time.

Source

like image 43
Dominik Roszkowski Avatar answered Oct 14 '22 23:10

Dominik Roszkowski