12.2 The Cache Hierarchy and Memory Bandwidth

CPU AND PERFORMANCESTATUS: DEVELOPINGLEVEL: INTROREVIEWED: 2026-10-21

Modern CPUs are faster than memory by orders of magnitude. A core running at 4 GHz retires several instructions per cycle, so an instruction costs it a fraction of a nanosecond. Reading from main memory takes on the order of 100 nanoseconds, which is hundreds of cycles. To bridge this gap, CPUs use a hierarchy of caches. Data lives in several layers, each smaller and faster than the last.

12.2.1 The cache hierarchy and latency

Different levels serve different access patterns. The sizes and latencies below come from Agner Fog’s microarchitecture tables for two recent cores, AMD’s Zen 4 and the performance core of Intel’s Alder Lake:

  • L1 data cache is per core and small: 32 KB on Zen 4, 48 KB on Alder Lake. A hit takes 4 or 5 cycles, about a nanosecond.
  • L2 cache is per core and larger: 1 MB on Zen 4, 1.25 MB on Alder Lake. A hit takes 14 or 15 cycles.
  • L3 cache is shared by a group of cores: 32 MB or more for each group of eight cores on Zen 4, and up to 30 MB for the whole chip on Alder Lake. A hit takes about 47 cycles on Zen 4 and 65 on Alder Lake.
  • Main memory is off-chip and shared by everything, roughly 100 nanoseconds away: several hundred cycles at 4 GHz.

Read those as ratios against L1 and the gap is obvious. Falling through to L2 costs about 3 times an L1 hit, to L3 about 12 times, and all the way to main memory around 100 times. One cache miss can cost more than the arithmetic it was feeding.

Data moves between levels in blocks called cache lines. A cache line is 64 bytes on current x86-64 cores. When you read one byte at address X, the CPU fetches the entire 64-byte cache line containing X into L1. The other 63 bytes of that line come along for free.

12.2.2 Spatial and temporal locality

The performance of memory access depends heavily on locality. Two patterns decide it:

Spatial locality means accessing nearby addresses. If you loop through an array sequentially, each cache miss brings in 64 bytes. The program then reads 63 more bytes for free before the next miss. A loop over an array of 64-byte structures, each starting on a line boundary, has one miss per structure. A loop that jumps randomly through an array much larger than the caches misses on nearly every access.

Temporal locality means reusing recently accessed data. If you read X and immediately read X again, the second read hits L1 cache. If the work between the two reads touches enough other data to fill the cache, X has been evicted and the second read misses. Temporal locality requires working sets to fit in cache.

Code with good locality thrives. A tight loop that processes an array sequentially gets L1 hits after the first access to each cache line. A linked list whose nodes are scattered across memory causes a cache miss on nearly every step, because each pointer leads somewhere uncached. A dense array processed sequentially causes one miss per cache line. A random access to a large array causes one miss per access.

12.2.3 Cache coherence and false sharing

When multiple cores access the same data, cache coherence protocols keep their views consistent. The cost is high. If core A writes to a cache line and core B reads from the same line, the line moves from A to B. If both cores write, the line bounces back and forth. Each handoff is a message between cores and costs far more than an L1 hit.

False sharing happens when two threads write different variables that land on the same cache line by bad luck. Thread A increments counter_A. Thread B increments counter_B. Both variables fit in one cache line. The line bounces between cores even though the threads share no data. Each thread writes only its own variable, but the two variables share a line.

The atomics page covers false sharing and its cost in more detail. Padding variables onto separate cache lines prevents false sharing at the price of memory. Rust’s crossbeam library does this with CachePadded, which aligns a value to 128 bytes on x86-64, two cache lines, because Intel’s prefetcher fetches lines in pairs.

12.2.4 Memory bandwidth saturation

The memory system has a fixed total bandwidth. A desktop with dual-channel DDR5-4800 has a theoretical peak of about 76 GB per second: 4800 million transfers per second, times 8 bytes per transfer, times 2 channels. When all cores stream data at once, they split that total.

Assume a system with 8 cores and 76 GB per second memory bandwidth. Each core gets 76 ÷ 8 = 9.5 GB per second. A tight loop that reads 8 bytes per cycle runs at 3 GHz, reading 24 GB per second per core. Bandwidth saturates just past 3 cores (76 ÷ 24 = 3.2). Cores 4 through 8 compete for the same bandwidth and do not improve throughput.

A tight loop that loads data can saturate bandwidth quickly. If your loop is purely arithmetic, adding more cores helps because each core has its own arithmetic unit. If your loop reads or writes memory, adding cores helps until bandwidth is exhausted.

Bandwidth-bound workloads hit this wall after a few cores on a desktop. Servers have more memory channels and can scale further. But the ceiling exists. Beyond some core count, more cores do not improve throughput.

Measure your bandwidth usage. If you see flat throughput curves beyond a few cores, you are bandwidth-bound. Adding more cores will not help. Changing the algorithm or moving computation elsewhere will.

12.2.5 Worked example: compute-bound versus bandwidth-bound

Assume two algorithms on the same 8-core desktop, with its 76 GB/s of theoretical memory bandwidth for the whole machine.

Compute-bound algorithm: the working set fits in cache, so each core reads about 1 GB/s from main memory.

  • Single core: 1 GB/s throughput.
  • Eight cores: 8 GB/s throughput (linear scaling).

Bandwidth-bound algorithm: reads 10 GB per core per second. Demand climbs with every core you add. Supply does not move.

the ceiling is fixed, the knee is not demand, GB/s 76 GB/s, the whole machine 10 GB/s per core 24 GB/s per core 3.2 cores 7.6 cores 0 76 1 2 3 4 5 6 7 8 cores

Demand grows with each core and supply does not, so each workload stops scaling where its line meets the 76 GB/s ceiling.

The 10 GB/s workload stops scaling between seven and eight cores. Past that point the extra cores contend for a bus that has nothing left to give, and total throughput sits at whatever the memory system can deliver. The tighter loop from earlier on this page, reading 24 GB/s per core, meets the same ceiling at three.

12.2.6 Prefetching and the memory hierarchy

Modern CPUs do not wait passively for cache misses. Hardware prefetchers watch memory access patterns and fetch data before the CPU asks for it. A sequential loop triggers prefetching. The CPU sees that you are reading addresses 0, 64, 128, 192 and automatically prefetches 256, 320, and so on.

A random access pattern does not trigger prefetching. The CPU cannot guess where you will access next. Prefetching hides much of the latency of a sequential scan, and together with spatial locality it is why sequential access runs so much faster than random access.

12.2.7 Measuring cache effects in practice

Profile your code to find cache misses. On Linux, perf stat -d adds L1 data cache and last-level cache counts to its default report. On macOS, Xcode Instruments profiles cache behavior. On Windows, Intel VTune does the same for Intel CPUs and AMD uProf for AMD CPUs.

If your profiler shows a high miss rate, the priority is to improve locality, not to add parallelism.

12.2.8 Failure modes

A fast CPU at high clock frequency can mislead you. A CPU that runs at 4 GHz seems 4 times faster than one at 1 GHz in compute-bound single-threaded tests. But if both are bandwidth-bound on memory-heavy tasks, they achieve nearly the same throughput. Clock frequency does not scale throughput when bandwidth is the limit.

A speedup that keeps growing to 16 workers on a task that should be bandwidth-bound needs explaining. Often each worker’s share of the data has become small enough to fit in its core’s caches, so the workers stop going to main memory: a cache effect, not parallelism. Measure memory bandwidth explicitly. If bandwidth is saturated with 4 workers, the remaining 12 workers are wasted. They compete for the same bandwidth and do not improve throughput.

Comparing speedup on a task with poor cache locality (random access) to one with good cache locality (sequential access) mixes two effects. The sequential version gets L1 cache hits. The random version misses the cache repeatedly. A parallel version with good cache locality runs much faster than a serial version with poor locality. This looks like parallel speedup and is mostly a cache improvement.

A developer optimizes a loop to fit its working set in L3 cache and reports 8x speedup with 8 cores. The speedup is real. But how much is from parallelism and how much is from cache improvement? Measure the two separately.

12.2.9 Practical implications for R and data science

A data.frame and a data.table store each column as one contiguous R vector, so scanning a column is sequential and a row-by-row loop jumps between columns. data.table is not faster because of a different layout. Its speed comes from modifying columns in place instead of copying them, radix-based ordering and grouping (the radix sort in base R’s sort() came from data.table), and multithreaded internals such as fread() and grouped aggregation.

R’s vectorized operations are C loops over contiguous vectors, so they get spatial locality and prefetching. Whether the compiler also turned them into SIMD instructions depends on how R was built; see SIMD, R Vectorization, and Threads. R stores a matrix column by column, so a loop down a column reads memory in order and triggers prefetching, while a loop along a row jumps a whole column length per step.

When building parallel algorithms in R, consider cache behavior. If each worker processes a large contiguous chunk of data, workers have good cache locality. If each worker processes scattered pieces from a large dataset, locality is poor and the workers spend their time waiting on main memory.

Optimize data layout first, then parallelize. A well-laid-out sequential algorithm often beats a poorly-laid-out parallel algorithm.

12.2.10 Parallel systems and the tradeoff

NUMA-aware systems keep data and workers close. Linux’s numactl can bind a process to the cores of one socket and allocate its memory from that socket’s RAM, so its memory accesses stay local and less data crosses between sockets.

Base R’s parallel package can pin forked workers to CPUs, with mcaffinity() and the affinity.list argument of mclapply(), but it has no control over where memory is allocated. On a multi-socket machine, the placement of each worker’s memory is left to the OS.

Further reading

  • Drepper, U. (2007). What Every Programmer Should Know About Memory <https://www.akkadia.org/drepper/cpumemory.pdf>. Section 3 covers caches and Section 5 NUMA.
  • Fog, A. The microarchitecture of Intel, AMD and VIA CPUs <https://www.agner.org/optimize/microarchitecture.pdf>. The source of the cache sizes and latencies above.
  • Norvig, P. Teach Yourself Programming in Ten Years <https://norvig.com/21-days.html>. Its table of approximate timings gives 100 nanoseconds for a fetch from main memory.
  • Intel, Product Specifications <https://www.intel.com/content/www/us/en/ark.html> (formerly ARK), with cache sizes and memory bandwidth for each model.
  • AMD, Processor Specifications <https://www.amd.com/en/products/specifications/processors.html>, with cache configuration for each model.
  • McCalpin, J. D., STREAM benchmark <https://www.cs.virginia.edu/stream/>. The standard way to measure achievable memory bandwidth on a given machine, and the number it reports is usually well below the theoretical peak.
  • Linux perf wiki <https://perf.wiki.kernel.org/> and the perf-stat(1) manual page <https://man7.org/linux/man-pages/man1/perf-stat.1.html>, for counting cache misses.
  • Gregg, B. Linux perf Examples <https://www.brendangregg.com/perf.html>, including hardware cache events.

Where to go next

Back to top