Bringing Asm.js to Chakra and Microsoft Edge
blogs.windows.com
blogs.windows.com
Isn't that something that benefits us all? I'm happy they're trying, at least.
Seems to me THAT is the way to go, but this is not my area of expertise.
I absolutely agree there are advantages to taking the asm.js -> LLVM bytecode shortcut (cue every single benchmark showing FF on top). But compiler warnings are not one of them.
EDIT: To clarify: my point is that this is not classical compilation, but rather "interpretation of the generated code". Nobody writes asm.js by hand, no matter how it is executed. If there are errors in there, there is something seriously wrong with the tooling, and when the errors show up will be the least of your worries. Comparable to errors in a .jar file or a .pyc---this is just not something we need to be generally concerned with.
EDIT2: I don't mind the downvotes but if I'm wrong, please explain so at least I understand. Otherwise I won't learn.
No, emscripten compiles asm.js from LLVM IR, but asm.js itself is more like a portable bytecode for a 32-bit register machine.
> The programmer sees errors when generating the asm.js, not when executing it.
Why would that be the case? The point of asm.js is so browsers can optimise it. It doesn't matter what your tooling thinks, in the end what matters is whether browsers accept it.
> Whether it is then executed by a JIT or not is, in this case, irrelevant.
No, whether it is executed by a JIT is quite important. asm.js is a subset that browsers can validate and then compile ahead-of-time. If your code is failing validation and falling back to the usual JavaScript mode, there's a big performance penalty, and your code isn't asm.js-compliant!
> Nobody writes asm.js by hand, no matter how it is executed.
Actually, some people do. It's not the nicest of languages, but there are some people who do.
But even if you don't, what if you're using buggy or outdated tooling producing incorrect output? What if you're targeting a browser that doesn't support some new asm.js feature? You need to know if your code didn't validate!
Who?
Here's are some quotes from the author of that very library:
"asm.js requires a style of coding only compilers can output. A person writing actual asm.js code by hand would need to be insane as asm.js code required style of coding is horribly disorganized"[1]
"Unfortunately asm.js requires one giant array to put things on. No one in their right mind codes like that by hand."[2]
1: https://github.com/taisel/IodineGBA/issues/16#issuecomment-2... 2: https://github.com/taisel/IodineGBA/issues/16#issuecomment-2...
https://github.com/s-macke/jor1k/blob/master/js/worker/or1k/...
Handwritten asm.js code. Around 10 hours or porting time.
When you load some asm.js code in Firefox and it compiles without errors or warnings, you then know for sure your code was fully compiled and you will not see parsing/compilation happening half-way through a game frame. This means it's slightly easier to reason about the performance of your code.
AFAIK, with the Chrome strategy, you have to think about JIT compilation kicking in at any point, which is unpredictable and completely out of your control.
You make a good point though; it's not like we are seeing helpful warnings when writing our frontend JS apps. But those warnings are still crucial when actually building & deploying stuff; with various tooling and browsers it's really nice to see a "successful asm.js compilation" message and you know it's working. If you upgrade a tool, you can be sure that it's still working, etc.
Also, it's not uncommon for people to write new languages or compilers and having that feedback that you are on the fast path is really nice.
I think your point has some merit though.
The other benefit to AOT is simply that, when the app starts running, it immediately starts running in the fast path.
And I didn't know it was just derived from it; I thought asm.js was idempotent with llvm ir!
LLVM IR is SSA and strongly typed, while asm.js is non-SSA and only has a simple, machine-level type system, just to name two large differences.
Related, and you've sort of mentioned this: having your browser be able to validate asm.js is useful for web developers, because then they know if their code is broken (and so will run slower).
asm.js runs currently through an foreign function interface in Firefox, which makes calling the asm.js code slower.
Also, both are based on single-vendor, single-implementation technology. NaCl used actual native code, so if you're not using x86-64 or ARM, too bad! And PNaCl uses LLVM, so there's only one implementation.
Compare this to asm.js. It's a strict subset of ECMAScript/JavaScript, which has several high-quality implementations, and is portable across platforms. It uses the existing standard web APIs, which also have several high-quality implementations.
It's still wasted effort, though. Maybe they share some code, but Pepper is an unnecessary extra API. One that's non-standard and results in vendor lock-in.
The general consensus among non-Google browser vendors was that just using the existing browser APIs was a more desirable approach than Pepper. Since then, Pepper has remained a Chrome-specific technology.
I can AOT compile a complete binary, and can run on (just about) any target.
If I want to implement asm.js, I have to implement a full JavaScript JIT (if I want decent performance). This is Hard. I can't AOT compile, by the nature of JavaScript.
So, no, there is a difference. One of these technologies brings the entire bloated browser technology stack along with it, and one finally cleans up that bloat.
What makes asm.js "insane" by comparison? The syntax?
There's a bytecode for asm.js too if you want it. https://github.com/kripken/emscripten/wiki/Emterpreter
> If I want to implement asm.js, I have to implement a full JavaScript JIT (if I want decent performance). This is Hard.
Writing a compiler that gets decent performance for LLVM is also Hard. (Go look at the size of the x86 backend alone in LLVM.) If you were writing an asm.js engine from scratch with no support for any JS other than Emterpreter bytecode, asm.js is probably even a bit easier than supporting PNaCl, due to the lack of types and no SSA. But honestly, the amount of work you need to parse a different bytecode doesn't matter much compared to the amount of work you need to do to write a good compiler, which you would have to do either way.
> One of these technologies brings the entire bloated browser technology stack along with it, and one finally cleans up that bloat.
Except that, as I mentioned above, you're never going to "clean up" the stack. HN, which you're using to post this comment, hasn't even moved beyond the <font> and <center> tags; what chance is there for browsers to drop all that technology when many sites haven't even adopted CSS1? The difference isn't between "HTML + CSS + JS" and "alternative stack", it's "HTML + CSS + JS + asm.js" and "HTML + CSS + JS + alternative-stack-that-duplicates-the-features-of-the-previous-three". You have to take that into account when talking about the complexity calculus.
Firefox AOT compiles asm.js. All you have to do is validate it (like you would any other bytecode) and then you can in fact AOT compile it. You don't even need a full JS parser, since asm.js only uses a subset of the syntax allowed in JS.
We're not supposed to be giving any application all power they want. We just want to let them use the GPU and take inputs on focus, and if they misbehave we kill them. If they need anything else, they'll have to ask the user. It took Microsoft what, two decades? to realize this. And it's only sightly better now. The mobile OSes were the first to grasp this but it's still imperfect.
Java almost got there but I believe it lost traction because of UI, lack of clear leadership and being tied to a language.
We're going to have to go with browsers because although it's a big pile of hacks they are finally realizing the obvious (in hindsight) way we should develop most applications. I have no doubt it will continue to catch on.
Edit: to those downvoting me (classic), when I left this comment the parent was at -2 or -3 votes and at the bottom of the page.
asm.js is attractive because it is a small extension to the Web platform, so the overall additional complexity of an HTML/CSS/JS engine (which we are likely never going to be able to drop) doesn't go up much by implementing asm.js optimizations.
That's why everybody should actively use non-standard things that they want to become standards.
ActiveX
Java Applets
Flash
Silverlight
VBScript (and other non-JS scripting langs)
NaCL
Dart (via a native VM)And on "emulating much better native platforms", well those native platforms are also trying (unsuccessfully) to emulate the web and the web is still winning.
However I bet the Android team only added the NDK forced by upper management, given how little care they give to it.
While Chrome's results have been impressive, in my experience Firefox does much better for Asm.js code - at least for the games I've been playing. So what benchmarks have you seen?
Also, Google not getting involved in asm.js to make it better, but developing and promoting PNaCL and Dart, well that to me just smells like Microsoft's lock-in tactics with IExplorer in the nineties.
Chrome already provides some speedup on asm.js code https://hacks.mozilla.org/2015/03/asm-speedups-everywhere/
Looks to be the year of WebGL + asm.js across all browsers sometime this year. If so next year could be big for WebGL and web gaming again. And one day it could also make an impact on mobile but that seems to be moving ahead with things like Apple Metal and Khronos Vulkan (OpenGL successor - https://www.khronos.org/vulkan).
I understand why other browsers don't implement the Pepper API, because it's highly Chrome-specific; however, I'd like to see other browsers implementing the native-code sandbox, at least.
Secondly, it's based on a self-contained open source spec that constitutes a logical subset of another widely-supported open spec (ECMAScript) - no convoluted, versioned APIs coordinated by large, possibly competing and mutually incompatible engineering efforts. The asm.js spec is actually so simple it fits on a single web page: http://asmjs.org/spec/latest/
Yet it pretty much manages to achieve all that NaCl does by being also forwards-compatible with all the DOM-based extensions like HTML5 without needing any additional APIs (many asm.js demos for example bind to WebGL - this does not require any additional "asm.js API", as the browser's existing WebGL implementation suffices)
> Secondly, it's based on a self-contained open source spec that constitutes a logical subset of another widely-supported open spec (ECMAScript) - no convoluted
I'm going to have to stop right there. asm.js is a lot of things, but "not convoluted" is certainly not one of them. asm.js involves compiling native code to JavaScript in the hopes that browsers will translate it back to some semblance of the same native code.