Course contentsShow
← Rust
Lesson 273 of 1,9103. Borrowing and ReferencesFree lesson

Memory Layout and Reference Indirection

Visualize how reference indirection affects CPU cache efficiency.

In this lesson, you will learn how reference indirection influences CPU cache efficiency by changing how your data is physically arranged in memory.

The "Cache Miss" Problem

To understand why references matter for performance, imagine your CPU is a chef and your RAM is a warehouse miles away. Because the warehouse is slow, the CPU has a small, ultra-fast "pantry" called the CPU Cache. When the CPU needs data, it doesn't just grab one byte; it grabs a "cache line" (a chunk of contiguous memory) to keep nearby for future use.

When you have a flat array of data—like [i32; 1024]—the numbers are packed side-by-side. The CPU loads a chunk of these into its pantry in one go. This is extremely efficient.

The Cost of Indirection

However, when you use references—like a Vec<&i32>—the layout changes dramatically. You are no longer storing the numbers directly in the vector. Instead, the vector contains pointers (memory addresses) that point to integers scattered elsewhere in memory.

// Contiguous: Data is packed, easy for the CPU to pre-fetch.
let data: Vec<i32> = vec![1, 2, 3, 4]; 

// Indirect: The Vec holds pointers. The CPU must visit the Vec,
// get an address, and then "jump" to a completely different 
// memory location to fetch the actual value.
let indirect: Vec<&i32> = vec![&1, &2, &3, &4];

When the CPU tries to iterate through indirect, it loads the pointer from the vector, realizes it doesn't have the target data in its pantry, and must pause to fetch the data from the distant warehouse. This is called a cache miss. If you do this repeatedly, your program spends more time waiting for memory to arrive than actually performing calculations.

Why It Matters

While references are vital for memory safety and avoiding copies, every layer of indirection is a potential "jump" that breaks the CPU's ability to predict and pre-fetch your data. When designing performance-critical structures, favoring contiguous data layouts over pointer-heavy structures allows the CPU to keep your data "in the pantry," keeping your application running at peak speed.

Key Takeaway: Reference indirection forces the CPU to jump across memory, which prevents efficient caching and degrades performance compared to storing data contiguously.