Skip to content

Instantly share code, notes, and snippets.

@floooh
Last active June 5, 2018 17:41
Show Gist options
  • Save floooh/4c86bf34645d42ad6cbd48856d5ff0b1 to your computer and use it in GitHub Desktop.
Save floooh/4c86bf34645d42ad6cbd48856d5ff0b1 to your computer and use it in GitHub Desktop.
Compiling galogen for emscripten

Ok, here's what I found out :)

  1. the file is 9 MB because of the embedded gl.xml file... this is about 2.7 MB but gets blown up when embedded in to the asm.js (probably because of base64 encoding and extra commas)
  2. the way the program is currently compiled, everything is in the .html file, and it looks like your web servers doesn't compress .html (fixing the server-side compression would be the most important thing to fix, if you have access to the web server configuration)
  3. I played around with the compile options, first that the gl.xml is put into a separate binary file (this reduces the size from 9 MB to the 2.7 MB of the original gl.xml file), and some other options to reduce the size of the generated asm.js code.

The command line is (I've put this into a build_emsc.sh file)

em++ galogen.cpp third_party/tinyxml2.cpp -fno-rtti -fno-exceptions -std=c++11 -stdlib=libc++ -o galogen.html -Os --llvm-lto 1 -DNDEBUG -s WASM=0 --shell-file galogen-shell.html -s ASSERTIONS=0 -s DISABLE_EXCEPTION_CATCHING=1 -s NO_EXIT_RUNTIME=1 -s TOTAL_MEMORY=134217728 -s EXPORTED_FUNCTIONS='["_jsmain"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap", "stringToUTF8", "setValue"]' --preload-file third_party/gl.xml

This yields the following files:

2708699  galogen.data
  11721  galogen.html
  24539  galogen.html.mem
 668472  galogen.js

If you can configure your web server to serve compressed files for the .js and .data file extensions, the download sizes would be like this (tested with gzip -c xxx >xxx.gz)

 galogen.data: 224 KByte
 galogen.js:   150 KByte

If you host on github pages, the .js file will be compressed, but most likely not the .data file (since the github server only compresses a small set of file types with known file extensions). So if you want to host on github pages, it might make sense to replace the --preload-file compile arg with --embed-file, it would look like this:

  11721  galogen.html
  24539  galogen.html.mem
9715976  galogen.js (there's the >9MB file again)

After compression it would be around 496 KByte, about 170 KByte bigger then the separate .data+.js version, but if you don't have full control over the web server configuration (and can only compress .js files), this is the best option.

If you cannot compress at all, then the 2.7 MB galogen.data + 0.67 MB galogen.js option from above is better than a 9.7 MB download :)

And that's it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment