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.
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
You may want to try wasm-tools strip
perhaps as that'd remove debugging-related dwarf custom sections
Wow that really helped. The stripped rust version is now more than 50% smaller than the stripped golang version.
Component before and after wasm-tools strip
Rust 1640386 bytes to 13726 bytes
Golang 107437 bytes to 34011 bytes
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.
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.
dlmalloc is like 4K uncompressed wasm code, so you'd really have to start bending over backwards to reach 1K
(or maybe it was ~8K? been a while since I did this level of size profiling)
True, but you don't need malloc to check if a number is even or odd.
ah, yeah I didn't look at the actual test program
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.
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
Does fmt get pulled in if you panic = 'abort'
?
I believe so because the panic message is printed before the abort
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
Last updated: Nov 22 2024 at 16:03 UTC