

Compiling LLJS to asm.js - jlongster
http://jlongster.com/Compiling-LLJS-to-asm.js,-Now-Available-?hn

======
STRML
LLJS, asm.js, & Emscripten are really moving Javascript forward at incredible
speed. I wonder if these projects will affect the future of the JS spec
(ECMAScript Harmony + above). It's an exciting time to be a web programmer.

~~~
samth
These projects are already affecting the spec. Here's a very small example:
[https://mail.mozilla.org/pipermail/es-
discuss/2012-November/...](https://mail.mozilla.org/pipermail/es-
discuss/2012-November/026126.html) which is used in asm.js and already
implemented in Firefox: <https://bugzilla.mozilla.org/show_bug.cgi?id=808148>

~~~
NinjaWarrior
Math.imul is just an ad-hoc patch to improve the performance of Emscripten.
It's totally for Firefox. Is there a motivation to standardize and implement
such a thing for other browser vendors? I'll star the V8 issue though...

------
abecedarius
Yesterday I rewrote a tiny Javascript demo in asm.js and also saw an order-of-
magnitude speedup. (I thought at first it didn't help at all, apparently
because the framerate was limited by setInterval().)

Code at <http://wry.me/hacking/canvas_asm.html>

~~~
simcop2387
Doesn't appear to be doing anything for me,

    
    
        noasm 285
        asm 13
        asm 13
        noasm 269 
    

That's all that appears in the console. Network and everything looks fine, no
404 or other errors.

~~~
Groxx
I wonder if most / all of that speedup is just using typed arrays (edit: nvm,
they both do). I get similar speeds in Chrome, which afaik doesn't have any
special handling for `"use asm";` functions.

~~~
srijs
Typed arrays do deliver a lot of speedup, and in fact, the above example is
slightly faster in Chrome than in Firefox Nightly for me. However, if your
algorithm does crunch a lot of data in a low-level way, there is definitely
even more performance you can get by using asm.js.

See my link in the comment below for benchmarks of a sha1 algorithm that shows
a good speedup in Chrome by using TypedArrays, but an even greater one in
OdinMonkey.

~~~
Groxx
awesome - thanks for doing the legwork! I'll definitely take a look.

------
kayoone
Whats the use for LLJS ? I dont really get it. So its not meant to be used in
web development but for things like games or complete VMs. But isnt it much
simpler for those cases to compile existing C codebases with emscripten
instead of rewriting everything in LLJS ? Even if you start today with writing
a new game engine, wouldnt it make more sense to use C++ and be able to target
any platform there is instead of just the browser?

~~~
jlongster
The main use for something like LLJS is to allow you to target asm.js with new
code that you write. It's true that it's probably not the best idea to
completely rewrite a 3d engine in it (right now), but it gives you a quick way
to rewrite core parts of an intensive app (like a 3d game) for high
performance. There are "inner loops" that could benefit from a small amount of
LLJS.

As the post says, it's a proof of concept and a way to open up asm.js to
people who want to tinker with it without compiling C code with emscripten.

~~~
kayoone
I see, so the main benefit vs compiling c to asm.js is that the development
process is easier because you dont need to manually compile. Sounds
interesting to use as a scripting language inside a c based game engine. So
the core of the engine would be directly compiled to asm.js while the game
logic scripting could be done in LLJS to be able to live-test your game.

~~~
jlongster
That's certainly a possibility! It would be neat to see powerful debugging
tools develop, after the language stabilizes. I also like the idea of somehow
linking it with emscripten-compiled code so that it can be used as a sort of
scripting language.

I'd like to explore the possibility of porting some of the C toolchain to the
browser too, though. It's possible that it could be quite successful, and in
that case LLJS would target people who just want to write small parts of there
app for asm.js.

~~~
kayoone
sounds really exciting, keep up the great work!

------
niggler
Has anyone built the _entire stack in javascript_ (C to JS compiler in JS +
standard library) so that you could write JS applications in C and compile and
run in the browser?

EDIT: to clarify, the ideal solution would be an editor (like codemirror or
ace) with a compile+run button that would compile the code down to JS and run
it.

~~~
jlongster
That's exactly what emscripten does, and is very mature and being used to port
massive 3d games to the web.

<https://github.com/kripken/emscripten>

See the BananaBread demo: <https://developer.mozilla.org/en-
US/demos/detail/bananabread>

~~~
niggler
You have to use the command-line `emcc` tool to do this. I'm asking if that
tool can be eliminated from the workflow.

The ideal solution would be an editor (like codemirror or ace) with a
compile+run button that would compile the code down to JS and run it.

~~~
jlongster
Oh, I see. No, not yet, I think the C compilers are complex enough to be
difficult to compile with emscripten. I have no evidence for that, but I know
they are incredibly complex.

The shorter path is something like LLVM, but we could see something like a
compiler all in js that can take a large subset of C and compile it.

EDIT: see azakai's comment below which provides much better insight into this

------
y0ghur7_xxx
I am sure there is some reason this can't be done, but can we compile
JavaScript to asm.js? If this can be done with C, it can be done with JS?

~~~
jlongster
Sure, but it's different because you need to compile a whole VM to asm.js and
run javascript inside of it. You can't compile a dynamic language like
javascript to low-level code because you need too much information at runtime.

See this project, which compiles SpiderMonkey to javascript via emscripten,
which supports asm.js: <https://github.com/jterrace/js.js/>

~~~
azakai
qt-emscripten does something similar, it builds the JSC interpreter together
with Qt.

------
gruseom
How far are we from being able to do meaningful text processing with typed
arrays and (therefore, presumably) asm.js? Last I checked, there was work
being done on a string encoding/decoding standard but it wasn't implemented in
browsers yet. Can anyone say more about this?

