Auto debloat unused `pub` items from dependencies.
from Doods@infosec.pub to rust@programming.dev on 23 Jan 17:46
https://infosec.pub/post/22773873

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.

EDIT: I FIGURED IT OUT!!!

My mistake was measuring the size of “target/release”, I discovered that that folder contains many “unnecessary” files, like “deps”, which greatly bloat the folder’r size, the actual size of my binary is 864K.

I am so relieved.

#rust

threaded - newest

KillTheMule@programming.dev on 23 Jan 17:53 next collapse

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?

quilan@lemmy.world on 23 Jan 17:57 next collapse

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

Starfighter@discuss.tchncs.de on 23 Jan 18:06 next collapse

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

[profile.release]
lto = "fat"

Reference

Doods@infosec.pub on 23 Jan 18:12 collapse

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.

Doods@infosec.pub on 23 Jan 18:18 collapse

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

KillTheMule@programming.dev on 23 Jan 19:02 collapse

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

Doods@infosec.pub on 23 Jan 19:44 collapse

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

sgued@pouet.chapril.org on 23 Jan 17:55 next collapse

@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

Doods@infosec.pub on 23 Jan 18:05 collapse

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

sgued@pouet.chapril.org on 23 Jan 18:07 collapse

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

Doods@infosec.pub on 23 Jan 18:14 collapse

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

TehPers@beehaw.org on 23 Jan 21:08 collapse

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

Doods@infosec.pub on 24 Jan 19:11 collapse

Just so you know I figured it out, re-read the post please.

BB_C@programming.dev on 25 Jan 04:52 collapse

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