CPU Cache Lines and Sequential Reads
CPUs fetch memory in fixed-size cache lines, commonly 64 bytes, so a sequential read uses every fetched byte while a scattered read wastes most of each line and pays the full latency per useful value.
The short answer
A CPU does not fetch individual bytes from memory. It moves data in cache lines, commonly 64 bytes on x86-64 and many ARM64 cores. Reading adjacent bytes reuses the line that is already in cache; reading scattered bytes fetches a new line for each small value, wastes the rest of it, and waits for memory each time. Sequential streams also let hardware prefetchers request lines ahead of the program.
A minimal example
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const size_t n = 4096;
int *a = malloc(n * n * sizeof *a);
if (!a) return 1;
for (size_t i = 0; i < n * n; i++) a[i] = 1;
unsigned long long sum = 0;
for (size_t i = 0; i < n; i++)
for (size_t j = 0; j < n; j++)
sum += a[i * n + j]; /* change to a[j * n + i] */
printf("%llu\n", sum);
return 0;
}
The first loop walks the array in address order. The variant jumps 16 KiB per iteration, so each access lands in a different page and on a different line. Both versions perform the same additions, but the strided one is typically several times slower.
cc -O2 -o stride stride.c
perf stat -e cache-references,cache-misses ./stride
Why this happens
Memory is transferred in bursts, and caches are organized in lines because programs usually exhibit spatial locality. When a load misses, the whole line is fetched from the next level or from DRAM. Subsequent loads to neighboring bytes hit in L1 or L2.
| Level | Approximate latency | Typical size |
|---|---|---|
| L1 data | 4-5 cycles | 32-64 KiB per core |
| L2 | 12-20 cycles | 256 KiB-2 MiB per core |
| L3 | 40-70 cycles | Several MiB, shared |
| DRAM | 80-120 ns | System memory |
At 3 GHz, a nanosecond is about three cycles, so a DRAM miss can cost several hundred cycles. A sequential loop keeps many misses in flight because the addresses are known in advance, and prefetchers extend that window. A pointer chase or a large stride defeats both effects: each access waits for the previous one's result.
Cache line size is implementation-defined on ARM64; most cores use 64 bytes, but Apple Silicon reports 128 bytes, so code that assumes 64 can suffer false sharing on those systems.
What changes the answer
- Working set size matters most. If the data fits in L1 or L2, scattered access can be nearly as cheap as sequential access because every access hits.
- A constant stride may still be prefetched; irregular or data-dependent access usually is not.
- False sharing occurs when two threads write independent variables that share a line. The line ping-pongs between cores even though the variables are unrelated.
- Huge pages reduce TLB misses, which helps large sequential and random workloads, but they do not reduce the number of cache lines fetched.
- NUMA placement, non-temporal stores, and software prefetch instructions change the cost on large machines.
Edge cases
Small benchmarks can be dominated by startup, page faults, or the first pass that warms the cache, so measure a steady state. Compiler vectorization changes how many lines a loop touches per iteration. perf counters are hardware-specific and may be multiplexed; compare runs on the same machine. Memory-mapped device registers are uncached and do not follow these rules.
Practical checks
getconf LEVEL1_DCACHE_LINESIZE reports the line size on glibc systems; lscpu lists cache sizes, and macOS exposes hw.cachelinesize through sysctl. Measure with perf stat -e cache-misses,instructions or a wall-clock benchmark over realistic data sizes, pin the process with taskset when comparing variants, and record the result in a performance baseline. For why a large array can reserve address space without using memory yet, see virtual and physical pages.
Sources
Revision history
- Published
- Updated
- — Initial publication.