Lecture series · 48 lessons · 7 parts · C++23

C++23 for Low-Latency Trading

From what is a class to cache-line padding, branchless decoding and a wait-free order book. Every chapter earns its place on a hot path: if it does not show up in a tick-to-trade profile, it is not here.

0allocations on the hot path
64 Bthe unit that actually matters
p99.9the number you are graded on

How to use this series

  1. Read a lesson, then compile its snippets. Nothing here is theoretical.
  2. Paste the hot ones into a disassembler and read the output. The assembly is the ground truth.
  3. Measure before you believe. Part IV teaches you how to measure without lying to yourself.

Compiles with GCC 14+ or Clang 18+ using -std=c++23 -O2. Linux-specific material is flagged where it appears.

Where to start

Curriculum

Part I - Foundations

The language from zero: translation units, classes, enums, pointers, value categories, ownership.

01How a C++ Program Is BuiltPreprocessor, compiler, linker. Where your code actually goes, why headers are not modules, and which build flags decide whether you are fast.25 min01aInteractive C++: A REPL for Asking QuestionsThe edit-compile-run loop is right for building systems and wrong for asking questions. An incremental C++ session answers layout, decoding and arithmetic questions in seconds, using your production headers.30 min02Types, Bits and RepresentationWhat the machine actually stores when you write int, double or char, and the conversion rules that quietly destroy correctness before you ever reach the hot path.30 min03Functions, References and constHow arguments actually reach a function on x86-64, why const reference is the wrong default for small types, and the const placement rules that decide what you may modify.25 min04What a Class Really IsA class is a struct with rules attached. The hidden this pointer, the member initialiser list, RAII, and the layout properties that decide whether you can memcpy it into a ring buffer.35 min05enum class and Strong TypesMaking the compiler reject send(qty, price) at zero runtime cost, using scoped enumerations for closed sets and tagged wrappers for the integers that keep getting swapped.25 min06Pointers, Arrays and the Memory ModelAddresses, the three storage durations and what each costs, why an array forgets its own size the moment you pass it, and how to read a network buffer without invoking undefined behaviour.30 min07lvalues, rvalues and Move SemanticsWhat the compiler means by an expression having identity, how references bind, why std::move moves nothing, and why a missing noexcept makes std::vector copy your orders instead of moving them.40 min08new, delete and Smart PointersWhat operator new really does to your latency, how unique_ptr costs nothing and shared_ptr costs more than you think, and a decision table for who owns what.35 min09Templates and Generic CodeHow the compiler generates code from templates, why a compile-time capacity turns a modulo into an AND, what the zero-cost abstraction claim actually means, and the compile-time and icache bills that come with it.35 min10The STL Containers, HonestlyWhat each standard container is actually made of, what it costs in cache misses rather than in big-O, and which of them belong anywhere near a hot path.35 min11Algorithms, Iterators and RangesThe standard algorithms worth knowing on a trading system, why lower_bound on a vector beats map::find, and where C++20 ranges and lazy views help you and where they quietly cost you.30 min

Part II - Modern C++23

Compile-time computation, concepts, views and vocabulary types that cost nothing at run time.

Part III - The Machine

Caches, alignment, branch prediction, inlining, allocation-free data structures.

19The Memory HierarchyRegisters to DRAM in cycles and nanoseconds, why the cache line is the unit of everything, and how to stop counting instructions and start counting cache lines.35 min20Data-Oriented Design: AoS vs SoALaying out an order pool so the hot loop touches one cache line per order - member ordering, hot/cold splitting, 32-bit handles and slot arrays, with the byte arithmetic at every step.40 min21Alignment, Padding and False SharingHow to compute a struct's size by hand, why an object straddling a cache line costs extra, and the two-line fix for the multithreaded slowdown that no profiler points at directly.30 min22Branches, Prediction and [[likely]]What a mispredicted branch costs, what the predictor can and cannot learn, and the honest trade-off between branchy and branchless code.35 min23Inlining, virtual and DevirtualizationWhat a function call really costs, why the inline keyword does not inline, how virtual dispatch works, and four ways to get polymorphism without paying for it.35 min23aCRTP and Static PolymorphismThe curiously recurring template pattern, what it buys over virtual, the mixin trick it enables, the three ways it bites, and where C++23 deducing this retires it.35 min24Programming Without the HeapWhy malloc is banned on the hot path, and the full toolkit that replaces it: StaticVector, arenas, handle pools, pmr, pre-faulting and an operator new that aborts.45 min25Vectorization and SIMDGetting one instruction to do eight comparisons: what the auto-vectoriser needs, how to check it worked, hand-written AVX2 and SWAR, and when wider registers buy you nothing.35 min

Part IV - Latency Engineering

Measuring nanoseconds honestly, then removing them: layout, prefetch, SIMD, PGO.

26Measuring Latency HonestlyClocks, calibration, histograms and percentiles; coordinated omission, hardware timestamping, and why a 2% improvement is usually noise.35 min27Microbenchmarking Without LyingThe optimiser deletes the code you are timing, an unrelated edit moves the loop across a cache line and the number shifts, and the win never appears in production. How to build a benchmark you can believe.30 min28Profiling: perf, Counters, Top-DownReading cycles, IPC and cache misses from perf stat, finding the guilty line with perf record, and using the top-down method to decide whether you are frontend bound, backend bound, or just mispredicting.35 min28aTracing: XRay, Processor Trace and FriendsSampling tells you where the average microsecond goes. Tracing tells you what happened on the one occasion that was slow, which is the only occasion you are paid for.40 min29Code Layout, iCache, PGO and BOLTInstructions live in the same memory hierarchy as data. Hot/cold splitting, text ordering, profile-guided optimisation and post-link layout, measured with iTLB and L1i counters rather than by feel.30 min29aCheap Wins from the BuildThe ranked list of things you can change in the build without touching a line of source, what each is typically worth, and how to prove it was the flag and not the weather.35 min30Taming the OS and the HardwareCore isolation, interrupt affinity, C-states, NUMA placement, huge pages and locked memory. Everything you do to the box so that the code you tuned is allowed to run without interruption.35 min

Part V - Concurrency

Atomics, memory ordering, wait-free queues, core pinning, seqlocks for market data.

Part VI - Trading Systems

Feed handlers, order books, tick-to-trade paths and a capstone you can compile.

Appendix

Reference cards, compiler flags, further reading.