What is Prefetching?

Loading data into the cache before it is requested because it is likely to be needed soon. Hardware prefetchers detect access patterns, and software prefetching uses explicit instructions.

Field: Memory Hierarchy

Japanese name: プリフェッチ

Why prefetching is needed

An L1 cache hit takes a few cycles, around 1 ns, while a DRAM access takes tens to about 100 ns, so a multi-GHz core can wait hundreds of cycles on a miss; this gap is called the memory wall. Compulsory misses, which occur the first time data is touched, are not reduced by more capacity or associativity because the data was never cached. Prefetching attacks this by requesting lines before the program needs them, overlapping memory latency with useful work. Larger cache lines help in a similar way by bringing in neighboring data with each miss.

How hardware prefetchers work

A hardware prefetcher watches the stream of miss or access addresses and looks for regularity. The simplest fetches the next sequential line; stride prefetchers track the address difference between successive accesses, often per load instruction, and once a constant stride repeats they issue requests several strides ahead. Reading an int array sequentially with 64-byte lines would otherwise miss once per 16 elements, a 6.25% miss rate, and a good prefetcher hides most of those misses. Pointer chasing defeats this: in a linked list the next address is unknown until the current node arrives, so misses serialize.

Software prefetching and prefetch distance

Software prefetching inserts explicit prefetch instructions, either by hand or by the compiler, for patterns hardware cannot predict, such as indirect accesses through an index array. These instructions are hints that typically do not fault or change program results. The key parameter is prefetch distance: fetching too close to the use leaves latency exposed, while fetching too far ahead risks eviction before use. A common rule of thumb is to prefetch ahead by about the miss latency divided by the time per loop iteration, then tune by measurement.

Accuracy, coverage and timeliness

Prefetching is not free. Useless prefetches consume memory bandwidth and can evict useful lines, a problem called cache pollution, which can make a program slower. Prefetchers are therefore judged on accuracy, the fraction of prefetched lines actually used; coverage, the fraction of original misses eliminated; and timeliness, whether data arrives before it is needed. Prefetching also differs from out-of-order execution, which overlaps misses only within its limited instruction window. When a data structure is hostile to prediction, reorganizing it into contiguous arrays often helps more than any prefetcher.

Try it: questions about Prefetching

For which access pattern is hardware prefetching especially effective?

  1. AReading an array in order at a fixed interval (stride)
  2. BFollowing a linked list one element at a time from the head
  3. CLooking up a hash table with random keys
Show answer and explanation

Answer: A. Reading an array in order at a fixed interval (stride)

A hardware prefetcher detects regularity in recent addresses, such as sequential or constant-stride patterns, predicts upcoming addresses and loads them ahead of time. Reading an array at a fixed stride is easy to predict, so the benefit is large. In a linked list the next element's address is unknown until the current one is read, so it is hard to predict from address patterns. Random-key hash lookups also lack address regularity, so prefetching helps little.

FAQ

What is the difference between hardware and software prefetching?

Hardware prefetchers detect patterns like sequential or constant-stride access automatically at run time. Software prefetching relies on explicit instructions placed by the programmer or compiler, which helps with irregular but computable addresses.

Why doesn't prefetching help linked lists much?

Each node's address is only known after the previous node has been loaded, so there is no predictable pattern and misses cannot overlap. Storing the data contiguously or adding jump pointers is usually needed.

Can prefetching make performance worse?

Yes. Inaccurate or overly early prefetches waste bandwidth and evict data that was still needed, so aggressive prefetching can slow memory-bound programs.

Questions that mention Prefetching (8)

Prefetching appears in 9 practice questions across 4 topics, including Memory Hierarchy, DRAM, CPUs and architecture.

  1. In the loop above, an int is 4 bytes, a cache line is 64 bytes, and the array is not cached and starts on a line boundary. Without prefetching, what is the miss rate for reading the array?
  2. What is the performance limitation called that arises because improvements in memory bandwidth and latency have not kept up with improvements in processor performance?
  3. Which correctly lists the "three Cs" classification of cache misses?
  4. Which method is effective at reducing the cache misses that occur when a program accesses data for the first time?
  5. When inserting software prefetch instructions, which statement about how far ahead to fetch (the prefetch distance) is correct?
  6. Roughly how many times longer is a main-memory DRAM access than an L1 cache hit (for typical PC or server CPUs)?
  7. Why does traversing the linked list above tend to be much slower than reading an array with the same number of elements in order?
  8. Measuring per-element time for repeatedly reading an array while gradually growing its size shows time rising in steps at certain sizes. Why?

Books on Memory Hierarchy Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary