Sub topic 17
TLB misses and page-level locality
This chapter assumes you know cache lines, spatial locality, and traversal order from the earlier chapters. You should also know that programs use virtual addresses.
Why translation costs time
Your program uses virtual addresses. The CPU must turn each one into a physical address before it can reach the cache or RAM. Memory is split into pages, usually 4 KiB each. The operating system keeps a page table that maps virtual pages to physical pages.
Walking the page table on every access would be far too slow. So the CPU keeps a small cache of recent translations called the translation lookaside buffer (TLB). A typical first-level data TLB holds only 64 to 100 entries, and a larger second-level TLB holds a few thousand. With 4 KiB pages, 1,500 entries cover only about 6 MiB.
A TLB miss makes the hardware read page table entries from memory. Those reads can themselves miss the cache. A miss can cost tens to hundreds of cycles, even when the data you wanted is already in the cache.
Page-level locality
Cache lines reward accesses that are close together within 64 bytes. The TLB rewards accesses that stay within a small set of pages. The two effects are separate, so a pattern can be kind to one and hostile to the other.
The next function makes a fixed number of reads and changes only the stride.
#include <stddef.h>
/* Makes `count` reads, advancing `stride` bytes each time and wrapping at `size`.
Requires stride < size. */
unsigned long walk(const unsigned char *buf, size_t size,
size_t stride, size_t count) {
unsigned long s = 0;
size_t pos = 0;
for (size_t i = 0; i < count; i++) {
s += buf[pos];
pos += stride;
if (pos >= size)
pos -= size;
}
return s;
}Run it over a 1 GiB buffer with a stride of 64 bytes. Each access uses a new cache line, but 64 accesses share one page, so the TLB almost never misses. Now use a stride of 4160 bytes (4096 + 64). Each access lands on a new page, so the TLB misses on nearly every read. Both strides miss the cache about equally, so the timing gap you measure comes largely from translation.
What to do about it
- Keep hot data in a compact region. Dense arrays touch fewer pages than scattered heap nodes.
- Allocate related objects from the same arena or pool instead of calling the general allocator for each one.
- Avoid page-sized strides in large arrays. Padding a row by one cache line can help both the TLB and cache associativity.
- Use huge pages (2 MiB on x86-64) for large, long-lived buffers. One TLB entry then covers 512 times as much memory. On Linux,
madvise(MADV_HUGEPAGE)requests transparent huge pages.
Exercise
A program loops over 1,000 heap-allocated nodes that were allocated at random times over a long run. Each node is 64 bytes, and the nodes sit on different pages. The whole set is only 64 KB, so it fits in the L2 cache. Why might the loop still be slow, and what would you change?
Show answer
The data fits in cache, but the nodes span up to 1,000 pages, which exceeds the first-level TLB. Many accesses then pay for a TLB miss. Allocate the nodes from one pool or store them in a contiguous array. They then occupy about 16 pages and the translations stay in the TLB.