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

  • @joonazan
    link
    24 years ago

    So optimizing for size increases binary size?

    • @nutomicOPMA
      link
      14 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.