Should default recommended opt-level for minimizing binary size be "s" or "z"?

Hello, maintainer of min-sized-rust here. The purpose of the repo is to serve as a starting point for people to learn about the ways to minimize the size of a Rust binary.

I have historically recommended opt-level = "z" since from the docs it seems like it should be "s" + also don't unroll loops (i.e. strictly better). But someone recently reported that "z" performed noticabley worse at size optimization than "s" in their example.

The docs state that this can happen

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.

So for me, I was curious from those that know much more about rustc and LLVM, what should I put as the default in min-sized-rust? I presume many users will simply copy whatever the default is there. Should I default instead to "s" (e.g., is "s" simpler such that it generally performs more reliably?)

I plan to at a minimum update the guide section for opt-level to include a reference to the fact that "s" and "z" might not always be smaller. Any other insights that might help me craft this in the best way would be appreciated.

6 Likes

Results are mixed in my experience. Try both and measure.

Yeah I would say something along the lines of:

  • s and z are usually about the same, neither is consistently better
  • use s if it's good enough, otherwise try z
1 Like

Thanks for the response. I made an update to make users more aware of the surprises with opt-level.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.