简体   繁体   English

将 Assemblyscript 编译为 Wasm,性能

[英]Compiling Assemblyscript to Wasm, performance

I saw that there already is a compiler for compiling TypeScript to WebAssembly (Wasm), here is the link .我看到已经有一个编译器可以将 TypeScript 编译为 WebAssembly (Wasm),这里是链接

I also heard from multiple sources that compiling JS to Wasm wouldn't be feasible, because of JavaScript's dynamic nature and dynamic types.我还从多个来源听说将 JS 编译为 Wasm 是不可行的,因为 JavaScript 的动态特性和动态类型。

However, TypeScript does offer typed variables which JavaScript lacks.然而,TypeScript 确实提供了 JavaScript 所缺乏的类型化变量。 And in the future Wasm might be able to even interact with the DOM/interact with other web API.并且在未来,Wasm 甚至可能能够与 DOM 交互/与其他 Web API 交互。

Question:题:

Would writing applications in TypeScript and compiling it to Wasm offer any performance benefits when compared to writing a web application in JavaScript?与使用 JavaScript 编写 Web 应用程序相比,使用 TypeScript 编写应用程序并将其编译为 Wasm 会提供任何性能优势吗?

The realistic answer is: no.现实的答案是:不。 There are a few common misunderstandings about TypeScript.关于 TypeScript 有一些常见的误解。 One is that it is less dynamic than JavaScript.一是它的动态性不如 JavaScript。 That is not true, it is in fact every bit as dynamic as JS, because it encompasses all of JavaScript's semantics (including all the crazy corner cases) and its type system is far too weak and unsound to provide guarantees that an ordinary offline compiler could use for static optimisations.这不是真的,它实际上和 JS 一样动态,因为它包含了 JavaScript 的所有语义(包括所有疯狂的极端情况),并且它的类型系统太弱和不健全,无法保证普通的离线编译器可以用于静态优化。 At best, the types can be used as hints that a dynamic VM could try to optimise for first, well knowing that they might turn out to be incorrect.充其量,这些类型可以用作动态 VM 可以首先尝试优化的提示,很清楚它们可能会被证明是不正确的。

(Also, I'm not aware of a TypeScript-to-Wasm compiler. You're probably thinking of AssemblyScript, but while that reuses TypeScript's syntax, its semantics is very different.) (另外,我不知道 TypeScript-to-Wasm 编译器。您可能会想到 AssemblyScript,但虽然它重用了 TypeScript 的语法,但其语义却大不相同。)

If you can compile a certain Javascript functionality down to Wasm it probably runs faster.如果您可以将某个 Javascript 功能编译为 Wasm,它可能会运行得更快。 JavaScript runs very fast compared to other dynamic languages, as (most) browsers compile it down to aa low level bytecode that gets interpreted very fast (or even runs natively).与其他动态语言相比,JavaScript 的运行速度非常快,因为(大多数)浏览器将其编译为低级字节码,该字节码可以非常快地被解释(甚至本机运行)。 Wasm is designed to be very close to bytecode so in the best case JS and "a subset of JS with types compiled to Wasm" run at the same speed (if both compilers do a good job). Wasm 被设计为非常接近字节码,因此在最好的情况下,JS 和“具有编译为 Wasm 类型的 JS 子集”以相同的速度运行(如果两个编译器都做得很好)。 But:但:

1) JS sourcecode is probably bigger than the Wasm code, so it takes longer to load it from the server (startup time) 1)JS源代码可能比Wasm代码大,所以从服务器加载它需要更长的时间(启动时间)

2) JavaScript has to be optimized to run that fast, and as it doesnt know the types / lifetime of variables and properties it takes some time until the engine found the best way to compile it, this optimization process takes time. 2) JavaScript 必须优化才能运行得那么快,而且由于它不知道变量和属性的类型/生命周期,引擎找到编译它的最佳方式需要一些时间,这个优化过程需要时间。 (warmup time). (热身时间)。

That said you cannot just turn most JavaScript or TypeScript into Wasm (thats why AssemblyScript is a subset of TS) for various reasons:也就是说,出于各种原因,您不能将大多数 JavaScript 或 TypeScript 转换为 Wasm(这就是为什么 AssemblyScript 是 TS 的子集):

1) eval , with or just for(const el of [1, "seven", null]) are performance killers in JS, but they are impossible to compile to Wasm (because they are performance killers). 1) eval , with or just for(const el of [1, "seven", null])是JS中的性能杀手,但它们不可能编译成Wasm(因为它们是性能杀手)。

2) JavaScript has garbage collection. 2) JavaScript 具有垃圾收集功能。

You referenced AssemblyScript in your question.您在问题中引用了 AssemblyScript。 Assembly script is an extremely strict subset of typescript.汇编脚本是打字稿的一个极其严格的子集。 Don't confuse it with typescript it's self.不要将它与它自己的打字稿混淆。 The big difference being that Typescript encompasses all of the dynamic attributes that we all know and love (hate) in Javascript.最大的不同在于 Typescript 包含了我们都知道和喜欢(讨厌)Javascript 中的所有动态属性。 AssemblyScript on the other hand does not.另一方面,AssemblyScript 没有。

Examples of a couple of the big differences are;一些重大差异的例子是: in AssemblyScript you can not have closures.在 AssemblyScript 中,你不能有闭包。 You also can not have union types.你也不能有联合类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM