Cache Lines: The Key to Spatial Locality Gains

Modern CPUs fetch data in fixed-size blocks called cache lines, typically 64 bytes. When a program requests a single byte, the entire cache line containing it is loaded, exploiting spatial locality by bringing nearby data into the fast cache for 'free' subsequent access.

The top 3

  1. Optimal Cache Line Size: Balancing Locality & Waste: Modern CPUs commonly use 64-byte cache lines, like in x86 and most ARM systems, as this size generally balances effective spatial locality exploitation with minimizing data fetched but not used; however, some architectures, such as Apple M-series, employ 128-byte cache lines.
  2. False Sharing: The Cache Line Performance Killer: False sharing occurs when multiple threads modify unrelated data that coincidentally resides on the same cache line, causing constant cache invalidations and reloads, which significantly degrades performance, especially in multi-threaded applications.
  3. Cache Line Prefetching: How CPUs Guess Your Data: Modern CPUs employ sophisticated hardware prefetchers that predict future data access patterns based on current memory access, bringing entire cache lines into the cache proactively to minimize latency for subsequent accesses.

Sources

Open the full topic