17

I'm writing a program that's quite compute heavy, and it's annoyingly slow to run in debug mode.

My program is also plagued by integer overflows, because I'm reading data from u8 arrays and u8 type spreads to unexpected places via type inference, and Rust prefers to overflow rather than to promote integers to larger types.

Building in release mode disables overflow checks:

cargo run --release

How can I build Rust executable with optimizations and runtime overflow checks enabled as well?

Kornel
  • 97,764
  • 37
  • 219
  • 309

1 Answers1

22

You can compile in release mode with overflow checks enabled:

[profile.release]
overflow-checks = true

This passes -C overflow-checks=true to the compiler. In earlier versions of Rust, overflow-checks was part of the debug-assertions switch, so you may need to use that in certain cases.

Other times, the easiest thing might be to build in test or dev mode with optimizations:

[profile.dev]
opt-level = 3
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 3
    Note: to get a good speed-up but still have meaningful stacktraces you might want to use `opt-level = 1` only. Each function body will get optimized, however in general there is no (or little) inlining. Of course it's not as fast... – Matthieu M. Dec 03 '15 at 07:44
  • For inlining there's `#[inline(never)]` and `#[inline(always)]` (of course use carefully only if you notice compiler doesn't do a good job, because overuse of these attributes can backfire). – Kornel Feb 08 '17 at 12:26