18

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?

1 Answer 1

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
2
  • 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... Dec 3, 2015 at 7: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 8, 2017 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.