Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 145 of 9362. Instruction Set Architecture and CPU MicroarchitectureFree lesson

Mitigating Cache Miss Penalties with Prefetching

Design hardware prefetchers that predict future memory accesses to hide latency by loading data into the cache before it is requested.

In this lesson, you will learn how hardware prefetchers proactively move data into the cache to hide memory latency and reduce stall cycles.

The Memory Latency Problem

Even with multi-level cache hierarchies, a cache miss forces the CPU to wait for data to be fetched from main memory. Because main memory is significantly slower than the CPU, this creates a "stall" where the pipeline sits idle. While we have optimized our cache structures (like set-associativity) and replacement policies (like LRU), we are still reacting to misses after they occur.

Predictive Prefetching

Hardware prefetching flips the script: instead of waiting for the processor to request data, a dedicated hardware unit observes the pattern of memory accesses and "guesses" what the CPU will need next. If the guess is correct, the data is already sitting in the cache by the time the instruction actually asks for it, turning a potential "miss" into a "hit."

The simplest and most common type is the Next-Line Prefetcher. When the CPU accesses a memory block at address N, the prefetcher automatically fetches block N+1 into the cache. This exploits spatial locality—the tendency of programs to access sequential memory addresses.

Consider this simple loop:

for (int i = 0; i < 1000; i++) {
    sum += array[i]; // Sequential access pattern
}

When the CPU requests array[0], the hardware prefetcher detects the stream and proactively fetches array[1] into the cache. When the loop moves to the next iteration, array[1] is already present, resulting in zero stall time for that access.

Trade-offs

While effective, prefetching isn't free:

  1. Cache Pollution: If the prefetcher guesses incorrectly, it might evict useful data to make room for data that is never used.
  2. Bandwidth Overhead: Prefetching consumes memory bus bandwidth. If the system is already bottlenecked by memory traffic, aggressive prefetching can actually slow down the system by competing with demand-driven fetches.

Modern prefetchers are more complex, tracking "strided" patterns (like accessing every 4th element) to adjust their behavior dynamically based on history.

Key Takeaway: Hardware prefetching improves performance by utilizing idle memory bandwidth to pull anticipated data into the cache, effectively masking latency through speculative execution.