Sub topic 1
Why memory is slower than the CPU: the memory hierarchy
Prerequisites
This chapter assumes you can read basic C, know what arrays and pointers are, and understand Big-O notation. You do not need any hardware background.
The latency gap
A modern CPU can execute several instructions per nanosecond. Main memory (DRAM) takes roughly 60 to 100 nanoseconds to answer a single request. In that time the CPU could have run hundreds of instructions.
This difference is the latency gap. Latency is the time between asking for data and receiving it. A program that waits on memory does no useful work during that time, so for many programs memory access, not instruction count, sets the speed.
Fast memory is small and expensive, and large memory is slow and cheap. Hardware designers cannot build one memory that is both big and fast. Instead, they stack several memories of different sizes.
The levels
Each level keeps copies of data from the level below it. The numbers below are typical orders of magnitude. Your machine will differ.
| Level | Typical size | Typical latency |
|---|---|---|
| Registers | a few hundred bytes | under 1 ns |
| L1 cache | 32 to 64 KB per core | about 1 ns (3 to 5 cycles) |
| L2 cache | 256 KB to 2 MB per core | about 3 to 5 ns |
| L3 cache | several MB to tens of MB, shared | about 10 to 20 ns |
| Main memory (DRAM) | gigabytes | about 60 to 100 ns |
A cache is a small, fast memory that holds copies of recently used data. When the CPU needs a value, it checks L1 first, then L2, then L3, then DRAM. A hit means the data was found at a level. A miss means it was not, and the request goes one level down.
Why the hierarchy works
A hierarchy only helps if most requests are answered by the upper levels. Programs usually behave in a way that makes this true. They tend to reuse data they touched recently, and they tend to touch data near what they just used. The next chapters call these two habits temporal and spatial locality, and they are the basis for everything that follows.
If 95% of accesses hit L1 at 1 ns and 5% go to DRAM at 80 ns, the average cost is about 0.95 × 1 + 0.05 × 80 = 4.95 ns. A 5% miss rate makes the average five times slower than the hit time. Small changes in hit rate have large effects on speed.
Seeing the gap
The following program reads the same number of elements twice. The first pass visits them in order. The second visits them in a shuffled order, so most reads miss the caches.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N (1 << 24)
int main(void) {
int *data = malloc(N * sizeof *data);
size_t *order = malloc(N * sizeof *order);
if (!data || !order) return 1;
for (size_t i = 0; i < N; i++) { data[i] = 1; order[i] = i; }
for (size_t i = N - 1; i > 0; i--) { /* Fisher-Yates shuffle */
size_t j = ((size_t)rand() * 32768u + (size_t)rand()) % (i + 1);
size_t t = order[i]; order[i] = order[j]; order[j] = t;
}
long s = 0;
clock_t t0 = clock();
for (size_t i = 0; i < N; i++) s += data[i];
clock_t t1 = clock();
for (size_t i = 0; i < N; i++) s += data[order[i]];
clock_t t2 = clock();
printf("sequential: %.1f ms\n", 1000.0 * (t1 - t0) / CLOCKS_PER_SEC);
printf("shuffled: %.1f ms\n", 1000.0 * (t2 - t1) / CLOCKS_PER_SEC);
printf("(sum %ld)\n", s);
free(data);
free(order);
return 0;
}Compile with gcc -O2. On most machines the shuffled pass is several times slower, even though both passes do the same arithmetic.
Common mistakes
- Assuming that equal Big-O means equal speed. Two algorithms with the same complexity can differ by a large factor in memory behavior.
- Treating the latency numbers as exact. They vary by CPU model, so measure on your own hardware.
- Forgetting that the compiler may remove work. Using the result (here, printing
s) keeps the loops from being optimized away.
Exercise
A program has a 98% L1 hit rate. L1 takes 1 ns and DRAM takes 80 ns, and all misses go straight to DRAM. What is the average access time?
Show answer
0.98 × 1 + 0.02 × 80 = 0.98 + 1.6 = 2.58 ns. Even a 2% miss rate makes more than half of the average time come from misses.