Flutter web 应该使用 Wasm 而不是 dart2js

Should Flutter web use Wasm instead of dart2js

我发现了几篇关于 Wasm 比 JS 更快的文章 [1, 2]

我看到主题已经被触及 在这个旧的(已关闭)问题中 [3, 4]

以及在 google 组 [5];

上似乎已放弃的讨论

问题是:

为什么 flutter web 不使用 Wasm 而不是 dart2js ?

感谢您的宝贵时间

(也在 github 上询问)

Vyacheslav Egorov (leading Dart AOT & JIT at Google) explains in this comment on Github 这根本不清楚 WASM 是比 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.

在上次 Flutter 团队 AMA 期间,reddit 上的 Flutter 团队成员 Todd Volkert 说:

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

2021 年 7 月 27 日,Michael Thomsen (a Google product manager working on Dart and Flutter) published a blog post on the Dart blog titled, "Experimenting with Dart and Wasm”。(Dart 是使用 Flutter 开发应用程序时使用的主要编程语言。)

他们(Google)一直在做将 Dart 编译成 Wasm 的实验。

底线:现在还为时尚早,但这绝对是他们一直在探索的事情。