Architecture: caches, branch prediction and SIMD
SYS · Chapter 112 min readAsked at Hudson River Trading, Jump, Tower, Citadel Securities
Assumes Latency, the memory hierarchy and why the tail is the number.
After this lesson you should be able to
- Explain the memory hierarchy and what a cache line is.
- Say why a mispredicted branch is expensive.
- Describe what SIMD buys and what prevents it.
Modern processors are fast at arithmetic and slow at waiting for memory, and nearly every performance question at a low-latency firm reduces to that asymmetry. Writing fast code means arranging data so the processor is never waiting and never guessing wrong.
| Level | Latency | Size |
|---|---|---|
| Register | 0 cycles | A few hundred bytes |
| L1 | ~4 cycles, ~1 ns | 32–48 KB per core |
| L2 | ~12 cycles, ~4 ns | 0.5–2 MB per core |
| L3 | ~40 cycles, ~15 ns | Tens of MB, shared |
| Main memory | ~200–300 cycles, ~80–100 ns | Gigabytes |
Proposition 1.3
The cache line is the unit
Memory moves in 64-byte lines, not in bytes. Reading one integer pulls in its fifteen neighbours, so a sequential walk pays one miss per sixteen elements and a random walk pays one per element — a sixteenfold difference in memory traffic from the access pattern alone.
Holds when
- Sequential access is also prefetched: the hardware notices the pattern and fetches ahead.
- Struct-of-arrays beats array-of-structs when you touch one field at a time, because the unused fields no longer occupy the line.
- False sharing is the pathological case: two threads writing different variables on one line invalidate each other’s copies.
Why a mispredicted branch costs so much. A modern pipeline has fifteen to twenty stages, and it does not wait to discover which way a branch goes — it predicts and speculates ahead. When the prediction is right the branch is effectively free. When it is wrong, everything speculatively executed is discarded and the pipeline refills from scratch, costing roughly the pipeline depth. Predictable branches are therefore nearly free and unpredictable ones cost fifteen to twenty cycles, which is why a data-dependent branch on random input can be slower than doing the work unconditionally.
Example 1.4
A loop sums the elements of an array that exceed a threshold. Why is it much faster on sorted data?
Show the worked solutionHide the worked solution
Worked solution
- Formula
- Substitute
- Solve
- Answer
Sanity check. Nothing about the arithmetic changed — the same comparisons and additions run in the same order. The entire difference is that the predictor can learn a sorted pattern and cannot learn a random one, which is why this is the canonical demonstration that the processor, not the algorithm, decides.
The rest of this lesson is in Premium
You have read the opening. 12 more sections follow, including 4 worked examples and 3 quick checks.
Nothing is charged for 7 days, and you can cancel before then. Or read Complexity: reading it off, and deriving it in full, free.