core23 min studyLesson 116 in Engineering

Prefetching, timeliness, and wasted work

Reason about bringing data early without confusing hidden latency with reduced traffic.

On this page

What you’ll understand

  • Distinguish accuracy, coverage, and timeliness in prefetch reasoning.
  • Estimate a lead distance using an explicitly steady consumption model.
  • Explain why pointer dependence and extra traffic can defeat a prefetch plan.

A cache miss is expensive when useful execution must wait for its data. A prefetch mechanism tries to request a likely future line before the demand instruction needs it. Arriving early enough can hide part of the wait, but does not make the transfer free. Predicting the wrong line, requesting too late, or evicting useful data too early can spend bandwidth and capacity without shortening the critical path.

Prediction and completion are different questions

Hardware prefetchers infer useful patterns from observed accesses, using implementation-specific logic. A sequential stream is often easier to anticipate than a chain whose next address is stored in the current node. Software prefetch instructions give another possible hint, but their semantics and usefulness depend on the target architecture. They generally do not replace correct address ownership or synchronization. A hint must not become an excuse to access objects outside their valid lifetime or ignore the language's rules.

Accuracy asks what fraction of issued prefetches later prove useful under a declared accounting rule. Coverage asks how many relevant misses the mechanism avoids relative to a baseline. Timeliness asks whether requested data arrives before demand without being evicted too soon. These dimensions differ: a perfectly accurate request issued after the demand cannot hide its latency, and an extremely early request may displace something more valuable. Define the denominator before comparing reported prefetch percentages.

dlead≈⌈τc⌉d_{\rm lead}\approx\left\lceil\frac{\tau}{c}\right\rceil
In a steady illustrative stream, τ is transfer latency in cycles and c is cycles of useful processing per consumed line. The estimated lead d is a number of lines. It assumes enough independent work and request capacity, stable latency, and no intervening eviction.↔ Scroll wide equations horizontally.

Latency hiding consumes resources

A streaming kernel may overlap several outstanding fills while arithmetic continues on earlier data. Its achievable overlap is limited by dependency chains, request-tracking entries, cache capacity, and memory bandwidth. If an address cannot be computed until a previous miss returns, simply asking for more simultaneous requests does not create independence. Reorganizing data or processing several independent chains can change the dependency structure, but may add bookkeeping and alter the working set.

Useless prefetches still compete for some combination of link bandwidth, fill buffers, and cache space. A workload already saturating memory bandwidth may gain little from fetching more aggressively. A useful benchmark therefore reports elapsed time and relevant traffic, not just fewer demand misses. Demand misses can decrease because prefetching brought lines early while total bytes increased enough to slow the program. Consider co-running workloads as well as an otherwise idle machine.

Buseful≤αBtotal,0≤α≤1B_{\rm useful}\leq\alpha B_{\rm total},\qquad 0\leq\alpha\leq1
α is the fraction of transferred bytes useful to the application under a stated accounting boundary. Bandwidths use bytes per second. This accounting bound does not claim that every unused byte came from prefetching or that a particular prefetcher has fixed accuracy.↔ Scroll wide equations horizontally.

A disciplined comparison changes one mechanism

First establish a representative access pattern and an observable result that prevents dead-code elimination. Compare warm and cold conditions deliberately, and record working-set size and placement. If supported controls let you change a prefetch mechanism, document exactly which one changed and repeat the measurement; a global switch may affect multiple streams. Large changes in instruction count, layout, or thread scheduling make a simple before-and-after claim harder to interpret.

The queue lab models fluid backlog from an initial four jobs using arrival rate λ and service rate μ. Reset, set λ=4 and μ=2, predict growth, then Play or Step. For a separate comparison, Reset again, re-set both rates to λ=2 and μ=4, predict the decline from four, then Play or Step. Control edits recompute the whole trajectory at the current elapsed time; they do not switch rates within a retained backlog history. Use this only for the abstract lesson that submitting work faster than service capacity creates pressure. It has no cache, request buffers, address prediction, or dependent instructions. Its separate M/M/1 readout adds stochastic assumptions and is not a CPU miss-latency formula.

How far ahead is early enough?

A hypothetical loop consumes one line every 12 cycles after warmup. A useful prefetch takes 180 cycles to complete. Assume independent addresses, stable latency, sufficient tracking capacity, and no eviction before use.

  1. Divide the assumed latency by the processing interval: 180/12=15 lines. A lead of about 15 lines is the earliest integer estimate that covers the latency in this steady model.

  2. If the loop instead consumes one line every six cycles, the estimate doubles to 30 lines. Faster processing can require more outstanding requests rather than less memory-system capacity.

  3. For declared 64-byte lines, 15 lines represent 960 bytes of future line data and 30 represent 1,920 bytes. These payload counts omit request metadata and do not guarantee the hardware can sustain either lead or the required bandwidth.

The result

A lead-distance estimate combines consumption rate and latency. It is a hypothesis to test, not an architecture-independent tuning constant.

Explore the model

Interactive lab

Interactive lab · queue

Submission pressure is not hidden latency

For each separate run, Reset, re-set both arrival rate λ and service rate μ, predict growth or decline from four jobs, then Play or Step. Rate edits recompute the trajectory at the current elapsed time rather than preserving prior rate history. This illustrates generic rate imbalance, not a CPU prefetcher, cache misses, or hardware request buffers.

Submission pressure is not hidden latencyFluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Current readings: Fluid backlog: 4; Net rate · λ − μ: −1 /s; M/M/1 mean residence: 1 s. All parameters are adjustable with the labeled controls below.02.557.51001.534.56Elapsed time · sFluid backlogλ < μ
Fluid backlogCurrent time
Fluid backlog
4
Net rate · λ − μ
−1 /s
M/M/1 mean residence
1 s
t = 0 s

Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forExtra speculative requests still need service capacity, and this rate model cannot decide whether an address prediction is useful.

Check your understanding

Demand cache misses fall after a prefetch change, but elapsed time rises. Which explanation is plausible?

Try it yourself

A proposed mechanism transfers 10 GB during a run, of which 7 GB is useful under the selected accounting rule. Total transfer bandwidth is 50 GB/s. Find the useful-byte fraction and the corresponding useful bandwidth ceiling.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Keep decimal GB units consistent and multiply total bandwidth by the useful fraction.

Compare with the solution

The useful fraction is 7/10=0.7, so useful bandwidth is at most 0.7×50=35 GB/s under this accounting. That does not reveal how many misses were hidden or whether execution sped up. A second implementation moving fewer total bytes at the same useful rate could still have a different latency profile or compute bottleneck.

Take it with you

  • A prefetch must be useful, timely, and affordable.
  • Independent work is needed to overlap latency; prediction cannot remove dependencies.
  • Demand misses and total traffic can move in opposite directions.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.