Full std crate is linked accidentally in wasm binary?


#1

Hi all,

When I was experimenting WASM with rust, I accidentally found that even very little function was used, the whole std crate seems being included in the final WASM binary, which bloats the final binary about 600 KB. In my experiment, I just wrote a simple crc32.

The full story is in the stackoverflow post, and I already made a simplest demo for this issue.

I already have a temporary work-around for this by manually use no_std, which follows instructions on official document.

I’m not familiar with Rust yet and not sure about posting this here, but I think someone may interested in this issue.

Thanks.


#2

IIRC, no gc is done on the resulting binary for wasm yet, so there’s no removal of dead code.

In the meantime, you can use wasm-gc or another tool such as cargo-web or cargo-wasm to strip the binary.


#3

I think that’s incorrect now that we use LLD; wasm-gc should be no longer needed.

OP, what compiler version are you using?


#4

wasm-gc is helpful on this. It shrinks the binary size from about 600KB down to 200KB+. But it’s still not enough. In my case, the actual code is only about 1KB. It looks like cargo-wasm uses wasm-gc internally, so the result could be the same. I haven’t tried yet.

It’s rustc 1.28.0-nightly (2a1c4eec4 2018-06-25), which I believe it’s a pretty new version. Demo in Github link should be able to reproduce this issue.


#5

I’ve written some details on this in the past but the tl;dr; is that for the smallest binary size today with the wasm32-unknown-unknown target you need to enable LTO. With LTO I generate a 152-byte binary with today’s nightly.

This story will get better once we upgrade LLD. After that happens if you disable LTO I generate a 158 byte binary by default.


#6

Thanks! It was my mistake to comment out LTO earlier during debugging.

LLD looks promising for me. Can’t wait for that.