简体   繁体   English

为asm.js编写优化的JS

[英]Writing optimised JS for asm.js

There is quite a bit of excitement on asm.js and how it will be able to run some very heavy applications. 对于asm.js以及它如何能够运行一些非常繁重的应用程序有一点兴奋。 However, it is compiled from C++ code. 但是,它是从C ++代码编译的。 Is it still possible to get the benefit of current improvements without knowing C++ or other low level languages? 如果不了解C ++或其他低级语言,是否仍然可以获得当前改进的好处?

Here is the thought that I had: Is it at all possible that we can write the code in Js, have it recompiled for asm.js for optimisation? 以下是我的想法:我们是否可以在Js中编写代码,为asm.js重新编译以进行优化?

If you have small function that is very computation-heavy (crunching numbers rather than manipulating DOM) you could rewrite it in asm.js style yourself, manually. 如果你有一个非常耗费计算量的小函数(处理数字而不是操作DOM),你可以自己手动重写asm.js样式。 It's possible ( I've done it ), but tedious. 这是可能的( 我已经完成了 ),但很乏味。

There are other asm.js compilers, eg LLJS that you may use instead of C++. 还有其他asm.js编译器,例如LLJS ,您可以使用它而不是C ++。

However, asm.js is not magic. 但是,asm.js并不神奇。 You will only get performance benefit when you use language that is much better suited for ahead-of-time optimization than JS. 当您使用比JS更适合于提前优化的语言时,您将获得性能优势。 You can't take fully-featured JS and make it faster by running JS VM on top of JS VM, just like you can't make ZIP files smaller by zipping them. 你不能使用功能齐全的JS并通过在JS VM上运行JS VM来加快速度,就像你不能通过压缩它们来制作更小的ZIP文件一样。

However, it is compiled from C++ code. 但是,它是从C ++代码编译的。

It is not. 它不是。 It's a language. 这是一种语言。 Any program can emit text files which contain asm.js code. 任何程序都可以发出包含asm.js代码的文本文件。 Emscripten compiles LLVM IR into asm.js, and there are compilers from C and C++ to LLVM IR, but this is only one possible way of getting asm.js code. Emscripten将LLVM IR编译为asm.js,并且有从C和C ++到LLVM IR的编译器,但这只是获取asm.js代码的一种可能方式。 Admittedly, it's currently the most mature, practical and popular way, but I would not be surprised at all if other asm.js compilers for other languages pop up some time in the future. 不可否认,它目前是最成熟,最实用和最受欢迎的方式,但如果其他语言的其他asm.js编译器在未来的某个时间出现,我也不会感到惊讶。

Is it still possible to get the benefit of current improvements without knowing C++ or other low level languages? 如果不了解C ++或其他低级语言,是否仍然可以获得当前改进的好处?

Well, in theory any language that can efficiently be compiled to machine code ahead-of-time can be implemented efficiently using asm.js, and that includes some rather high-level ones (eg Haskell). 那么,从理论上讲,任何能够有效地编译成机器代码的语言都可以使用asm.js高效地实现,并且包括一些相当高级的语言(例如Haskell)。 But currently, nobody has a working implementation, and I don't expect this to ever become very popular. 但是目前,没有人有工作实施,我不希望这种情况变得非常流行。 Right now, if you want asm.js performance, you'd probably write C or C++ code and compile it to asm.js, yes. 现在,如果你想要asm.js性能,你可能会编写C或C ++代码并将其编译为asm.js,是的。

Note that the above excludes (among many others) Javascript.The fact that asm.js is a subset of Javascript is convenient in that asm.js code will run on unmodified browsers, but it's not of much use for anyone writing Javascript. 请注意,上面排除了(以及其他许多)Javascript。事实上asm.js是Javascript的一个子集很方便,因为asm.js代码将在未修改的浏览器上运行,但对于任何编写Javascript的人来说都没有多大用处。 asm.js is basically just a thin layer above machine code, with some amends for security and JS interoperability. asm.js基本上只是机器代码之上的一个薄层,对安全性和JS互操作性有一些修正。 Compiling JS to asm.js is as hard as compiling it to machine code: Easy if you don't give a damn about performance (just always used boxed dynamically-typed values like an interpreter, and emit calls to runtime library functions), very hard when you do. 将JS编译为asm.js与将其编译为机器代码一样困难:如果你不吝啬性能(只是总是使用盒装的动态类型值,如解释器,并发出对运行时库函数的调用),那就很容易了,非常你做的很难。

In fact, after decades of research into the subject, there's still no example of a highly dynamic language like Javascript, Ruby or Python being compiled into machine code ahead of time and running much faster than a clever interpreter. 事实上,经过几十年的研究,仍然没有像Javascript,Ruby或Python这样的高度动态语言提前编译成机器代码并且运行速度比聪明的解释器快得多的例子。 Just-in-time compilation, on the other hand, is very much practical -- but the major JS engines already do that, in a less roundabout way than compiling to asm.js, then parsing it again and compiling it to machine code. 另一方面,即时编译非常实用 - 但主要的JS引擎已经这样做了,比编译为asm.js更简单,然后再次解析并将其编译为机器代码。

Asm.js isn't a separate language, but a subset of Javascript. Asm.js不是一种单独的语言,而是Javascript的一个子集。 It's just Javascript with a lot stripped out for performance. 这只是Javascript,因为性能而被剥夺了很多。 This means that you don't need to learn another language, though in this case knowing C/C++ might be useful to understand it. 这意味着您不需要学习另一种语言,但在这种情况下,了解C / C ++可能对理解它很有用。

Asm.js is a very strict subset of JavaScript, that can be generated relatively easily when compiling from C/C++ to JavaScript. Asm.js是JavaScript的一个非常严格的子集,在从C / C ++编译到JavaScript时可以相对容易地生成。 Asm.js code is much closer to machine code than ordinary JavaScript code, which allowed browsers to heavily optimise for any code written in asm.js. Asm.js代码比普通的JavaScript代码更接近机器代码,这使得浏览器可以对asm.js中编写的任何代码进行大量优化。 In browsers that implemented those optimizations, your code will typically run about 50% of the speed of a C/C++ program that is compiled to machine code... which may seem slow, but is a hell of a lot faster than any ordinary JavaScript! 在实现这些优化的浏览器中,您的代码通常会运行编译为机器代码的C / C ++程序速度的大约50%......这可能看起来很慢,但是比任何普通的JavaScript都要快得多!

However, precisely because it's optimized for machines rather than humans, asm.js is practically impossible to handcode by any human developer... even though it's just JavaScript. 然而,正是因为它针对机器而非人类进行了优化,asm.js几乎不可能被任何人类开发人员手动编码......即使它只是 JavaScript。 While it is technically possible to convert - at least a subset of - ordinary JavaScript to an asm.js equivalent, such a conversion is not an easy task and I haven't encountered any project yet that made any attempt to achieve this. 虽然技术上可以将 - 至少是普通JavaScript的一部分转换为asm.js等价物,但这样的转换并不是一件容易的事,我还没有遇到任何试图实现这一目标的项目。

Until someone achieves such a Herculean task, the best approach to producing asm.js code remains writing your code in C/C++ and converting it to JavaScript. 在有人完成这样一项艰巨任务之前,生成asm.js代码的最佳方法仍然是用C / C ++编写代码并将其转换为JavaScript。

For more info on asm.js, see eg. 有关asm.js的更多信息,请参阅eg。 John Resig's article from 2013 or the official specs . John Resig的文章来自2013年官方规格

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

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