Jump to content

asm.js

From Wikipedia, the free encyclopedia
asm.js
Designed byMozilla
First appeared21 March 2013;11 years ago(2013-03-21)[1]
OSPlatform independent
Websiteasmjs.org
Influenced by
JavaScript
Influenced
WebAssembly

asm.jsis asubsetofJavaScriptdesigned to allowcomputer softwarewritten in languages such asCto be run asweb applicationswhile maintaining performance characteristics considerably better than standardJavaScript,which is the typical language used for such applications.

asm.js consists of astrict subsetof JavaScript, to which code written instatically typedlanguages with manual memory management (such as C) is translated by asource-to-source compilersuch asEmscripten(based onLLVM).[2]Performance is improved by limiting language features to those amenable toahead-of-time optimizationand other performance improvements.

Mozilla Firefoxwas the first web browser to implement asm.js-specific optimizations, starting with version 22.[3]

asm.js is superseded byWebAssembly.

Design

[edit]

asm.js enables significant performance improvements forweb applications,but does not aim to improve the performance of hand-written JavaScript code, nor does it enable anything other than enhanced performance.

It is intended to have performance characteristics closer to that of native code than standard JavaScript by limiting language features to those amenable toahead-of-time optimizationand other performance improvements.[4]By using a subset of JavaScript, asm.js is largely supported by all majorweb browsers,[5]unlike alternative approaches such asGoogle Native Client.

Code generation

[edit]

asm.js is not typically written directly: instead, as an intermediate language, it is generated through the use of acompilerthat takes source code in a language such asC++and outputs asm.js.

For example, given the following C code:

intf(inti){
returni+1;
}

Emscripten would output the following JS code:

functionf(i){
i=i|0;
return(i+1)|0;
}

Note the addition of|0and the lack of type specifiers. In JavaScript, bitwise operators convert their operands to 32-bit signed integers and give integer results. This means that abitwise ORwith zero converts a value to an integer (a very simple "conceptual" presentation of bitwise operators may not deal with type conversion at all, but every programming language defines operators for its own convenience, as Javascript does here). By doing this for each parameter, this ensures that if the function is called from outside code, the value will be converted to the correct type. This is also used on the return value, in this case to ensure that the result of adding 1 to i will be an integer (as otherwise it could become too large), and to mark the return type of the function. These conversions are required by asm.js, so that an optimising compiler can produce highly efficient native code ahead-of-time. In such an optimising compiler, no conversions are performed when asm.js code calls other asm.js code, as the required type specifiers mean it is guaranteed that values will already have the correct type. Furthermore, rather than performing a floating-point addition and converting to an integer, it can simply do a native integer operation. Together, this leads to significant performance benefits.

Here is another example to calculate the length of a string:

size_tstrlen(char*ptr){
char*curr=ptr;
while(*curr!=0){
curr++;
}
return(curr-ptr);
}

This would result in the following asm.js code:

functionstrlen(ptr){
ptr=ptr|0;
varcurr=0;
curr=ptr;
while((MEM8[curr>>0]|0)!=0){
curr=(curr+1)|0;
}
return(curr-ptr)|0;
}

In the generated code, the variable MEM8 is actually a byte-by-byte "view" of a typed buffer, which serves as the "heap" of the asm.js code.

Performance

[edit]

Since asm.js runs in a browser, the performance heavily depends on both the browser and hardware. Preliminary benchmarks of C programs compiled to asm.js are usually within a factor of 2 slower than native compilation withClang.[6]

Much of this performance gain over normal JavaScript is due to 100%typeconsistency and virtually nogarbage collection(memory is manually managed in a large typed array). This simpler model with no dynamic behavior, no memory allocation or deallocation, just a narrow set of well-defined integer and floating point operations enables much greater performance and potential foroptimization.[citation needed]

Mozilla's benchmark from December 2013 showed significant improvements: "Firefox withfloat32optimizations can run all those benchmarks at around 1.5× slower than native, or better. "[7]Mozilla points out that the performance of natively compiled code is not a single measure but rather a range, with different native compilers (in this caseClangandGCC) delivering code of differing performance. "In fact, on some benchmarks, likeBox2D,FASTAand copy, asm.js is as close or closer to Clang than Clang is to GCC. In one case, asm.js even beats Clang by a slight amount on Box2D. "[7]

Implementations

[edit]

TheEmscriptenproject provides tools that can be used to compile C and C++ codebases (or any other languages that can be converted toLLVMIR) into asm.js.[2]

All browsers with support forECMAScript 6should be able to run asm.js code, as it is a subset of that specification. However, since features were added in that edition to enable full asm.js support (Math.fround()), older browsers lacking those features may encounter problems.

Some browser implementations are especially optimised for asm.js:

  • Mozilla Firefoxwas the first web browser to implement asm.js-specific optimizations, starting with Firefox 22.[3]OdinMonkey,Mozilla's asm.js ahead-of-time compiler used in Firefox, is a component ofIonMonkey,the JIT compiler ofSpiderMonkey.
  • Microsoft was implementing support for asm.js inChakra,the JavaScript engine used byMicrosoft Edge Legacy,performing validation to produce highly optimised JIT code.[8]
  • The optimizations ofGoogle Chrome'sV8 JavaScript enginein Chrome 28 made asm.js benchmarks more than twice as fast as prior versions of Chrome,[9]although Chrome's V8 does not use ahead-of-time compilation.

Adoption

[edit]

Almost all of the current applications based on asm.js are C/C++ applications compiled to asm.js usingEmscriptenor Mandreel. With that in mind, the kind of applications that are going to target asm.js in the near future are those that will benefit from the portability of running in a browser but which have a level of complexity for which a direct port to JavaScript would be infeasible.

So far, a number ofprogramming languages,application frameworks,programs,libraries,games,game enginesand other software have already beenported.[10]Some of them are given below.

Programming languages

[edit]

Application frameworks

[edit]
  • pepper.js: Ports of miscellaneousPNaClapps (earth, voronoi, bullet, etc.)[14]
  • Qt:ports of various Qt demos, plus KDE apps, such asKate[15]

Programs and libraries

[edit]

Game engines

[edit]

Games

[edit]

Emulators

[edit]
  • EM-DOSBox: an Emscripten port ofDOSBox[35]
  • Start9.io:a web emulation platform targeting multiple gaming architectures
  • JSMESS: a port of theMESSemulator for many game consoles and computer systems[36]

Mathematics

[edit]

Deprecation

[edit]

asm.js is mostly rendered obsolete with the introduction ofWebAssembly(wasm), which has a bytecode format that is faster to parse.[38]Efforts to extend JavaScript with more low-level features like SIMD.js has also been suspended since 2017.[39]

asm.js remains useful primarily as a "fallback" for wasm, through a program written by the WebAssembly organization that converts wasm to asm.js. There is no dedicated converter from asm.js to wasm, butTypeScript-to-wasm compilers can be partially used.[40]The reference WebAssembly emitter binaryen used to contain anasm2wasmmodule, but it was removed after Emscripten stopped using it.[41]

See also

[edit]

References

[edit]
  1. ^"asm.js in Firefox Nightly".Luke Wagner's blog.21 Mar 2013.Retrieved13 Nov2014.
  2. ^ab"kripken/emscripten · GitHub".Github.com.Retrieved2015-03-05.
  3. ^ab"Firefox 22.0 release notes".Mozilla.RetrievedJuly 4,2013.
  4. ^"Asm.js".Asm.js.Retrieved2015-03-05.
  5. ^"asm.js — frequently asked questions".Asmjs.org. July 26, 2014.
  6. ^"asm.js".Asm.js.Retrieved2015-03-05.
  7. ^abAlon Zakai; Robert Nyman (20 December 2013)."Gap between asm.js and native performance gets even narrower with float32 optimizations".Retrieved11 April2014.
  8. ^"Bringing Asm.js to Chakra and Microsoft Edge".Microsoft. May 7, 2015.RetrievedMay 7,2015.
  9. ^"Chrome 28 Beta: A more immersive web, everywhere".Google.Retrieved2013-07-06.
  10. ^"Home — Demos — Games and Game Engines".
  11. ^"plu".Themucker.github.io. Archived fromthe originalon 2013-08-03.Retrieved2015-03-05.
  12. ^"repl.it — Python".Repl.it.Retrieved2015-03-05.
  13. ^"repl.it — Ruby".Repl.it.Retrieved2015-03-05.
  14. ^"pepper.js Examples".Trypepperjs.appspot.com. Archived fromthe originalon 2020-02-14.Retrieved2015-03-05.
  15. ^"emscripten-qt — Demos".Vps.etotheipiplusone.com. Archived fromthe originalon 2015-02-13.Retrieved2015-03-05.
  16. ^"About Emscripten".
  17. ^"Vim.js — JavaScript port of Vim".Coolwanglu.github.io.Retrieved2015-03-05.
  18. ^"TrueType Fonts in JavaScript".Archived fromthe originalon 2012-10-12.
  19. ^"Port of SQLite to Javascript".Github.com.Retrieved2015-03-05.
  20. ^"GnuPG.js".Manuuels.github.io.Retrieved2015-03-05.
  21. ^"ctags in the browser".Github.com.Retrieved2015-03-05.
  22. ^"Gnuplot online".Gnuplot.respawned.com. Archived fromthe originalon 2015-02-22.Retrieved2015-03-05.
  23. ^"A hack to put GraphViz on the web".Github.com.Retrieved2015-03-05.
  24. ^"JavaScript port of ZLib DEFLATE for the browser".Github.com.Retrieved2015-03-05.
  25. ^"Epic Games Releases 'Epic Citadel' on the Web".UnrealEngine.com(Press release). May 2, 2013. Archived fromthe originalon November 30, 2016.RetrievedOctober 24,2014.
  26. ^"Unreal Engine 3 ported to JavaScript and WebGL, works in any modern browser".ExtremeTech.Ziff Davis.Retrieved2015-03-05.
  27. ^"On the future of Web publishing in Unity".Blogs.unity3d.com. April 29, 2014.
  28. ^"HTML5".Clb.demon.fi. Archived fromthe originalon 2015-03-06.Retrieved2015-03-05.
  29. ^"Compiling for the Web".godotengine.org.November 10, 2016.
  30. ^"Emscripten-Generated Code".Kripken.github.io.Retrieved2015-03-05.
  31. ^"Emscripten-Generated Code".Forandom.github.io.Retrieved2015-03-05.
  32. ^Guryanov Aleksander."Dune 2 - Online (browser version)".Epicport.Retrieved2015-03-05.
  33. ^"Mozilla Banana Bread Demo".Developer.mozilla.org.Retrieved2015-03-05.
  34. ^"Humble Mozilla Bundle pushes WebGL-powered browser gaming".Ars Technica. 15 Oct 2014.Retrieved15 Oct2014.
  35. ^"EM-Dosbox on Github".Retrieved2015-04-09.
  36. ^"Page Redirection".Jsmess.textfiles.com.Retrieved2015-03-05.
  37. ^"HTML5 Fractal Playground".Danielsadvernture.info. Archived fromthe originalon 2015-02-22.Retrieved2015-03-05.
  38. ^"FAQ".WebAssembly.
  39. ^"TC39 proposal for SIMD.js".Ecma TC39. 23 June 2020.
  40. ^"WebAssembly/binaryen".GitHub.WebAssembly. 25 June 2020.AssemblyScript which compiles TypeScript to Binaryen IR; wasm2js which compiles WebAssembly to JS
  41. ^"Binaryen Changelog".GitHub.v97: Remove asm2wasm, which supported Emscripten's fastcomp backend, after fastcomp was removed.(See also thePR#3042.)
[edit]