All these tests are run on Lemmy tag v0.6.74, using cargo build --release.

  • Normal release build: 19mb
  • After running strip 13mb
  • Setting opt-level=z (optimize binary size): 21mb
  • Optimize for size + strip: 13mb
  • Optimize for size + enable link-time optimization: 14mb
  • Optimize for size + lto + strip: 14mb
  • only lto: 15mb
  • lto + strip: 11mb
  • lto + strip + reduce codegen-units: 11mb

Check this document for a detailed explanation of these options

Based on these numbers and the difference in compile time between options (which I didnt record), lto + strip looks like the best option to me.

This is also useful: https://github.com/johnthagen/min-sized-rust

  • DessalinesA
    link
    fedilink
    arrow-up
    3
    ·
    5 years ago

    Cool, I’ll look later, but we have to make sure that smaller binary size doesn’t have a speed cost, I know some of those options do.

    • nutomicOPMA
      link
      fedilink
      arrow-up
      2
      ·
      5 years ago

      LTO should improve performance, as it allows for more optimizations, and dead code gets removed. Strip doesnt affect the behaviour at all. Only disadvantage is a longer compile time with LTO, but I think thats fine for releases.

  • joonazan
    link
    fedilink
    arrow-up
    2
    ·
    5 years ago

    So optimizing for size increases binary size?

    • nutomicOPMA
      link
      fedilink
      arrow-up
      1
      ·
      5 years ago

      In our case yes. Quote from the docs I linked above:

      It is recommended to experiment with different levels to find the right balance for your project. There may be surprising results, such as level 3 being slower than 2, or the “s” and “z” levels not being necessarily smaller. You may also want to reevaluate your settings over time as newer versions of rustc changes optimization behavior.