FerrousPath

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

  1. Decide what matters — latency, throughput, memory, binary size, compile time — and how you will measure it.
  2. Build in release mode. Debug builds can be 10–100× slower and tell you nothing about production. cargo run --release.
  3. Measure a baseline with a repeatable benchmark.
  4. Profile to find where the time goes.
  5. 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_capacity when the size is known; clear() and reuse buffers in loops instead of creating new ones.
  • Take &str / &[T]; return iterators rather than collected Vecs when the caller will just iterate.
  • Avoid .clone() and .to_string() in hot paths; Cow when cloning is only sometimes needed.
  • SmallVec / ArrayVec for 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> over LinkedList<T>; indices into a Vec over Boxed nodes.
  • Smaller is faster. Check std::mem::size_of. Use u32 indices instead of usize or 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 Vec keeps 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-hash or ahash is several times faster. Sorted Vec plus 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 using chunks_exact, zip and windows lets LLVM prove indices in range. Reach for get_unchecked only 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::arch exposes intrinsics behind #[target_feature] with runtime detection (is_x86_feature_detected!); std::simd is the portable API (nightly), and crates such as wide work 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

  1. Write three versions of “count the distinct words in a large text” — HashSet<String>, HashSet<&str>, and sort-then-dedup on Vec<&str> — and benchmark them with Criterion.
  2. Profile your minigrep from Level 2 on a large file with cargo flamegraph. Where does the time go? Remove one allocation per line.
  3. Take a struct with fields (u8, u64, u8, u32, u16). Predict its size with and without #[repr(C)], then check.
  4. Write a dot product over two &[f32] with an indexed loop and with zip. Compare the assembly for bounds checks and vectorisation.

Check your understanding

Pick an answer to see the explanation.

  1. 1.What is the first step in making a program faster?

  2. 2.Why is a Vec<Struct> often faster to scan than a Vec<Box<Struct>>?

  3. 3.Why must a benchmark pass inputs and outputs through std::hint::black_box?

Key terms in this lesson

Finished the lesson and the exercises?