Course contentsShow
← Rust
Lesson 941 of 1,9108. Collections and Iterator PipelinesFree lesson

Stack Allocation vs Heap Persistence

You will be able to determine when to use SmallVec or Stack-allocated arrays to avoid dynamic allocation overhead.

In this lesson, you will learn how to bypass the performance cost of dynamic memory allocation by strategically choosing stack-based structures for predictable, small-scale data.

The Cost of the Heap

When you use a Vec<T>, Rust allocates memory on the heap. While flexible, this comes with two hidden costs: the overhead of the system allocator and the loss of cache locality. Every time you push an element that exceeds the current capacity, the vector must request a new, larger memory block and copy all existing elements over. For small collections, this "Heap Persistence" is often overkill.

Stack-Allocated Arrays

If you know the maximum size of your collection at compile time, the stack is your best friend. Storing data directly on the stack allows for instant allocation (just moving the stack pointer) and superior CPU cache performance because the data is guaranteed to be contiguous and local to your current frame.

// Use a fixed-size array when size is known
let buffer: [u8; 16] = [0; 16]; 

The SmallVec Pattern

Sometimes, your data size varies, but it is usually small. This is where the SmallVec pattern (often implemented via the smallvec crate) shines. A SmallVec<[T; N]> acts as a hybrid: it stores the first N elements inline on the stack. If you exceed N elements, it seamlessly spills the data onto the heap.

This approach is highly effective for high-performance pipelines where 99% of your operations involve 4 or fewer items. You eliminate the allocation for the common case while maintaining the safety of a dynamic container for the rare "overflow" cases.

use smallvec::SmallVec;

// Stays on the stack until we push more than 4 elements
let mut stack_or_heap: SmallVec<[i32; 4]> = SmallVec::new();
stack_or_heap.push(1);

By keeping data on the stack whenever possible, you minimize the pressure on the global memory allocator and ensure your data is "hot" in the CPU cache, leading to significantly faster iteration speeds.

Key Takeaway: Prefer stack-allocated arrays or SmallVec containers for small, transient data to avoid the latency of heap allocation and improve memory locality.