EMZETT.
Login

L1, L2 and L3 cache

In short: The cache is a very fast buffer in the CPU that holds data and instructions that will be needed soon. There are three levels: L1 (small, fastest), L2 (medium) and L3 (large, shared, slower).

In more detail: Main memory is very slow for a CPU: one access takes as long as hundreds of computation steps. The cache bridges this with the observation that programs need the same data repeatedly (temporal locality) and neighboring data (spatial locality). Several levels are a compromise: the faster a memory, the more expensive and smaller it is.

In Depth

LevelTypical sizeAccess timeBelongs toSpecial
L132 – 64 KB (each data and instructions)~4 cycleseach coreFastest and smallest, split into L1d (data) and L1i (instructions)
L2256 KB – 2 MB~12 cyclesusually each coreBuffer between L1 and L3
L38 – 128 MB (and more, 3D V-Cache)~40 cyclesall cores togetherExchanges data between cores
RAMGB~200+ cyclesexternalfor comparison

How it works:

  • Data is loaded in cache lines of 64 bytes.
  • Hit: The data is there, fast. Miss: Data must be fetched from the next level, the CPU waits.
  • Eviction: When the cache is full, usually the longest unused line is thrown out (LRU-like).
  • Coherence: If one core changes data, other cores must update or discard their copies (protocols such as MESI).

Why several levels?

  • A single large cache would be slow (longer paths, more management).
  • A small, fast L1 catches most accesses. What does not fit lies in L2 and L3.
  • This yields a combination of high speed and enough capacity.

Practice:

  • Programs run faster when they read data in order (arrays) and not in jumps (linked lists, random access).
  • Games and databases benefit strongly from a large L3 (AMD X3D, 3D V-Cache).
  • Cache size is, besides clock and core count, a buying criterion.
  • Attacks such as Spectre and Meltdown exploited timing differences in the cache.

See also: CPU structure: execution units and cache, Memory hierarchy and cache lines, Caches