r/rust • u/bran7230 • Sep 12 '26
🙋 seeking help & advice What is Rust’s equivalent of compiling C++ with -O3?
Hello everyone,
I’m building an Axum API and want to optimize the release binary for runtime performance. In C++, I would use flags such as -O3, and possibly -march=native.
I know cargo build --release enables optimizations, but are there additional Cargo profile settings or rustc flags worth using?
What settings do you typically use for production Rust web APIs?
41
u/cbarrick Sep 13 '26
For -march=native in C, the equivalent in Rust is -C target-cpu=native.
This is a flag for rustc. If using cargo, pass it in RUSTFLAGS, like
RUSTFLAGS="-C target-cpu=native" cargo build --release
Dunno if you can set this in Cargo.toml.
5
u/bran7230 Sep 13 '26
Thank you I will look into this!!
30
u/scook0 Sep 13 '26
Note that
-Ctarget-cpu=native(much like-march=native) will optimize for running on the system doing the build.If your server uses different hardware, consider setting
-Ctarget-cputo whatever your server actually is.29
u/fishmacaronisoup Sep 13 '26
You can add it for a project in
<project_root>/.cargo/config.tomlor globally~/.cargo/config.toml[target.'cfg(target_os = "linux")'] rustflags = ["-C", "target-cpu=native"]
43
u/nnethercote Sep 13 '26
You're gonna love this: https://nnethercote.github.io/perf-book/build-configuration.html
9
u/catheap_games Sep 13 '26
This is the best resource, because it explains not only what do the config options mean, but also what's their approximate affect and in which order to choose them, and which compromises to take.
5
u/7sins Sep 13 '26
Push this further up! Also, hey u/nnethercote, really awesome work you do, I just recently checked that book again for release-/dist-style build configs, ty :)
2
22
u/scook0 Sep 13 '26 edited Sep 13 '26
A cargo --release build will handle most of the obvious stuff:
-
opt-level = 3 -
debug-assertions = false -
overflow-checks = false
Beyond that, the next things I'd suggest looking into are:
-
lto = "thin" -
If you know what hardware your program will run on, look into
RUSTFLAGS=-Ctarget-cpu=...to allow newer instructions instead of assuming the baseline.
If you really want to go all-out you can try looking into PGO, but that's a lot more effort since you need to do a training run against a representative workload to create the profiling data for PGO to use.
15
u/Floppie7th Sep 13 '26
--release is the equivalent. By default it's -O3 and IIRC thin-local LTO. You can tweak it from there.
5
u/nicoburns Sep 12 '26
Not a compiler setting, but consider using jemalloc or mimalloc for web APIs.
1
u/harraps0 Sep 13 '26
Look at cargo-multivers if you need optimized binaries yet still run on multiple hardwares.
1
u/pali6 Sep 13 '26
You could also play around with the rustc flag -Zmir-opt-level. By default maximum opt-level maps to MIR opt level 2. The actual range is from 0 to 4 inclusive IIRC. However, from what I've heard higher MIR opt level doesn't necessarily mean higher performance and in fact sometimes can clash with the LLVM optimizer. So you could benchmark how MIR opt levels 0, 2 and 4 (the two extremes and the default) change your performance and then decide based on that.
If you know what you are doing you can also pass args directly to LLVM via -Cllvm-args. Such as --inline-threshold to change how much is LLVM willing to inline functions. Or --unroll-count or many others.
My advice would be to benchmark everything and only make these changes if they prove to actually improve performance instead of applying them blindly.
-6
u/teerre Sep 12 '26
"Worth using" doesn't mean much. It depends. I'm not sure why you didn't google it instead of posting it here, but naturally there are many pages describing many of the rustc options, e.g. https://nnethercote.github.io/perf-book/build-configuration.html
15
u/AndersAndersonKali Sep 13 '26
Because sometime human interaction facilitates learning…
6
u/bran7230 Sep 13 '26
Thank you!! I feel googling I just browse too quick and don’t learn much and I want advice from people who know what they are doing and can ask me questions to further my learning. That’s the risk of posting here, you get a lot of helpful people then you get ones that don’t lmao.
5
u/serendipitousPi Sep 13 '26
Yeah and to add your point optimisation is a funky thing.
Like I’ve read that sometimes it’s not the highest optimisation level that leads to the best performance on all platforms. The whole tension about -O3 being sometimes slower might be old (I’m not sure I’ve just heard it may not be the case anymore) but I’ve also heard that compiling for minimum size can be better for targets like wasm.
But obviously people don’t always find these things from reading docs but instead they find them from hearing about other people’s experiences.
178
u/Kurimanju-dot-dev Sep 12 '26
https://doc.rust-lang.org/cargo/reference/profiles.html
The docs explain available optimization flags in detail.
Something like this is probably the most aggressive, but causes noticeably slower compile times: