Cheerp 1.2 – C++ to JavaScript: faster than Emscripten with dynamic memory
blog.leaningtech.com
blog.leaningtech.com
The slower startup speed with asm.js enabled, at least in Spidermonkey is because it does Ahead-Of-Time (AoT) compilation. The advantage is that once the binary code is generated, it will never be touched again. Other JS engines might decide to de-optimize and re-compile some code, or go through several 'tiers' and only optimize the hot-running parts, which means slow first-time execution and at worst stuttering at unpredictable times. Both are deadly for apps where a smooth frame update is required.
I wonder why startup speed in V8 is slow though, AFAIK it doesn't do AOT compilation, or is this now the case with the new Turbofan module?
The second area, dynamic memory growth is unfortunate at first glance, but a trade-off for better performance, I wonder if pre-allocating a large chunk of memory is still as much an issue in 64-bit browsers as with 32-bit.
I'm not too happy that the browser that was first to deprecate native plugins didn't provide usable asm.js as a replacement first.
Correct, Turbofan does a lot more up-front compilation for asm.js. There is also work in v8 to do full asm.js validation and AOT using that info,
https://bugs.chromium.org/p/v8/issues/detail?id=4203
> I wonder if pre-allocating a large chunk of memory is still as much an issue in 64-bit browsers as with 32-bit.
On 64-bit it's mostly a non-issue, yeah, you can allocate a single 1GB+ chunk fairly easily.
There have also been big improvements on 32-bit Chrome over the last year,
https://code.google.com/p/chromium/issues/detail?id=394591
https://code.google.com/p/chromium/issues/detail?id=533580
But again, this is mostly a non-issue on 64-bit, and 64-bit is rapidly becoming the norm anyhow.
I get the feeling this is one the one hand pretty cool, but on the other hand poorly timed. Unless WebAssembly never arrives or arrives really late, there's going to be a way to run almost-native code with dynamic memory allocation fairly soon. So it's likely this will soon be relegated to "old-android support".
1. Normal output, asm.js, no memory growth. Maximal speed, fixed memory size.
2. Memory growth. As the article mentions, asm.js no longer supports this, but Emscripten does. It just removes the 'use asm'.
3. Split memory. As the article mentions, this is non-asm.js. This supports not just growing and shrinking memory, but allocating each chunk of memory separately, leading to flexibility similar to that of Cheerp.
The article compares 1 and 3, but not 2. In practice, almost all Emscripten users use 1, a small amount use 2, and as far as I know almost no one uses 3. Also, 2 would start up much faster than 1, and run much faster than 3, so it would be an interesting comparison to Cheerp.
Regarding startup time, there are two things that I think should have been measured:
1. Large programs. The largest they test is the Bullet physics library, which is still quite small.
2. Execution of the first few frames. Non-asm.js code, like Cheerp output, is compiled dynamically. This generally means that startup is fast, then the first few frames are very slow, then it eventually gets smooth. The measurements in the blogpost appear to ignore that.
Both of those things - larger programs, and more sophisticated measurements of startup speed - are present in the open source Massive benchmark [1]. It would be interesting to see Cheerp compared to Emscripten on that (and also on the 2nd mode mentioned earlier).
Startup time: https://docs.google.com/spreadsheets/d/17fFM1YqhMV2-O0ZdymEv...
Execution time (small benchmarks): https://docs.google.com/spreadsheets/d/17fFM1YqhMV2-O0ZdymEv...
Execution time (larger benchmarks): https://docs.google.com/spreadsheets/d/17fFM1YqhMV2-O0ZdymEv...
Im very excited sbout competition in this space even if it becomes impractical
there are all these questions about how to use LAPACK from js.. and none seem to work right.
[1] http://stackoverflow.com/questions/21990243/use-emscripten-w...
[2] https://github.com/software-engineering-amsterdam/MLitB/issu...
These demos run in the browser, and natively on iOS, Android, Windows, Linux, OSX with the smallest size and best performance on each platform compiled from the same C++ code: http://floooh.github.io/oryol/, http://floooh.github.io/voxel-test/, http://floooh.github.io/virtualkc/
The better performance then manually written JS comes from the use of asm.js, LLVM's optimizer passes, and a simple linear memory model which 'preserves' spatial locality also present in the natively-compiled C/C++ code (if the programmer paid attention to this)
Passing an URL along from which the code directly runs is the simplest distribution model imaginable, both for the 'publisher' and for the user.
PS: the performance part applies to emscripten and asm.js, not necessarily to Cheerp which uses a different approach
I'm still deciding on which libraries to use, and Oryol suddenly seems like a great option, though what I'm making is not a game and is not 3D (basically a score editor). Other options considered were SDL(2) or something more basic like GLFW. But Oryol's killer feature here is that it's specifically designed to result in a lightweight web version. (Though SDL1 also results in small sizes, but that's because the emscripten team put a lot of work into converting SDL calls into browser API calls)