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.

all 15 comments

sorted by: hot top controversial new old
[–] 9 points 2 years 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?

  • source
  • hideshow 6 child comments
  • [–] 3 points 2 years ago* (1 child)

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

    [profile.release]
    lto = "fat"
    

    Reference

  • source
  • parent
  • hideshow 2 child comments
  • [–] [S] 1 point 2 years ago (1 child)

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

  • source
  • parent
  • hideshow 2 child comments
  • [–] 4 points 2 years ago (1 child)

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

  • source
  • hideshow 2 child comments
  • [–] 3 points 2 years ago (1 child)

    @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

  • source
  • hideshow 2 child comments
  • [–] [S] 1 point 2 years ago* (1 child)

    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

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 2 years ago (1 child)

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

  • source
  • parent
  • hideshow 2 child comments
  • [–] [S] 2 points 2 years ago* (1 child)

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

  • source
  • parent
  • hideshow 2 child comments