Not intended to merge in its current state. Just creating this PR for discussion purposes.
Used https://rustwasm.github.io/book/reference/code-size.html for some guidance. In particular this poitned me toward wasm-snip. I used it here to snip out panic code, which is probably what we'd want in most cases, and also to strip out formatting code. The latter is a bit more aggressive and could break things if the game is using Rust's string formatting other than in panics.
Un-post-processed release build on main: ~140 KB
$ cargo build --release
$ ls -go target/wasm32-unknown-unknown/release/*.wasm
-rwxrwxr-x 2 140570 Jan 2 09:26 target/wasm32-unknown-unknown/release/snake_w4_rs.wasm
wasm-opt
only on main: ~99 KB
$ cargo build --release
$ wasm-opt -Oz --strip-producers --dce --zero-filled-memory target/wasm32-unknown-unknown/release/*.wasm -o optimized.wasm
$ ls -go optimized.wasm
-rwxrwxr-x 1 99304 Jan 2 09:30 optimized.wasm
wasm-opt
+ wasm-strip
(strips out debug info) on main (EDIT: you can also just add the --strip-debug
flag when calling wasm-opt
): ~22 KB
$ cargo build --release
$ wasm-opt -Oz --strip-producers --dce --zero-filled-memory target/wasm32-unknown-unknown/release/*.wasm -o optimized.wasm
$ wasm-strip optimized.wasm
$ ls -go optimized.wasm
-rwxrwxr-x 1 22562 Jan 2 09:31 optimized.wasm
wasm-opt
+ wasm-strip
+ wasm-snip
on main: ~13 KB
$ cargo build --release
$ cp target/wasm32-unknown-unknown/release/*.wasm optimized.wasm
$ wasm-snip --snip-rust-fmt-code --snip-rust-panicking-code -o optimized.wasm optimized.wasm
$ wasm-strip optimized.wasm
$ wasm-opt -Oz --strip-producers --dce --zero-filled-memory optimized.wasm -o optimized.wasm
$ ls -go optimized.wasm
-rwxrwxr-x 1 12887 Jan 2 09:35 optimized.wasm
wasm-opt
+ wasm-strip
+ wasm-snip
+ remove usage of Mutex
and lazy_static
: ~7 KB
$ ./build.sh
$ ls -go optimized.wasm
-rwxrwxr-x 1 6876 Jan 2 09:36 optimized.wasm