Level 3 · Systems· Lesson 9 of 11 · 30 min
Performance engineering
Measure first — benchmarking, profiling, allocation, data layout, bounds checks, inlining, SIMD, and build configuration.
The method
- Decide what matters — latency, throughput, memory, binary size, compile time — and how you will measure it.
- Build in release mode. Debug builds can be 10–100× slower and tell you nothing about production.
cargo run --release. - Measure a baseline with a repeatable benchmark.
- Profile to find where the time goes.
- Change one thing. Measure again. Keep it only if it helped.
Rust is fast by default; most slow Rust programs are slow for algorithmic reasons or because of accidental allocation and copying, not because of the language.
Benchmarking
Use Criterion for statistically meaningful micro-benchmarks, and hyperfine to time whole commands.
// benches/sum.rs — with `criterion` as a dev-dependency and `harness = false`
use criterion::{criterion_group, criterion_main, Criterion};
use std::hint::black_box;
fn sum_loop(xs: &[u64]) -> u64 {
let mut total = 0;
for i in 0..xs.len() {
total += xs[i];
}
total
}
fn sum_iter(xs: &[u64]) -> u64 {
xs.iter().sum()
}
fn bench(c: &mut Criterion) {
let data: Vec<u64> = (0..100_000).collect();
c.bench_function("indexed loop", |b| b.iter(|| sum_loop(black_box(&data))));
c.bench_function("iterator", |b| b.iter(|| sum_iter(black_box(&data))));
}
criterion_group!(benches, bench);
criterion_main!(benches);
black_box hides a value from the optimiser so the work is not folded away.
Profiling
| Tool | Platform | Shows |
|---|---|---|
cargo flamegraph |
Linux, macOS, Windows | CPU time as a flame graph |
samply |
all | Sampling profiler with the Firefox Profiler UI |
perf |
Linux | Sampling, hardware counters (cache misses, branch mispredictions) |
| Instruments, VTune, Windows Performance Analyzer | per-OS | Vendor profilers |
dhat, heaptrack, bytehound |
various | Allocation counts, sizes, and sites |
cargo bloat, cargo llvm-lines |
all | What takes space in the binary; generic bloat |
cargo build --timings |
all | Where compile time goes |
Profile with optimisations and debug info so you get symbols:
[profile.release]
debug = true
Allocation
The heap is the usual first suspect.
use std::time::Instant;
fn main() {
let n = 200_000;
let t = Instant::now();
let mut slow = Vec::new();
for i in 0..n {
slow.push(format!("item-{i}")); // one allocation per element, plus regrowth
}
let a = t.elapsed();
let t = Instant::now();
let mut buf = String::with_capacity(n * 12); // one allocation, up front
let mut spans = Vec::with_capacity(n);
for i in 0..n {
use std::fmt::Write;
let start = buf.len();
write!(buf, "item-{i}").unwrap();
spans.push(start..buf.len()); // indices into one shared buffer
}
let b = t.elapsed();
println!("{} strings: {a:?}", slow.len());
println!("one buffer + {} spans: {b:?}", spans.len());
println!("check: {} == {}", slow[77], &buf[spans[77].clone()]);
}
Habits that remove allocations:
with_capacitywhen the size is known;clear()and reuse buffers in loops instead of creating new ones.- Take
&str/&[T]; return iterators rather than collectedVecs when the caller will just iterate. - Avoid
.clone()and.to_string()in hot paths;Cowwhen cloning is only sometimes needed. SmallVec/ArrayVecfor collections that are usually tiny;Box<str>/Arc<str>for immutable strings; arenas (bumpalo) for many short-lived objects.- A different global allocator (
mimalloc,jemalloc) can help allocation-heavy multi-threaded programs.
Data layout
Memory, not arithmetic, is usually the bottleneck. A cache miss costs on the order of a hundred simple instructions.
- Contiguous beats linked.
Vec<T>overLinkedList<T>; indices into aVecoverBoxed nodes. - Smaller is faster. Check
std::mem::size_of. Useu32indices instead ofusizeor pointers where ranges allow; box the large, rare variant of an enum so the common ones stay small. - Struct of arrays. If a loop touches one field of many records, storing each field in its own
Veckeeps the cache full of useful data. - Hash maps. The default hasher (SipHash) resists collision attacks and is slow for small keys. When keys are not attacker-controlled,
rustc-hashorahashis several times faster. SortedVecplus binary search often beats a map for small, read-mostly sets.
use std::mem::size_of;
enum Wasteful { Small(u8), Huge([u64; 32]) }
enum Lean { Small(u8), Huge(Box<[u64; 32]>) }
fn main() {
println!("Wasteful: {} bytes per value", size_of::<Wasteful>());
println!("Lean: {} bytes per value", size_of::<Lean>());
println!("Option<Box<u8>>: {} (niche optimisation: no extra tag)", size_of::<Option<Box<u8>>>());
}
Helping the optimiser
- Bounds checks. Iterators have none. For indexed loops, slicing once to a known length (
let xs = &xs[..n];) or usingchunks_exact,zipandwindowslets LLVM prove indices in range. Reach forget_uncheckedonly with a profile in hand. - Inlining. Small functions are inlined automatically within a crate. Across crates, generic functions are always candidates; non-generic ones need
#[inline]or LTO.#[inline(never)]and#[cold]on error paths keep hot code compact. - Monomorphization versus dispatch. Generics are fastest but duplicate code; a thin generic wrapper around a non-generic inner function gives ergonomics without bloat.
- Auto-vectorisation. Simple loops over slices with no early exits and no bounds checks vectorise. Inspect the result on Compiler Explorer or with
cargo asm. - Explicit SIMD.
std::archexposes intrinsics behind#[target_feature]with runtime detection (is_x86_feature_detected!);std::simdis the portable API (nightly), and crates such aswidework on stable.
Build configuration
[profile.release]
opt-level = 3 # "s" or "z" to optimise for size
lto = "fat" # whole-program optimisation; "thin" is nearly as good and faster to build
codegen-units = 1 # better optimisation, slower compile
panic = "abort" # smaller binary, no unwinding tables
strip = true
Setting RUSTFLAGS="-C target-cpu=native" lets the compiler use every instruction your machine has — at the price of a binary that may not run elsewhere. Profile-guided optimisation (cargo pgo) feeds real execution profiles back into the compiler and is commonly worth 10–20% on large programs.
Exercises
- Write three versions of “count the distinct words in a large text” —
HashSet<String>,HashSet<&str>, and sort-then-dedup onVec<&str>— and benchmark them with Criterion. - Profile your
minigrepfrom Level 2 on a large file withcargo flamegraph. Where does the time go? Remove one allocation per line. - Take a struct with fields
(u8, u64, u8, u32, u16). Predict its size with and without#[repr(C)], then check. - Write a dot product over two
&[f32]with an indexed loop and withzip. Compare the assembly for bounds checks and vectorisation.
Check your understanding
Pick an answer to see the explanation.
1.What is the first step in making a program faster?
2.Why is a
Vec<Struct>often faster to scan than aVec<Box<Struct>>?3.Why must a benchmark pass inputs and outputs through
std::hint::black_box?