Actually, dead code eliminination should do the trick, if you're compiling a binary at least (same for a library I think, but there could be re-exports there). Did you compile in release mode?
Rust
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
To expand: Just configure whatever profile you're using (dev
, release
, ...) to have link time optimization (lto) enabled:
[profile.release]
lto = "fat"
This really doesn't seem to do the trick, the binary's still at 63MiB.
Also "fat"
and true
are identical.
Edit: I'm not sure I replied to the right person, ignore this.
I don't recall what the default behavior is with the linker, but it might also benefit from at least thin LTO.
The compiler doesn't consider it to be dead code since it's marked pub
.
Sure, but isn't this in a dependency? Can't be reached when only importing your crate anyways? And if you're building a binary, I don't think this could really considered exported, is what I mean :)
Yes that's exactly what I want. The compiler should stop considering it accessible.
@Doods are you using release
? If you want the best dead-code elimination, you can also enable Link time optimisations (LTO): https://doc.rust-lang.org/cargo/reference/profiles.html#lto
Yes, I am using both LTO and release mode, I can show you:
[profile.release]
opt-level = 3
codegen-units = 1
panic = "abort"
strip = true
lto = true
cargo build --release
@Doods I'm surprised you can gain that much with that already enabled!
Should I bring it up to the 'min-sized-rust' working group or the forums or something?
Yes. This behavior seems strange, so either an explanation or investigation by a compiler dev seems like it would be helpful.
Just so you know I figured it out, re-read the post please.
I'm glad you figured it out.
I don't know what is it with new rustaceans and binary size, but many of them seem to be incessant on wasting time uselessly optimizing it, usually without actually understanding what they're doing, without gaining any relevant benefit, and possibly even causing relevant harm (e.g. degrading run-time performance for no good reason).