Sub topic 3
Spatial and temporal locality
This chapter assumes you know C or a similar language, arrays and structs, and the idea of a cache line (a 64-byte block that moves between memory and cache as one unit). Locality is the property that makes caches work. Code with good locality hits the cache often. Code with poor locality waits on main memory.
Two kinds of locality
Temporal locality means that if you use a piece of data now, you are likely to use it again soon. The cache keeps recently used lines, so the second use is cheap.
Spatial locality means that if you use an address now, you are likely to use nearby addresses soon. A cache line brings in neighbors along with the byte you asked for, so those neighbors are already present when you need them.
Finding locality in code
Read a loop and ask two questions. Which addresses does each iteration touch, and are they close to each other? Does a later iteration touch addresses an earlier one already used?
#include <stddef.h>
long sum_and_scale(const int *a, size_t n, int *out) {
long total = 0;
int factor = 3; /* reused every iteration: temporal */
for (size_t i = 0; i < n; i++) {
total += a[i]; /* a[i], a[i+1] share lines: spatial */
out[i] = a[i] * factor; /* a[i] was just read: temporal */
}
return total;
}In this loop:
totalandfactorare reused on every iteration, so they stay in registers or L1 cache. This is temporal locality.a[i]andout[i]move forward one element at a time. With 4-byteintvalues, one 64-byte line holds 16 elements, so about 15 of every 16 reads are spatial hits.- The second use of
a[i]in the same iteration is temporal locality.
Locality and working set
Temporal locality only helps if the data is still in cache when you come back to it. The set of data a piece of code touches repeatedly is its working set. If the working set fits in a cache level, repeat accesses are fast. If it is larger, lines are evicted before reuse and you lose the benefit.
| Pattern | Spatial locality | Temporal locality |
|---|---|---|
| Sequential scan of a large array, once | High | None |
| Repeated scan of a small array | High | High, if the array fits in cache |
| Repeated scan of an array larger than the cache | High | Lost to eviction |
| Random reads across a large table | Low | Low |
Common mistakes
- Treating "it's an array" as proof of good locality. A loop that jumps by a large stride uses one element per line.
- Assuming reuse is free. A second pass over a huge array reloads from memory because the first pass pushed the early data out.
- Counting only the data you read. Neighboring fields you never use still occupy the line.
Exercise
A loop reads every 16th element of an int array (4-byte ints, 64-byte lines) and the array is far larger than the cache. Which kind of locality does it have, and how many useful bytes does it use per cache line loaded?
Show answer
It has almost no spatial locality, because each access lands in a different line and uses only 4 of the 64 bytes. It has no temporal locality either, because no element is touched twice. Reading every element in order would use all 64 bytes of each line.