Why does `cargo build` not optimise by default?

Optimisations get in the way of a nice compile-test/debug cycle for several reasons:

  • they take time (as @DanielKeep says)
  • they hinder debugging:
    • inlining of functions means panic! backtraces (by setting RUST_BACKTRACE=1) may not point to the actual function that caused the problem
    • more generally, it is non-trivial to preserve good debug info (as used by debuggers like gdb) in the presence of optimisation, so getting gdb to connect a certain chunk of instructions back to the code that generates it may be hard-to-impossible

Also, a release build (for Rust software but also C/C++, etc.) would generally not have any debug information at all and would often have (the most expensive/most conservative) assertions turned off. Cargo's --release flag by default not only enables optimisations, but also disables debug info & debug assertions, making the debugging experience extremely annoying, and hiding any bugs that would be revealed by detection of, for example, integer overflow (one of the debug assertions).

4 Likes