Operating systems / Cache lines and cache misses

Cache lines and cache misses

Why reading 32 numbers from an array can be 14 times faster than reading them from a linked list, and how two cores can slow each other down without sharing any data.

Intermediate7 steps~7 min
Core — runs the program, asks for memory
Cache — small, fast copy of recent lines
RAM — main memory: large and slow to reach
Miss — data not in the cache: fetch its line
// THE SHORT VERSION

In 30 seconds

Main memory is slow compared with the processor, so each core keeps a small, fast cache of recently used memory. The cache fetches memory in fixed 64-byte blocks called cache lines.

Asking for one value brings its neighbours along. Code that walks through neighbouring data, like an array, mostly finds what it needs already loaded. Code that jumps around memory, like a scattered linked list, waits on almost every step.

Lines also matter between cores: two cores writing to different variables on the same line keep stealing it from each other.

Cache lines and cache misses, step by step

STEPS01 / 07

Simplified: one cache level, round timings and no prefetcher. Real chips have L1, L2 and L3 caches and guess ahead on array walks. 64-byte lines are typical; some chips, such as Apple’s M series, use 128.

STEP 01 / 07 RAM is far away
THE MATH
cache hit: about 1 ns RAM trip: about 100 ns
  1. Step 1: RAM is far away. A processor core keeps a small, very fast store of recently used memory right beside it: the cache. Data already there arrives in about a nanosecond. Data that has to come from main memory (RAM) takes about a hundred times longer, so programs run fast when what they need is already in the cache.
  2. Step 2: Memory moves in lines. The cache does not fetch single bytes. Memory is divided into fixed 64-byte blocks called cache lines, and a whole line is copied at once. When the CPU asks for a[2] and it is not in the cache (a miss), the line holding it comes over, bringing a[0] to a[15] along.
  3. Step 3: An array: 1 miss, 15 hits. Summing an array of 32 ints reads them in address order. The first read of each line misses and pulls in the line; the next 15 reads find their data already waiting. Using data that sits next to data you just used is called spatial locality, and arrays have it built in.
  4. Step 4: A linked list: every hop. A linked list stores each value in its own node, and each node holds the address of the next. Nodes created at different times tend to end up scattered across memory. If each of the 32 nodes sits on a different line, every step to the next node is a miss: nothing useful rode along with the previous one.
  5. Step 5: Same data, different time. Price each miss at 100 ns and each hit at 1 ns. The array walk costs 230 ns, the list walk 3200 ns: about 14 times slower for the same 32 numbers and the same number of steps. Layout in memory, not the amount of work, makes the difference.
  6. Step 6: False sharing. Each core has its own cache. Here core A only counts in a and core B only counts in b, but the two 8-byte counters sit on the same line. A core must own the whole line to write to it, so each write throws out the other core’s copy, and the line bounces back and forth. The cores share no data, yet slow each other down.
  7. Step 7: Fix: one line each. The cure is to move the counters onto different lines, usually by padding or aligning each one to 64 bytes. Now b starts at 0x2040, on line 129. Each core keeps its own line in its cache and writes without disturbing the other, at the cost of 56 unused bytes per counter.
VALUES SO FAR
hitabout 1 nsmodel
missabout 100 nsmodel
line····rule
per line····result
array····result
list····result
array····time
list····time
a and b····shared

Spotted a mistake on this page?