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.

  • @[email protected]
    link
    fedilink
    811 days ago

    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]
      link
      fedilink
      3
      edit-2
      11 days ago

      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]OP
        link
        fedilink
        1
        edit-2
        11 days 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.

    • @quilan
      link
      111 days 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]
        link
        fedilink
        311 days ago

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