Memory and Performance
Rust promises C-level speed without a garbage collector, and most of that promise is kept at compile time. Generic functions turn into separate copies for each type, an iterator chain turns into an ordinary loop, a newtype wrapper takes exactly as much space as its field. Such abstractions are called zero-cost abstractions: they cost no more than code written by hand. But some features do have a price, and it is known in advance: dyn Trait, the Rc and Arc counters, the RefCell check, every heap allocation.
The second half of speed is memory. The compiler reorders a struct's fields on its own to remove empty alignment bytes, and #[repr(C)] restores the declared order. An enum takes the space of its largest variant plus a tag, but the tag often hides in a "niche" — an invalid value such as the null address. That is why Option<&T> takes 8 bytes, like the reference itself.
The third part is measurement. A debug build is several times slower than a release build, and the optimizer throws away a computation whose result nobody reads. An honest measurement is done in a release build, with std::hint::black_box and repetitions. The topic builds on Monomorphization and Stack and Heap and walks through the material layer by layer — from abstractions to measurements. All sizes in the topic were obtained on x86_64.
Topic map
- Zero-Cost Abstractions — iterators, closures, generics and newtypes cost no more than hand-written code in a release build; where a cost appears:
dyn,RcandArc,RefCell, bounds checks, binary size. - Memory Layout — size and alignment,
size_of,align_of,offset_of!; field reordering by default;#[repr(C)]and padding;#[repr(packed)]and E0793; the sizes of enums, fat pointers and zero-sized types. - Niche Optimization —
Option<&T>,Option<Box<T>>,Option<NonZero<u32>>with no extra bytes; what the documentation guarantees and what is compiler behaviour;f64and raw pointers have no niche. - Allocations — what goes to the heap;
with_capacityversus a growing buffer; reusing a buffer withclear();&strinstead ofString; counting allocations with#[global_allocator]. - Benchmarking —
Instantand warm-up;black_boxversus a deleted computation; debug versus release; criterion for statistics; a profiler before optimizing.
Common mistakes and traps
| Mistake | Consequence |
|---|---|
| Measuring speed in a debug build | An iterator chain in debug is about 10 times slower here; conclusions about "slow iterators" are wrong |
Measuring without black_box | The optimizer throws away a computation with an unused result: the measurement shows 0ns |
Relying on field order without #[repr(C)] | The compiler reorders fields; for FFI and binary formats the order is fixed with the attribute |
Taking a reference to a field of a #[repr(packed)] struct | error[E0793]: reference to field of packed struct is unaligned — copy the field into a variable |
Storing an optional id as Option<u32> in a large array | 8 bytes per element instead of 4; Option<NonZero<u32>> takes 4 |
Filling a Vec in a loop without with_capacity when the size is known | 9 trips to the heap for 1000 elements instead of one |
| Optimizing by guesswork | Time goes to the wrong place; first a profiler and a measurement, then the change and a second measurement |
Why it matters in interviews
This is a senior-level topic: the candidate is expected to have a model rather than memorized numbers — where type sizes come from, where Rust pays at run time and how to prove that a change made the code faster. Almost always they ask about the niche optimization of Option and about the cost of abstractions.
What interviewers usually check:
- What "zero-cost abstraction" means and where the promise breaks:
dyn,RcandArc,RefCell, a debug build. - How the compiler lays out a struct, what
#[repr(C)]changes and why#[repr(packed)]is dangerous. - Why
Option<&T>andOption<Box<T>>take as much space as a pointer. - Where a program goes to the heap needlessly and how to fix it.
- How to measure performance properly: release,
black_box, repetitions, criterion. - How to find a slow spot: a profiler and a flamegraph instead of guesses.