Explicit undefined check
The asm/wasm load code contains a lot of if (Module[name]) if (!Module[name]) to check if name is in Module.
It is known that explicit checking Module[name] === undefined is faster for V8 (in fact 15% in some cases). Although no benchmark about this is made on Emscripten, I believe it is worth to do the switch.
I propose to do the following transformations.
if (Module[name])->if (void 0!==Module[name])if (!Module[name])->if (void 0===Module[name])if (condition)->if (!0===condition)if (!condition)->if (!1===condition)
This should not increase size of load code too much if gzipped. Startup performance might be improved.
If no one objects, I will prepare a PR for this.
Reference:
http://benediktmeurer.de/2017/04/03/v8-behind-the-scenes-march-edition/
I think these all reduce the clarity of the code, personally. And slightly increase code size. So unless this is a big speedup, I don't think it's worth doing. And I suspect it doesn't matter much, many of those checks are done once during startup. But, if you measure an interesting speedup, I'd be very interested to see.
@kripken I went through the code and modified the checks where it is obviously either undefined or something else, the rest is left untouched because in many !v it is not clear to me it is trying to check for null or false or 0 or even "".
Note: .js files are compiled with -O2 -s INVOKE_RUN=0 -s NO_FILESYSTEM=1 -s WASM=1 to ensure .wasm execution time is not included. I use external closure compiler (latest) to minify code (simple opt) and calculate gzipped size. Measuring is done with latest d8.
Even without modification, startup time is only about 7-9 ms. After modification, the startup time only reduces by 0.0x ms. "Original" increases by 0.5KB, after being compiled by closure compiler the increase is 0.4KB, after gzipped the increase is 50B.
Conclusion
I agree that the modification does not worth it. (I must admit I am rather disappointed)
(Some comments not entirely related to this issue)
- Some functions are used before they are defined. For example,
shell.jsusesassertbutassertis not defined untilpreamble.js. This will not cause execution error, but for a C/C++ programmer this is can be confusing. - In
ccallthe lineassert(!returnType, 'async ccalls cannot return values')should really bereturnType==="". (The fact that!""== true is not very intuitive)
Overall the code is pretty hacky to handle wasm and asmjs with same template file. I hope as WebAssembly becomes more mature the template will be refactored properly. Maybe consider to use a third party template engine in JS or Python instead, we will be able to do some code evaluation inside template.
Thanks for verifying performance here. Sometimes these things are surprising!
About the other comments, I agree that the code might be a little odd to C++ devs (or even closure compiler using devs, used to more rigorous type usage). It's written in a somewhat more "modern" JS-ey style, which has tradeoffs.
About asm.js and wasm together, the long-term goal is to deprecate and remove asm.js support, once wasm is everywhere. That will take a while, but hopefully not years, as browsers are making fast progress. (We'd leave asm.js support to older emscripten versions, so it will still be there for those that need it, of course.)