The following is a transcript of a talk I gave the the Denver HTML5 Users Group at Rally Software in downtown Denver on Monday, March 23, 2015
The fractal-rendering software demonstrated during this talk lives here: http://danielsadventure.info/html5fractal/docs/intro.html
Daniel: My name is Daniel Langdon. I've been an IT consultant here in Denver for the past couple years and Denver's been really great to me, and I've been looking forward to this opportunity to give a little bit back, so I'm going to be giving you a talk about asm.js, but since I don't have time to do a super deep-dive into the code right now, I'd like to start out by telling you about my motivation to be an early adopter of this system that you may have never heard of. So to begin...
Who can tell me what this is?
Audience members: Fractal! Mandelbrot!
Daniel: Right, the Mandelbrot Fractal! It was discovered by the late Benoit Mandelbrot who passed away just a few years ago. He pointed out that this figure, in its amazing complexity, is actually defined by extremely simple mathematics. In a PBS interview, he said, "As a matter of fact, all you have to do is add and multiply; you don't even have to subtract and divide!" Why then, was this not discovered by Pythagoras or Archimedes?
Single audience member: You have to do a lot of it!
This started out as an experimental feature in FireFox. I thought perhaps it would be a flash in the pan; maybe they wouldn't find the killer app for it. I wish that instead of showing you this, I could show you the Unreal Citadel demo, which I'm told is a port of an Unreal demo, a game engine, that lets you walk around a castle in 3D in a web browser, but unfortunately, they took it down and replaced it with something called "Tappy Chicken", which isn't nearly as good. I have no idea why they did that, but the good news is, just last month, even after I had signed up to give this talk, Microsoft has announced that they are going to put support for asm.js in the latest iterations of their browser stack and not only that, but Google is, too, I think, and Microsoft also announced that support for asm.js was in the top 10 list of features requested for Internet Explorer.
I actually wrote my own little code-generation tool which, since you may know that these images are defined in terms of mathematical functions, I wrote code that interprets what we think of as standard mathematical language, so I can type in a completely different mathematical function, and I know this function generates a beautiful plot, so just to illustrate how it works, I'm going to change the function from "z2 + c", we're going to draw a plot for "sin(z/c)", and you can see that this one takes a good bit of time to load even with asm.js, but it draws a completely different image. This is just one small example of the power you get with asm.js to fill in the void left by having to deal with a dynamic language for doing heavy-duty mathematics.
That's my talk. Are there any questions?
Audience member: Is that C language, or the version you show being compiled?
Audience member: Sort of; so you're going thru several layers, then, but ultimately, you're compiling some code, creating more speed?
Audience member: How can you know for sure if the browser is running it in the optimized asm.js mode or not?
Daniel: That's a great question. In the development console in Mozilla FireFox, it will show you a compilation error if it didn't work, and believe me, I've seen lots of them.
Audience member: What kind of performance gains are you looking at?
Daniel: Compared to other browsers... It's lightning fast compared to IE; we'll see how it is with their new version. Chrome is extremely optimized; it closes the gap a good bit. The only statistic I know off the top of my head is that there was an announcement by the asm.js team that compared to running the code in a native C compiler, it's a slow-down factor of only 1.5, so it's close to native performance compared to anything else you can get on the web.
Audience member: I have a bunch of code for a DSP written in an SSC assembly. Is there a way I can transpile it into asm.js, or do I need to rewrite it in C++?
Daniel: I understand that Emscripten uses an intermediate language called LLVM and I didn't mention this before, but there is a separate tool that you must use to convert from C to LLVM and then to asm.js, so I think you're going to need some compilation tool that will target LLVM or transpile to C.
Audience member: Is asm converted to JVM bytecode, or is it converted to machine language.
Daniel: When you say JVM, do you mean Java Virtual Machine? (Yes?) it doesn't have anything to do with Java as far as I'm aware. As far as I understand, it is compiled to something like machine code, but I don't know the exact specifics. I would have to look into that.
Daniel: Right, and in addition to using asm.js, it also uses Web Workers to parallelize the computations for maximum efficiency in this particular application.
Audience member: You have to hand-code the Web Workers?
Daniel: Yes. Unfortunately, the tools do not generate the Web Workers for you and you're still stuck having to pass strings back and forth as messages, which is one of the intrinsic limitations of the Web Workers you may be aware of, but basically, you have a separate instance of asm.js running this code on each of your web workers.
Audience member: How does asm.js handle the implications of running native code in your web browser. Is there a sandbox for that?
::Camera cuts off:: Thank you.