Skip to content
QuantMax
QuantMax
  • Overview
  • Curriculum
    • FLUMental maths and numerical fluency
    • MKTMarkets and products
    • CSData structures and algorithms
    • PYPython and data for quants
    • NUMNumerical methods
    • SYSSystems and low latency
      • 1Computer architecture

        • Architecture: caches, branch prediction and SIMD
      • 2C++ for trading

        • C++ for trading: RAII, moves and what belongs on the hot path
      • 3Concurrency

        • Concurrency: atomics, memory ordering and lock-free queues
      • 4Networking

        • Networking: multicast market data, kernel bypass and gap recovery
      • 5Measurement

        • Latency, the memory hierarchy and why the tail is the number
      • 6Order book engineering

        • Order book engineering: data structures and the operations they serve

Practise

  • Question bank
  • Mental arithmetic
  • Market simulator
  • Arbitrage trees
  • Horse racing
  • Bid book
  • Screening tests
  • Mock papers

Reference

  • Formula reference
  • Search

Your record

  • Review queue
  • Progress
  • Leaderboard
  • Profile
  • Invite friends
AccountSend feedback
  1. Curriculum
  2. /Quantitative development
  3. /Systems and low latency
  4. /Computer architecture

Architecture: caches, branch prediction and SIMD

SYS · Chapter 1·12 min read·Asked 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.

LevelLatencySize
Register0 cyclesA few hundred bytes
L1~4 cycles, ~1 ns32–48 KB per core
L2~12 cycles, ~4 ns0.5–2 MB per core
L3~40 cycles, ~15 nsTens of MB, shared
Main memory~200–300 cycles, ~80–100 nsGigabytes
Table 1.1 · The memory hierarchy. A cache miss to main memory costs roughly as much as a hundred arithmetic operations. That ratio, more than any instruction count, is what determines the speed of real code.
L1 cache1L2 cache4L3 cache12Main memory100
Figure 1.2 · The memory hierarchy, in nanoseconds. Typical figures on a modern server core. A hundred to one between L1 and DRAM is why layout beats instruction count: a loop over a contiguous array and a loop over the same data behind pointers execute the same operations and differ by two orders of magnitude.

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

  1. Formula
    cost≈n×(work+P(mispredict)×15)\text{cost} \approx n \times \left(\text{work} + P(\text{mispredict}) \times 15\right)cost≈n×(work+P(mispredict)×15)
  2. Substitute
    sorted: the branch goes one way then the other, so it predicts\text{sorted: the branch goes one way then the other, so it predicts}sorted: the branch goes one way then the other, so it predicts
  3. Solve
    random: P(mispredict)≈0.5⇒≈7.5 cycles wasted per element\text{random: } P(\text{mispredict}) \approx 0.5 \Rightarrow \approx 7.5 \text{ cycles wasted per element}random: P(mispredict)≈0.5⇒≈7.5 cycles wasted per element
  4. sorted: P≈0⇒effectively free\text{sorted: } P \approx 0 \Rightarrow \text{effectively free}sorted: P≈0⇒effectively free
  5. Answer
    several times faster, with identical instructions\text{several times faster, with identical instructions}several times faster, with identical instructions

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.

Start the free 7-day trialSign in

Nothing is charged for 7 days, and you can cancel before then. Or read Complexity: reading it off, and deriving it in full, free.

C++ for trading: RAII, moves and what belongs on the hot path →
On this page
  • The memory hierarchy
  • The memory hierarchy, in nanoseconds
  • The cache line is the unit
  • Worked example

QuantMax · 141 lessons · 1342 questions · c5c0caa

  • Premium
  • Arbitrage trees
  • Horse racing
  • Invite friends
  • Account
  • About QuantMax

Firm names identify publicly reported question patterns and nothing more. QuantMax is not affiliated with, endorsed by, or recruiting for any firm named in the curriculum. Everything you do in lessons and the question bank is kept to your account.