序言(一)
I'm very excited to see this book, which covers in great detail a wide range of topics regarding WebAssembly. At this point in time WebAssembly is around one year old-if we count from when it shipped in all major browsers-so it's still fairly young, and the industry is just starting to figure out how revolutionary it is going to be. The potential is there for huge impact, and good documentation is necessary for that.
Why is WebAssembly's potential impact so large? For several reasons:
WebAssembly helps make the Web fast: WebAssembly is designed for small download size, fast startup, and predictably fast execution. The improvement compared to JavaScript can be very significant, over 2x in many cases, and especially in startup, where the speedup can be 10x.
WebAssembly makes the Web competitive with native: WebAssembly is designed as a compiler target for multiple languages. That includes C and C++, and in many areas of software the best implementations are in those languages, for example, game engines like Unity and Unreal, design software like AutoCAD, etc. It would take many years to write comparable products in JavaScript; instead, by compiling them to WebAssembly, the Web can be on par with native platforms today.
WebAssembly also fills an industry need outside the Web: WebAssembly is fast, portable, sandboxed, has multiple excellent open source implementations, and just like the Web itself it is an industry standard expected to be supported for the long term. As a result, it's not surprising that WebAssembly is starting to be used outside of browsers, for example in the blockchain and content delivery network (CDN)spaces.
Looking back, it's remarkable that our industry has gotten to this point. Just a few years ago, there was no cross-browser collaboration on getting native code to run on the Web. Instead, there were multiple options, including Native Client, Adobe Alchemy, and ASM.js, each with its own advantages and disadvantages. I believe it was the momentum of ASM.js that got the industry to focus on fixing things: ASM.js started out in Firefox, and by virtue of being a subset of JavaScript it immediately ran in all browsers-just not as efficiently. That led top companies in the video game industry and elsewhere to adopt ASM.js, together with Emscripten, the open source compiler to JavaScript that I started in 2010, and which could emit ASM.js. That adoption led to ASM.js support in Edge and later Chrome, at which point there was consensus that the industry should produce a proper standard in this space, which turned into WebAssembly. As the spec was designed and implementations started to appear, we added WebAssembly support to Emscripten, which allowed people to compile to both ASM.js and WebAssembly by just flipping a switch, making it easy for people to use the new technology. Finally, as of May 2018 Emscripten emits WebAssembly by default, and today WebAssembly has robust and stable support both in all major browsers and in the toolchain projects that emit it.
It's been a complicated path to get here, but the future looks bright. It is especially worth noting that WebAssembly is expected to add features like multithreading, SIMD, GC, and others, which will open up even more interesting opportunities.
Alon Zakai
Alon is a researcher at Mozilla, where he works on compile-to-Web technologies. Alon co-created WebAssembly and ASM.js, and created the Emscripten and Binaryen open source projects which are part of the primary WebAssembly compiler toolchain.
译文:
我很高兴能够看到这本书的出版,作者在书中详细地介绍了有关WebAssembly的各种主题。在这本书即将出版之际,WebAssembly差不多一岁了——如果从所有主流的Web浏览器开始支持WebAssembly算起,那么这项技术仍然相当年轻,业界也才刚刚开始意识到它将多么具有革命性。WebAssembly所拥有的潜力将会在未来对IT行业产生巨大的影响,但在此之前,我们需要有优秀的文档。
为什么WebAssembly的潜在影响力会如此之大?有以下几个原因。
WebAssembly让Web应用运行更快。WebAssembly是一种新的格式,文件体积更小,启动速度更快,运行速度也更快。与使用JavaScript构建的Web应用相比,性能提升非常明显。在大部分情况下,运行速度提升两倍以上,特别是在启动速度方面,速度提升可以达到10倍。
WebAssembly让Web应用能够与原生应用展开竞争。WebAssembly是多种编程语言的编译器目标,包括C和C++。基于这些编程语言实现的优秀软件,如游戏引擎Unity、Unreal,设计软件AutoCAD等,如果使用JavaScript开发在功能上与这些软件旗鼓相当的产品可能需要很多年时间。但如果将它们编译成WebAssembly,这些原生应用就可以直接运行在Web平台上。因此,Web能够与原生平台相提并论。
WebAssembly还在Web领域之外为行业带来了其他可能性。WebAssembly运行速度快、可移植,提供了沙箱机制,并拥有众多优秀的开源实现,就像 Web 本身一样,它将会是一个被长期支持的行业标准。因此,WebAssembly开始被应用在Web浏览器之外的领域也就不足为奇了,例如区块链和内容分发网络(CDN)。
回首过去,我们的行业能够取得如此的成就已经很了不起了。几年前,还没有人去进行这种跨浏览器协作,以便让原生代码运行在Web平台上。不过有很多不同的项目,如Native Client、Adobe Alchemy和ASM.js,它们都在尝试做同样的事情,只是每个项目都有各自的优缺点。而我认为,是ASM.js的出现让业界开始专注于解决这个问题——ASM.js最初出现在Firefox中,由于它是 JavaScript 的一个子集,因此可以无缝地运行在所有浏览器中,但运行效率不高。视频游戏等行业的一些顶级的公司开始尝试使用ASM.js和Emscripten(我在2010年开源的编译器工具链,可以将代码编译成ASM.js)。由于在这些领域的广泛应用,Edge以及后来的Chrome均开始支持ASM.js。此时,人们一致认为这个领域需要一个行业标准,于是WebAssembly出现了。
随着规范设计和实现的不断演进,我们在Emscripten中加入了WebAssembly支持——只需要在编译命令中加入一个“开关”,便可选择性地将编译目标设置为ASM.js或WebAssembly,从而可以更轻松地使用这项新技术。截至2018年5月,Emscripten已经将默认的编译目标类型改为WebAssembly。今天,WebAssembly已经在所有主流浏览器和工具链项目中得到了强大而稳定的支持。
一路走来历经坎坷,但未来是光明的。特别值得注意的是,WebAssembly将会在未来添加多线程、SIMD、GC等功能,而这些新特性将会为我们带来更多有趣的可能性。
Alon Zakai
Alon是Mozilla的研究员,从事与“编译到Web平台”相关的研究工作。Alon参与制定了WebAssembly和ASM.js 标准,并创建了 Emscripten和Binaryen等开源项目,这些项目都是WebAssembly编译器工具链的重要组成部分。