Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile and run an optimized Rust program with overflow checking enabled

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?

like image 351
Kornel Avatar asked Dec 02 '15 22:12

Kornel


People also ask

How do you turn on overflow in Rust?

If your code relies on overflow occurring, use wrapping_add , or the Wrapping type.

How many release profiles does rust Optimisation level have?

You'll only compile in release mode once, but you'll run the compiled program many times, so release mode trades longer compile time for code that runs faster. That is why the default opt-level for the release profile is 3 .


1 Answers

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
like image 118
Shepmaster Avatar answered Sep 24 '22 02:09

Shepmaster