To compile your Rust library for best performance, follow these steps, combining Rust's built-in optimization features and best practices:
1. Use Release Mode
Always compile with --release to enable optimizations:
cargo build --release
This activates the [profile.release] settings in Cargo.toml, which defaults to -O3-level optimizations.
2. Tune Cargo.toml for Performance
Customize the [profile.release] section in your Cargo.toml:
[profile.release]
opt-level = 3 # Aggressive optimizations (default)
lto = true # Link-Time Optimization (cross-crate inlining)
codegen-units = 1 # Optimize whole crate (slower compile, better perf)
panic = "abort" # Disable unwind info (smaller binaries)
incremental = false # Disable incremental compilation for better opt
3. Target Specific CPU Features
Use RUSTFLAGS to enable CPU-specific optimizations:
RUSTFLAGS="-C target-cpu=native" cargo build --release
This enables SIMD and other instructions specific to your machine. For portability, replace native with a specific target (e.g., skylake, apple-m1).
4. Optimize Dependencies
Ensure all dependencies are built with optimizations. Add this to .cargo/config.toml:
[profile.release]
codegen-units = 1
lto = "thin" # Faster than full LTO, good for dependencies
5. Profile-Guided Optimization (PGO)
For advanced optimization:
- Compile with instrumentation:
RUSTFLAGS="-C instrument-coverage" cargo build --release
- Run benchmarks/test workloads to generate profiles.
- Rebuild with profile data:
RUSTFLAGS="-C profile-use=PATH_TO_PROF_DATA" cargo build --release
See Rust's PGO docs for details.
6. Use a Better Memory Allocator
Replace the default allocator for better performance (e.g., mimalloc, jemalloc):
// In your lib.rs or main.rs
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
Add mimalloc to Cargo.toml:
[dependencies]
mimalloc = { version = "0.1.28", features = ["serde"] }
7. Enable SIMD (if applicable)
Use crates like packed_simd or enable target-specific SIMD features:
RUSTFLAGS="-C target-feature=+avx2,+fma" cargo build --release
8. Strip Debug Symbols
Reduce binary size and cache pressure:
strip target/release/libyour_library.so # On Linux/macOS
Or configure strip in Cargo.toml:
[profile.release]
strip = "symbols" # Remove debug symbols
9. Benchmark and Iterate
Use criterion.rs to measure performance:
// benches/benchmark.rs
use criterion::{criterion_group, criterion_main, Criterion};
fn bench_my_func(c: &mut Criterion) {
c.bench_function("my_func", |b| b.iter(|| my_crate::my_func()));
}
criterion_group!(benches, bench_my_func);
criterion_main!(benches);
Run benchmarks:
cargo bench
10. Use Latest Rust Version
Update to the latest stable compiler for performance improvements:
rustup update
Summary of Critical Flags
RUSTFLAGS="-C target-cpu=native -C lto -C codegen-units=1 -C opt-level=3 -C panic=abort" \
cargo build --release
Trade-offs
- Compile Time: Aggressive optimizations (
lto, codegen-units=1) increase build time.
- Portability:
target-cpu=native produces non-portable binaries.
- Safety: Avoid
unsafe unless necessary; Rust's safety guarantees matter more than micro-optimizations.
By combining these steps, you'll extract maximum performance from your Rust library! 🚀