Thanks to visit codestin.com
Credit goes to github.com

Skip to content

WASM Size Optimization Trick #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

SaumitraLohokare
Copy link

TLDR;

  • Updated build script to perform the following commands:
    • $ wasm2wat client.wasm > client.wat
    • $ wat2wasm client.wat -o client.wasm
    • $ wasm2wat server.wasm > server.wat
    • $ wat2wasm server.wat -o server.wasm
  • Updated README to add dependency on wabt.

Problem

LLVM bloats the generated wasm binary.

Solution

While learning about WASM and WAT I noticed that if you disassemble the wasm binary using wasm2wat, then compile it again using wat2wasm, it significantly reduces the size of the generated wasm binary.
This is important for online games as smaller binaries take less time to load.

Results

# Before
$ ls -alh *wasm
-rwxr-xr-x 1 saumi saumi 172K Jan 17 00:21 client.wasm
-rwxr-xr-x 1 saumi saumi  82K Jan 17 00:21 server.wasm

# After
$ ls -alh *wasm
-rwxr-xr-x 1 saumi saumi 162K Jan 17 00:43 client.wasm
-rwxr-xr-x 1 saumi saumi  70K Jan 17 00:43 server.wasm

On running the game no issues were noticed.

@nikpivkin
Copy link

Hi @SaumitraLohokare ! Have you considered using wasm-opt? This tool is specifically designed for WASM optimization.

@SaumitraLohokare
Copy link
Author

Thanks for the link! I did not know about this! I'll definitely check this out @nikpivkin

@nikpivkin
Copy link

@SaumitraLohokare using wasm-opt with optimization flag O4 I got a file which is 2 times smaller than the original one:

❯ wasm-opt -O4 client.wasm -o client-opt.wasm
❯ du -sh *.wasm
 96K    client-opt.wasm
184K    client.wasm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants