Skip to content
Tutorials Center
My learning

Topics

Lesson 100 of 132 ยท Testing and Tooling

Performance and Optimization

Make Rust fast the right way: release builds, zero-cost iterators, counting allocations, with_capacity, avoiding clones, black_box and profiling.

Rust has a reputation for speed, and it is earned: there is no garbage collector, generics are specialized for every type, and the optimizer sees through iterators and closures. But fast code does not come for free. A program that clones every string, grows a vector one element at a time or runs in a debug build can easily be ten to a hundred times slower than the same idea written with care.

This lesson gives you a practical order of work: build in release mode, measure, remove allocations, then think about data layout. Along the way you will count heap allocations with a few lines of standard-library code, see why iterator chains cost nothing extra, and learn which tools find the slow spot. We never print timings in this tutorial, because a number from a shared server teaches nothing; instead we measure things that are the same on every machine, such as the number of allocations and the size of a type.

Rule one: measure, then optimize

Programmers are bad at guessing where time goes. The workflow that works:

  1. Make it correct and clear first.
  2. Build with --release and measure with a real input.
  3. Find the hot spot with a profiler or a benchmark.
  4. Change one thing, measure again, and keep the change only if it helped.

Most programs spend almost all their time in a tiny fraction of the code. Optimizing the rest only makes it harder to read.

Debug versus release

cargo build makes a debug build: almost no optimization, overflow checks on, debug assertions on, lots of debug information. cargo build --release turns the optimizer on (opt-level = 3), removes the debug-only checks and is routinely 10x to 100x faster for loops, iterators and arithmetic-heavy code. Never judge Rust's speed from a debug build.

Terminal
$ cargo run --release
$ cargo bench            # builds an optimized benchmark profile

Our editor compiles with a little optimization (opt-level=1) but keeps overflow checks and debug assertions on, so it is excellent for checking behaviour and bad for benchmarking. All code below is therefore about how much work the program does, not how long it takes.

Iterators are zero-cost

Beginners often fear that .iter().map(...).filter(...).sum() is slower than a hand-written for loop. In an optimized build it is not: iterator adapters are small generic structs, the compiler inlines every next() call and the result is the same machine code as the loop, often better because it can skip bounds checks and use SIMD instructions. This principle is called a zero-cost abstraction.

Rust Loop and iterator chain do the same work

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Both print 167167000. The iterator version has no index, so the bounds check on values[i] disappears from the source; the loop version relies on the optimizer to remove it. Prefer iterators for clarity (Iterators), and reach for index loops only when the algorithm needs random access.

Iterator adapters are lazy: nothing runs until a consumer such as sum, collect or for pulls items. A common waste is collecting into a temporary vector between steps:

Rust
// Allocates a Vec just to throw it away (wasteful):
let evens: Vec<_> = values.iter().filter(|v| *v % 2 == 0).collect();
let total: u64 = evens.iter().map(|v| *v * *v).sum();

// One pass, no allocation:
let total: u64 = values.iter().filter(|v| *v % 2 == 0).map(|v| v * v).sum();

Counting allocations

The heap is the main cost Rust code adds without telling you. Each String::from, Vec::push that grows, format!, Box::new and .clone() of a heap type calls the allocator, which is much slower than working on the stack. A cheap way to make that visible is a counting allocator: wrap the system allocator and bump an atomic counter on every allocation. This uses only std.

Rust A counting global allocator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Every number the program prints is deterministic. Growing a vector doubles its capacity (4, 8, 16, ... 1024 for u32 items), so nine allocations were needed instead of one (the program prints 9 and 1); each growth also copies the old contents. Vec::with_capacity(n) asks for the space up front. When you know, or can guess, how many items are coming, use it. It also exists for String::with_capacity, HashMap::with_capacity and VecDeque::with_capacity. The collect() method on an iterator with an exact size does this for you automatically, which is one more reason to prefer it over a manual push loop.

Writing a GlobalAlloc is unsafe because you promise to follow the allocator contract. This one only counts and then forwards to the system allocator, so it is safe in practice. Real profiling allocators (dhat, jemalloc with profiling) work the same way.

We will reuse the counting idea in the next examples. To keep the lesson short, each program repeats the same 20 lines of allocator code; in your own project you would put them in a module.

Clones, borrows and Cow

Calling .clone() on a String or a Vec allocates and copies. The usual fix is to borrow (&str, &[T]) or to move the value when you are done with it. When a function only sometimes needs to change its input, Cow<str> ("clone on write") avoids the allocation in the common case, as covered in Cow and Borrow.

Rust Clone, borrow and Cow compared by allocations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Look at the last pair: building a string with out + w + "," in a loop reallocates as it grows (3 allocations for just three words), while join computes the total length first and allocates once. With thousands of words the gap widens. Small algorithmic details like this account for most easy wins.

Data layout: size and padding

How big a type is matters for speed because smaller data fits more items in each cache line. Rust is free to reorder struct fields to reduce padding, unless you ask for #[repr(C)]. Option<Box<T>> and Option<&T> use the null pointer as None, so they cost no extra byte (a "niche" optimization).

Rust Sizes of types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

The numbers above are for a 64-bit platform. The repr(C) struct keeps declaration order and so pays for padding (24 bytes), while the default layout packs the same fields into 16. You rarely need to think about this: Rust already does the sensible thing. But a large enum where one variant is much bigger than the rest makes every value as big as that variant. Box the large variant and the whole type shrinks, which Clippy reports as large_enum_variant. More in Memory Layout.

Choosing the right data structure and algorithm

No micro-optimization beats a better algorithm. A few rules of thumb that regularly matter:

  • Vec first. Contiguous memory is fast to scan. A Vec with linear search often beats a HashMap for under about a dozen items, and a sorted Vec with binary_search is compact and quick.
  • HashMap for lookups by key, BTreeMap when you need order. Call with_capacity if you know the size.
  • Avoid Vec::remove(0) and insert(0, ..) in loops: each is O(n). Use VecDeque for queues, or swap_remove when order does not matter.
  • sort_unstable is usually faster than sort and needs no extra memory; use sort only when equal elements must keep their order. See Sorting and Searching.
  • Build strings with push_str and write! into one String, not with repeated format! and +.
  • Pass references, return owned values only when the caller needs them, and use &[T]/&str in signatures.
  • Prefer u32/usize indices over pointers in graph-like data, and keep hot data separate from cold data.
Rust Queue operations: Vec versus VecDeque by counting moves

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

For six items the total is 5 + 4 + 3 + 2 + 1 + 0 = 15 moves; for a million items it would be about 500 billion. That quadratic blow-up is the kind of problem a profiler finds and micro-tuning cannot fix.

Benchmarking: black_box and criterion

When you do benchmark, the optimizer is your enemy: if a result is never used, it may delete the whole computation, or compute a constant at compile time. std::hint::black_box is an identity function the optimizer cannot see through. Wrap the input (so it cannot be constant-folded) and the output (so it cannot be thrown away):

Rust black_box keeps the computation alive

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

The sum is the same with or without black_box; the function only tells the compiler "assume you do not know this value". Without it, a benchmark of a pure function with a constant input could report zero nanoseconds.

For real measurements use Criterion (a crate; read-only here). It runs the code many times, warms up, removes outliers, reports confidence intervals and compares against the previous run:

TOML
# Cargo.toml (read-only: needs crates.io)
[dev-dependencies]
criterion = "0.5"

[[bench]]
name = "collatz"
harness = false
Rust
// benches/collatz.rs (read-only: needs the criterion crate)
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn bench(c: &mut Criterion) {
    c.bench_function("collatz 27", |b| b.iter(|| collatz_steps(black_box(27))));
}

criterion_group!(benches, bench);
criterion_main!(benches);
Terminal
$ cargo bench

For a whole program, a profiler shows where the time goes. On Linux, perf record plus cargo flamegraph (a Cargo subcommand) produces an SVG where the widest boxes are the hottest functions. On macOS use Instruments; on Windows, the Visual Studio profiler or samply. To get readable names in a release build, enable debug symbols in the profile:

TOML
# Cargo.toml (read-only)
[profile.release]
debug = 1          # line tables for the profiler, no runtime cost

Build settings that help

When the code is right and a profiler says the compiler is the limit, tune the release profile. These options are explained further in Release Builds, Cross-Compilation and Deployment:

TOML
# Cargo.toml (read-only)
[profile.release]
opt-level = 3        # the default for release
lto = "fat"          # optimize across crate boundaries
codegen-units = 1    # slower compile, better optimization
panic = "abort"      # smaller code, no unwinding

Add RUSTFLAGS="-C target-cpu=native" to use every instruction your own CPU has, but the binary then runs only on similar CPUs.

A checklist before you optimize

  • Is it a release build with a realistic input?
  • Did a profiler or benchmark point at this code?
  • Can an allocation be avoided (with_capacity, borrowing, Cow, reuse of a buffer with clear())?
  • Is the algorithm the right one (a map instead of a nested loop, a queue instead of remove(0))?
  • Are there unnecessary intermediate collect() calls?
  • Is it still correct? Tests first, speed second (Testing in Rust).

Reusing a buffer is one of the best optimizations: keep a String or Vec outside the loop, call .clear() at the start of each pass and the capacity from earlier passes is kept, so steady state needs no allocation at all.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why should you not judge Rust's speed with cargo run?

    cargo run builds the dev profile. Optimized code from --release is often 10 to 100 times faster for loops and iterators.
  2. 2. What does it mean that iterators are a zero-cost abstraction?

    Adapters are small generic structs that the optimizer inlines, so a map and filter chain compiles to code like the loop you would have written by hand.
  3. 3. Why call Vec::with_capacity(n) when you know roughly how many items you will add?

    Capacity is not a limit. Reserving it up front replaces several grow-and-copy steps with a single allocation.
  4. 4. What is std::hint::black_box for?

    black_box is an identity function the optimizer must treat as opaque, so benchmarks cannot be optimized away.
  5. 5. A queue is implemented with a Vec and repeated remove(0). What is the better choice?

    Vec::remove(0) shifts every remaining element each time. VecDeque removes from the front without moving the others.

Summary

  • Measure first: always benchmark --release builds with realistic data, and optimize only what a profiler shows is hot.
  • Iterator chains are zero-cost; avoid intermediate collect() calls and use index loops only when needed.
  • Heap allocations dominate many programs. Count them with a small GlobalAlloc wrapper, and cut them with with_capacity, borrowing, Cow and buffer reuse.
  • Type size and layout matter for cache use; Rust reorders fields, uses niches for Option<Box<T>>, and #[repr(C)] keeps declaration order.
  • Pick the right structure: Vec by default, VecDeque for queues, HashMap/BTreeMap for keys, sort_unstable when stability is not needed.
  • Use black_box and Criterion for micro-benchmarks, cargo flamegraph or perf for whole programs, and profile settings (lto, codegen-units, panic = "abort") as a last step.

Next: Release Builds, Cross-Compilation and Deployment