Cache Line Fundamentals: CPU's Data Fetching Unit
A cache line is the smallest block of memory a CPU cache loads from RAM, typically 64 bytes on most mainstream CPUs. The CPU fetches an entire cache line even if only a single byte is requested, leveraging spatial locality. Misaligned data, which straddles two cache lines, forces the CPU to perform multiple memory read operations instead of one, significantly degrading performance.
The top 3
- Most Common Cache Line Size: 64 Bytes Dominance: The vast majority of modern CPU architectures, including x86_64 and ARM64, utilize a 64-byte cache line size as the standard unit for data transfer between memory and cache. However, some specialized architectures like Apple's M-series chips and IBM's POWER processors can use larger lines, such as 128 or even 256 bytes.
- Cache Miss Cost: 50x to 80x Slower than Hits: The performance penalty for a cache miss is drastic: an L1 cache hit typically takes about 4 clock cycles, while a miss requiring data from main RAM can incur a cost of over 200 cycles, making it 50 to 80 times slower. This vast difference highlights why optimizing for cache locality can yield 10-100x performance improvements.
- False Sharing: The Cache Line Performance Killer: False sharing is a critical performance issue in multi-threaded programming, occurring when unrelated data accessed by different threads inadvertently resides within the same cache line. Even if threads modify distinct variables, the cache coherency protocol forces constant invalidation and re-fetching of the entire shared cache line, leading to significant slowdowns. Eliminating false sharing, often by padding data structures, can result in order-of-magnitude performance improvements.
Sources
Open the full topic