Use contiguous memory layouts like Vec<T> and structs with aligned fields to maximize CPU cache hits. Avoid pointer-heavy structures like linked lists or Box<T> chains when processing large datasets sequentially.
// Cache-friendly: contiguous memory
let data: Vec<i32> = (0..1000).collect();
for item in &data {
// Fast sequential access
let _ = item * 2;
}
// Cache-unfriendly: scattered memory
// let list: LinkedList<i32> = ...; // Avoid for hot loops
This approach ensures data is loaded into the CPU cache in large, efficient blocks rather than scattered across memory addresses.