Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

compiler optimization - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...