WebAssembly 为什么比unity asm.jss 快

中国领先的IT技术网站
51CTO旗下网站
WebAssembly 为什么比 asm.js 快?
WebAssembly 是为 Web 而设计的、可以生成浏览器可执行的二进制文件的编程语言。WebAssembly 的一个主要目标就是变快。本文将给出一些它如何变快的技术细节。
作者:胡子大哈| 14:37
WebAssembly 是为 Web 而设计的、可以生成浏览器可执行的二进制文件的编程语言。并且于2017 年 2 月 28
日,四个主要的浏览器 WebAssembly 的 MVP 版本已经完成,即将推出一个浏览器可以搭载的稳定版本。WebAssembly
的一个主要目标就是变快。本文将给出一些它如何变快的技术细节。
当然,&快&是相对的概念。相比于 JavaScript 和其他动态语言,WebAssembly
的快主要是因为它的。WebAssembly 意在速度上能够达到和本地执行一样快,其实 asm.js 已经比较接近这一目标了,但是
WebAssembly 要进一步缩短和本地执行速度之间的差距。因此本文着重介绍为什么 WebAssembly 比 asm.js 更快。
在开始介绍之前,先做一些说明:新的技术总是有一些还没来得及优化的情况,所以目前来说,并不是所有情况下 WebAssembly 都是最快的。本文主要表达的是
WebAssembly 为什么应该是更快的。对于它还不是那么快的一些情况,也是未来需要 fix 的问题。
那么有了这些标准以后,我们来介绍为什么 WebAssembly 更快。
WebAssembly 设计之初就定位在:体积更小、下载更快、解析更快,这样即使应用于一个大型的 web 应用上启动也会很快。
JavaScript 代码通过 gzip 进行压缩,与本地代码相比它已经压缩了很多,想要进一步缩小它的体积确实不是一件容易的事情。但是通过对
WebAssembly 文件大小的精心设计(LEB128 指标),二进制格式的 WebAssembly 的文件大小要比压缩后的 JavaScript
文件更小。通常来讲,要比 gzip 压缩后的小 10% - 20%。
WebAssembly 在解析过程有更大的进步:它的解析速度要比 JavaScript 快一个数量级。这得益于 WebAssembly
的二进制格式就是为更适合解析而设计的。同时 WebAssembly 的解析和优化函数也更容易实现并行,这一特点在多核机器上的体现更好。
影响启动时间的因素除了下载和解析以外还有很多,比如代码的 VM
完全优化、执行之前下载所必须的额外文件等。但是下载和解析步骤是无论如何都不可避免的步骤,因此要尽可能地对它们进行优化。不论是对于浏览器还是
app,其他的影响因素都有办法避免或者缓和它们的影响(例如代码的完全优化,可以通过 WebAssembly 的来避免)。
2. CPU 特性
使得 asm.js 这么快的技巧之一是利用好 CPU 特性。JavaScript 数字类型都是 double 型的,在 asm.js
中加法操作后会有一个按位与的操作,这使得 double 加法这个操作,在逻辑上和 CPU 做简单的 int 加法(CPU 做简单 int
加操作很快)是等价的。而 asm.js 巧妙地通过这种方式使得 VM 可以更加有效地利用 CPU。
但是有一些 JavaScript 中表达的东西,asm.js 是表达不了的。
WebAssembly 则不受 JavaScript 的约束,我们一起来看一下更多的可利用的 CPU 特性:
64 位整型。基于 64 位整型的操作速度可以快 4 倍。例如可以增加哈希算法和加密算法的速度。
内存读写偏移量。它的用处特别广泛,基本上所有包含字段的内存对象都涉及到偏移量的问题(如 C 语言中 struct 等)。
非对齐内存读写。这避免了 asm.js 所需要的 mask(asm.js 为 Typed Array
兼容而做的操作),同时这在几乎所有的内存读写问题都用得上。
多种 CPU 指令,例如 popcount,copysign 等。每一个指令都有助于某种具体使用场景(例如,popcount
在密码分析这个场景下很有用)。
在每一种具体场景下到底能起到多大作用,要依赖于如何使用上面提到的特性。和正常使用 asm.js 相比,我们的统计是大概比 asm.js 提升了 5%
的速率。未来还有更多可挖掘的 CPU 特性来提高速率,例如。
3. 工具链改进
WebAssembly
是编译器生成的主要目标,所以它的运行主要包含两个部分:生成它的编译器(工具链端)和运行它的虚拟机(浏览器端)。优良的性能全都依赖于这两部分。
对于 asm.js,情况也类似,并且 Emscripten 做了一系列对工具链的优化,还做了运行 LLVM 的优化器和 Emscripten 的
asm.js 优化器。对 WebAssembly 的优化都是在这些的基础上来设计的,并且同时还加入了一些专门针对 WebAssembly 的改进。我们自己在学习
asm.js 的过程对我们改善 WebAssembly 很有帮助,尤其体现在一下几个方面:
我们使用 Binaryen WebAssembly 优化器来代替 Emscripten asm.js
优化器,前者是专门为速度而设计,其速度的提升是以更多优化步骤为代价的。例如在优化的过程中会移除重复函数,这样会使 C++ 编译代码整体减小 5% 左右。
对冗余、复杂的控制流进行更好的优化,这可以提升 Relooper 算法的效率,对于编译解释类型循环也很有帮助。
Binaryen 优化器是在试验中不断设计和完善的,通过超级优化器做的实验也会带来各种情况的微妙改进&&我想 asm.js 也做过同样的事情。
总的来说,这些工具链的改进,在 asm.js 的基础上进一步提升了 WebAssembly 的速度(Box2D 速度评测中,WebAssembly
的速度分别提升了 5% 和 7%)。
4. 可预见的优良性能
asm.js 的速度很接近本地执行的速度了,但是它不可能在所有的浏览器中都达到同样的标准。因为在使用 asm.js 的过程中,一些人尝试用一种方法来优化
asm.js,而另一些人用另一种方法优化,这导致了不同人有不同的版本和结果。虽然随着时间的推移,大家对于 asm.js 也达成了某种共识,但是对于 asm.js
来讲,本质问题是它还是没有一个统一的标准。它只是一个由一个厂商推出的,非标准的 JavaScript
子集而已,而它的使用者根据自己的偏好和习惯来使用它。
WebAssembly 则不同,它是由几大主要的浏览器厂商共同设计的。与 JavaScript相比,JavaScript 只能通过一些创新方法或者
asm.js 这种方法来提高速度,而不论哪种方法都不可能适用于所有浏览器。WebAssembly 的优化方案则得到了大多数厂商的认可。对于
WebAssembly 来讲,对于不同的 VM 依旧有很大的提升空间(如 AOT、JIT
有不同编译方式)。不过能够基本断定的是,可以应用于整个网络的优良性能是指日可期的。
点击阅读原文。
【本文是51CTO专栏作者&胡子大哈&的原创文章,转载请联系作者本人获取授权】
【编辑推荐】
【责任编辑: TEL:(010)】
大家都在看猜你喜欢
本周排行本月排行
讲师:207519人学习过
讲师:119828人学习过
讲师:131309人学习过主题信息(必填)
主题描述(最多限制在50个字符)
申请人信息(必填)
申请信息已提交审核,请注意查收邮件,我们会尽快给您反馈。
如有疑问,请联系
CSDN &《程序员》编辑/记者,投稿&纠错等事宜请致邮
你只管努力,剩下的交给时光!
如今的编程是一场程序员和上帝的竞赛,程序员要开发出更大更好、傻瓜都会用到软件。而上帝在努力创造出更大更傻的傻瓜。目前为止,上帝是赢的。个人网站:。个人QQ群:、
个人大数据技术博客:
WebAssembly 是为 Web 而设计的、可以生成浏览器可执行的二进制文件的编程语言。并且于2017 年 2 月 28 日,四个主要的浏览器一致同意宣布 WebAssembly 的 MVP 版本已经完成,即将推出一个浏览器可以搭载的稳定版本。WebAssembly 的一个主要目标就是变快。本文将给出一些它如何变快的技术细节。阅读全文请点击:当前位置 & &
& Google/微软/苹果联手:浏览器提速20倍!
Google/微软/苹果联手:浏览器提速20倍!
23:11:59&&出处:&&
编辑:上方文Q &&)
让小伙伴们也看看:
阅读更多:
好文共享:
文章观点支持
当前平均分:0(0 次打分)
[06-18][06-18][06-18][06-18][06-18][06-18][06-18][06-18][06-17][06-17]
登录驱动之家
没有帐号?
用合作网站帐户直接登录WebAssembly Takes A Big Step Towards Being Real
New Articles!
New Book Reviews!
Popular Articles
Written by Ian Elliot
WebAssembly will be the biggest change in web development since JavaScript was introduced and now we have proof positive that it is moving towards becoming something real. Microsoft, Google and Mozilla have all released compatible previews of WebAssembly and a game you can actually run.
WebAssembly was announced last year as a great revolution in the way code could be run in a browser. At the time it all seemed very vague and a long way from a practical proposition but slowly things are coming together. In theory Google, Mozilla, Microsoft and Apple are working on defining and implementing WebAssembly withing the W3C WebAssembly Community but today's announcements are notable for the absence of Apple and Webkit.
WebAssembly is now available in Chrome Canary and Firefox Nightly and in an internal build of Edge. You have to set a flag to enable WebAssembly in both Chrome and Firefox. After restarting, a new Wasm object is available in JavaScript that can instantiate and run WebAssembly modules.&
There is also a demo program showing how fast WebAssembly can run a 3D game. The game, Angry Bots, can be run in WebAssembly or using asm.js. &
WebAssembly is an Abstract Syntax Tree (AST) representation of what is essentially the asm.js subset of JavaScript. This is much easier to optimize and hence WebAssembly can run fast using a lot of the infrastruture already available within the current JavaScript engines. Essentially the ASTs are precompiled representations of asm.js which allows the engines to get on with executing the code rather than parsing the source. &According to the V8 engine blog:
"A specialized WebAssembly decoder validates modules by checking types, local variable indices, function references, return values, and control flow structure in a single pass. The decoder produces a TurboFan graph which is processed by various optimization passes and finally turned into machine code by the same backend which generates machine code for optimized JavaScript and asm.js."
You can see how the Mozilla team describes the difference between processng asm.js and WebAssembly:
The Mozilla blog suggests that the demo game compiles in about half the time it takes for asm.js.&
At the moment the ASTs are transferred in human readable form, but the next stage is to convert them to a compact binary format. This will make them smaller and hence load faster.&
On the game's GitHub page is also a status report:
WebAssembly Demo Status
Early multi-browser support
Runs in experimental builds of Chromium, Firefox, and Edge.
Demo execution
Full execution semantics implemented.
Stable binary format
Binary format will be updated to match design iterations, until it is frozen for stable release.
Standard textual encoding
Textual encoding will be standardized before stable release.
Stable tooling
A mature toolchain for compiling and debugging WebAssembly will be available before stable release.
Stable JS API
The Wasm object will be updated to allow additional behavior and introspection before stable release
You can see that there is still some way to go.&
What is also missing are the toolchains needed to create WebAssembly. These are under development but as the V8 blog says:
We’re also planning future WebAssembly features (including multi-threading,&dynamic linking, and GC / first-class DOM integration) and continuing the development of toolchains for compiling C, C++, and other languages via the&WebAssembly LLVM backend&and&Emscripten.
At the momement none of this is usable for production apps, but it is proof that WebAssembly is coming. There are plans to change the details of most of what is on offer at the moment. The Firefox blog suggests that the as well as the basics they plan to augment the browser devtools to include a WebAssembly debugger and profiler.&
There is some speculation about whether or not WebAssembly could be used to compile JavaScript programs. While anything in the software world is possible if you put enough effort into it this seems unlikely. The reason is that WebAssembly can be regarded as a precompiled version of asm.js which is a strict subset of JavaScript. To represent a complete JavaScript program in WebAssembly you would have to map all of JavaScript into asm.js. This might be possible but it isn't easy and it is difficult to know how fast the result would be.&
More Information
Related Articles
To be informed about new articles on I&Programmer,&sign up for our&,subscribe to the&&and&follow us on,&&,&&or&.&
In a new initiative to bridge the digital skills gap in the EMEA region Google is offering 60,000 Scholarships for both absolute beginners and existing programmers for&Udacity&Android and We&[&...&]
There's a new preview version of FaunaDB Developer Edition that provides a free to use, single-node, plug-and-play version of the database. FaunaDB has been available as a managed, serverless, cloud d&[&...&]
Please enable JavaScript to view the
RSS feed of news items only
Copyright © 2017 . All Rights Reserved.
is Free Software released under the}

我要回帖

更多关于 asm.js如何使用 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信