OdinMonkey and Asm.js arrive in Firefox Nightly
OdinMonkey, an Asm.js optimising module for Firefox's JavaScript engine, has appeared in the nightly builds of Firefox and is planned to ship with June's release of Firefox 22. Despite reports that this is a JavaScript optimiser, OdinMonkey will not speed up normal JavaScript code – only a particular subset of JavaScript and only when it is packaged in a particular way.
Asm.js is a subset of JavaScript which has been selected firstly to route around the things that tend to slow down Java, such as boxed floating point numbers, JIT and runtime checking, garbage collection, and the language's flexible handling of memory layout. All these things take time to manage. By flagging code as "asm" and then using only the Asm.js subset of JavaScript, code can be written which runs well on any browser but can run really quickly if an Asm.js optimiser is available. OdinMonkey is such an optimiser for Firefox.
The ideas behind Asm.js have been put together to create near native performance for JavaScript without abandoning JavaScript and portability between browsers. In contrast, Google's performance efforts are focused on Native Client, which has been designed to run pre-compiled native code for whatever processor the browser is running on in its own sandbox. Native Client has been implemented for x86 and more recently ARM; the company is also working on Portable Native Client, which will execute LLVM bitcode and is due for release some time this year.
Asm.js isn't designed to be manually written, though developers can code it by hand, instead it is designed to be a target for other compilers and other languages. It uses its own Ahead-of-Time (AOT) compiler to optimise and validate Asm.js code. The Asm.js code itself has limited access to JavaScript's standard library – mostly Math functions – and is statically typed. It supports 64-bit double precision numbers and 32-bit integers only and has a selection of Array types. Overall, it presents to code an abstraction of a safe virtual machine for running memory-unsafe languages like C or C++.
One example of software that can generate Asm.js conformant code is Emscripten, a well known LLVM to JavaScript compiler used by a number of projects that compile C or C++ code using llvm-gcc or clang compilers into LLVM bitcode and then compile that into JavaScript to run in a browser. Mozilla's Asm.js developers were inspired by Emscripten's code generation and Emscripten's creator Alon Zkai is part of the Asm.js team. The team took Emscripten and modified it to produce Asm.js code and then ran it to compare Firefox's SpiderMonkey, Chrome's V8, and Firefox with the OdinMonkey optimisations. The results were impressive – in the zlib benchmark, Chrome V8 was 5.95 times slowed than native, SpiderMonkey 5.15 times slower and OdinMonkey only 1.61 times. Other benchmarks show a similar pattern of getting much closer to native performance.
Source: Alon Zakai's "Big Web App? Compile It!"
Currently though, it is early days for Asm.js and OdinMonkey. Developers wanting to test them will need to be running Windows or Linux with x86/x64; Mac OS X and Firefox on ARM builds are "almost done" but not here yet. The spec and implementation will be enhanced over the coming months too and the flag controlling OdinMonkey's operations (javascript.options.experimental_asmjs
in about:config
), although currently enabled, will be disabled when this edition of Firefox goes into beta and should be reenabled on final release. Aspiring developers will also want to get Emscripten to compile their C/C++ code; in the future, the Asm.js developers hope to offer another language such as a derivative of LLJS. There are still many issues with Asm.js and OdinMonkey to be overcome though, from a lack of analogues for C++ backend optimisations to a high cost between moving between non-Asm.js code and Asm.js code.
See also:
(djwm)