subo icon indicating copy to clipboard operation
subo copied to clipboard

Optimize Wasm file sizes

Open cohix opened this issue 4 years ago • 8 comments

We should run a Wasm bundle size optimization tool (like Wasm-gc) on modules built by Subo

cohix avatar Jan 14 '21 10:01 cohix

hi,, I'm working on this

samderlust avatar Jun 19 '21 21:06 samderlust

@samderlust are you still working on this?

denopink avatar Nov 23 '21 14:11 denopink

A quick experiment with wasm-opt on one of our Rust Runnables from the Reactr repo got these results:

wasm-opt -o rust-get.O3.wasm -O3 rust-get.wasm
wasm-opt -o rust-get.Os.wasm -Os rust-get.wasm

-rw-r--r--  1 jagger  staff   1.5M 23 Nov 11:40 rust-get.O3.wasm
-rw-r--r--  1 jagger  staff   1.5M 23 Nov 11:40 rust-get.Os.wasm
-rw-r--r--  1 jagger  staff   1.8M 20 Nov 21:36 rust-get.wasm

Something is up with our Rust Runnables, because TinyGo spits out much smaller files for similar behaviour.

-rwxr-xr-x  1 jagger  staff    57K 20 Nov 21:36 tinygo-cache.wasm

And AssemblyScript is smaller still:

-rw-r--r--  1 jagger  staff   6.8K 20 Nov 16:54 as-get.wasm

danielledeleo avatar Nov 23 '21 16:11 danielledeleo

@ospencer any ideas?

danielledeleo avatar Nov 23 '21 16:11 danielledeleo

I wouldn't say anything is up. Different languages include different amounts of the runtime in the final binary. Besides hand-written wasm, AssemblyScript will get you the tiniest binaries since the code you write is very similar to what gets emitted (it's not actually TypeScript, so there's no runtime it needs to ship other than malloc and a light GC). It's a similar problem with Swift—they include the entire standard library in the binaries.

Rust is definitely a premier language for WebAssembly, but it wasn't necessarily designed with WebAssembly in mind. It's getting better over time though.

wasm-opt is really smart, but there's a lot of situations that it can't understand how to optimize without some help from the compiler. I can go into a ton of detail on this :)

I can also say that a handful of languages don't need a wasm-opt pass (though it won't hurt). AssemblyScript and Grain, for example, already have wasm-opt run on the wasm as a part of their compilation process.

ospencer avatar Nov 23 '21 17:11 ospencer

@ospencer that makes sense. Since it doesn't hurt to run wasm-opt, is there any downsides to running it as a default behavior for subo build [dir] and then provide a flag to disable it subo build [dir] --no-opt?

denopink avatar Nov 24 '21 18:11 denopink

We could maybe do something like that, yeah. The only thing I'm concerned about is when building with --native, I don't want to require having wasm-opt available on the host machine 🤔

ospencer avatar Nov 24 '21 19:11 ospencer

@samderlust are you still working on this?

@denopink Hi, sorry no, I'm not working on this one right now. I remembered back then I read some documents that mentioned wasm file size was optimized by default or something (Couldn't find it now). So I stopped working on this. sorry for not updating

samderlust avatar Dec 04 '21 03:12 samderlust