LOGBOOK

HELP

Quiz Entry - updated: 2026.07.07

What are temporal and spatial locality, and why do caches exploit them?

Temporal locality: recently used data is likely reused soon. Spatial locality: data near a just-used address is likely used next. Caches bet on both.

Two panels. Temporal locality: a loop over i where sum += a[i] reuses the same variable sum every iteration (loop-back arrow keeping sum in a register). Spatial locality: accessing a[0] triggers one cache miss that loads the whole 64-byte line a[0..15], making the next fifteen accesses cache hits.

* Temporal locality reuses the same address soon, while spatial locality gets nearby addresses for free because a whole cache line is loaded at once. *

Examples in code:

// Temporal locality - 'sum' accessed every iteration
for (i = 0; i < N; i++)
    sum += a[i];  // 'sum' is reused many times

// Spatial locality - consecutive array elements
for (i = 0; i < N; i++)
    // Access a[0], a[1], a[2]...
    a[i] = b[i] + c[i];

How caches exploit locality:

  • Temporal: Keep recently accessed data in cache (LRU replacement)
  • Spatial: Load whole cache lines (64 bytes), not single bytes

Cache line effect:

  • You access a[0] (4 bytes)
  • Cache loads a[0] through a[15] (64 bytes / 4 = 16 ints)
  • Next 15 accesses are "free" - already in cache!

Why this matters:

  • Sequential array access: ~1 cache miss per 16 elements
  • Random access: ~1 cache miss per element (16x more misses!)

Tip: "Cache-friendly code" means code that exhibits good locality.

Go deeper:

From Quiz: REVE1 / Overview of Computer Systems | Updated: Jul 07, 2026