this post was submitted on 23 Jan 2025
6 points (80.0% liked)

Rust

6243 readers
23 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

[email protected]

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS
 

I shaved off 10 MiB from my binary in 2 hours!

I made a program using Macroquad, then I built it in release mode, the binary was 63 MiB in size.

So I used cargo vendor to have a better look at Macroquad and one of its dependencies, glam.

I then started to delete code, like, lots and lots of code(about 30_000 lines of code); none of it affected my main project, some of it became 'dead_code' just by removing the pub keyword.

The result is that my project was unaffected and the binary went down to 52 MiB.

Is there a way to automate removal of unneeded elements from dependencies? This is potentially huge.

top 12 comments
sorted by: hot top controversial new old
[–] KillTheMule 7 points 20 hours ago (3 children)

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?

[–] [email protected] 2 points 20 hours ago* (last edited 20 hours ago) (1 children)

To expand: Just configure whatever profile you're using (dev, release, ...) to have link time optimization (lto) enabled:

[profile.release]
lto = "fat"

Reference

[–] [email protected] 1 points 20 hours ago* (last edited 20 hours ago)

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.

[–] [email protected] 1 points 20 hours ago (1 children)

The compiler doesn't consider it to be dead code since it's marked pub.

[–] KillTheMule 3 points 19 hours ago (1 children)

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 :)

[–] [email protected] 1 points 19 hours ago

Yes that's exactly what I want. The compiler should stop considering it accessible.

[–] [email protected] 1 points 20 hours ago

I don't recall what the default behavior is with the linker, but it might also benefit from at least thin LTO.

[–] [email protected] 3 points 20 hours ago (1 children)

@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

[–] [email protected] 1 points 20 hours ago* (last edited 20 hours ago) (1 children)

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

[–] [email protected] 1 points 20 hours ago (1 children)

@Doods I'm surprised you can gain that much with that already enabled!

[–] [email protected] 2 points 20 hours ago* (last edited 20 hours ago) (1 children)

Should I bring it up to the 'min-sized-rust' working group or the forums or something?

[–] [email protected] 3 points 17 hours ago

Yes. This behavior seems strange, so either an explanation or investigation by a compiler dev seems like it would be helpful.