Stream: wasmtime

Topic: Rust components significantly larger than TinyGo components


view this post on Zulip adam (Apr 04 2024 at 19:12):

https://github.com/eighty4/learn-wasm-components/blob/main/README.md?plain=1#L43

Is there a way to minimize the filesize of a component build with Rust? I'm seeing a 12x larger component making a one liner arithmetic component in Rust vs using TinyGo.

Contribute to eighty4/learn-wasm-components development by creating an account on GitHub.

view this post on Zulip adam (Apr 04 2024 at 19:14):

Does using the wit_bindgen::generate macro add to the compiled WASM source because its a dependency?

The component:

https://github.com/eighty4/learn-wasm-components/blob/main/component/rust/src/lib.rs

Contribute to eighty4/learn-wasm-components development by creating an account on GitHub.

view this post on Zulip Alex Crichton (Apr 04 2024 at 19:25):

You may want to try wasm-tools strip perhaps as that'd remove debugging-related dwarf custom sections

view this post on Zulip adam (Apr 04 2024 at 21:38):

Wow that really helped. The stripped rust version is now more than 50% smaller than the stripped golang version.

view this post on Zulip adam (Apr 04 2024 at 21:39):

Component before and after wasm-tools strip
Rust 1640386 bytes to 13726 bytes
Golang 107437 bytes to 34011 bytes

view this post on Zulip bjorn3 (Apr 05 2024 at 15:58):

Since rustc 1.77 in release mode all debuginfo will be stripped by default (including the debuginfo of the standard library that was kept by linking even when debuginfo generation for the main program is disabled as was already the case for release mode). The symbol table is still preserved though.

view this post on Zulip Joel Dice (Apr 05 2024 at 16:06):

And if you really want to go down the rabbit hole, there are a lot more tricks you can use: https://github.com/johnthagen/min-sized-rust. I'd expect you could get it well under 1KB if you wanted to.

🦀 How to minimize Rust binary size 📦. Contribute to johnthagen/min-sized-rust development by creating an account on GitHub.

view this post on Zulip fitzgen (he/him) (Apr 05 2024 at 16:07):

dlmalloc is like 4K uncompressed wasm code, so you'd really have to start bending over backwards to reach 1K

view this post on Zulip fitzgen (he/him) (Apr 05 2024 at 16:07):

(or maybe it was ~8K? been a while since I did this level of size profiling)

view this post on Zulip Joel Dice (Apr 05 2024 at 16:07):

True, but you don't need malloc to check if a number is even or odd.

view this post on Zulip fitzgen (he/him) (Apr 05 2024 at 16:08):

ah, yeah I didn't look at the actual test program

view this post on Zulip Joel Dice (Apr 05 2024 at 16:13):

FWIW, I was able to get to 1.3KB using CARGO_PROFILE_RELEASE_LTO=true for a no_std app that did a lot more than the test program here.

view this post on Zulip fitzgen (he/him) (Apr 05 2024 at 16:36):

another thing to look out for is panics, which pull in std::fmt stuff that is hard to prove cannot be called because it is all indirect calls. things like division, unwrap, etc

view this post on Zulip Lann Martin (Apr 05 2024 at 16:55):

Does fmt get pulled in if you panic = 'abort'?

view this post on Zulip fitzgen (he/him) (Apr 05 2024 at 16:56):

I believe so because the panic message is printed before the abort

view this post on Zulip Ramon Klass (Apr 05 2024 at 17:00):

https://github.com/johnthagen/min-sized-rust?tab=readme-ov-file#remove-panic-string-formatting-with-panic_immediate_abort this guide also mentions that you need nightly to fully get rid of std::fmt

🦀 How to minimize Rust binary size 📦. Contribute to johnthagen/min-sized-rust development by creating an account on GitHub.

Last updated: Nov 22 2024 at 16:03 UTC