Worst 3D Array Access: Striding Against Layout

Accessing a 3D array in an order that conflicts with its memory layout (e.g., column-major traversal in a row-major stored array) leads to severe cache misses, as only one element per cache line is used before fetching a new one.

The top 3

  1. The Triple Threat: Worst 3D Array Access Orders: In row-major languages like C/C++, iterating the first index slowest (e.g., array[i][j][k] where 'i' changes last) or the last index fastest (e.g., array[i][j][k] where 'k' changes fastest) when the data is stored in a way that doesn't match the access pattern, leads to the most cache misses. Fortran's column-major storage requires the opposite access order for optimal performance.
  2. Cache Line Eviction: The Core Problem: Worst-case striding patterns cause rapid and frequent cache line evictions, forcing the CPU to constantly fetch new 64-byte cache lines from much slower main memory, significantly increasing memory access latency by 10-100x compared to sequential access.
  3. L1 vs. L3: Different Cache Impact: While all cache levels suffer, the L1 cache, being the smallest and fastest (typically 16KB-128KB per core with 1-4 cycles latency), experiences the most damaging thrashing from poor access patterns, often nullifying its performance benefits entirely and escalating lookups to slower L2 and L3 caches.

Sources

Open the full topic