A C++20 limit order book prototype exploring price-time priority, pooled order storage, and a producer/consumer queue.
- Ordered bid and ask price levels (
std::map), with linked orders within each level. - An order-ID index (
std::unordered_map) for cancellation lookup. Unlinking within a level is constant-time; cancellation also performs a price-level tree lookup. - An SPSC ring buffer between a synthetic producer and the matching thread.
- Preallocated order objects with a mutex-protected free list. The maps and free-list container can allocate; this is not a zero-allocation or wholly lock-free engine.
The repository includes a historical latency plot from an Apple M-series machine. The exact chip, compiler configuration, and raw run data are not recorded here, so the plot is illustrative rather than a reproducible performance guarantee.
The two benchmark programs measure different things:
apps/main.cppgenerates one million synthetic orders. Its per-order samples timebook.add_orderafter dequeue, including timer overhead; they do not measure producer-to-consumer latency. The printedAvg Latencyis total consumer elapsed time divided by processed orders, an amortized time per order. Its reciprocal estimates throughput for that run, not a latency percentile.benchmarks/EngineBench.cppuses Google Benchmark. Each timed iteration allocates and submits two crossing orders, so its iteration time is not a per-order figure.
scripts/make_plot.py computes mean, median, and percentiles from build/latencies.csv. These are distinct statistics; the histogram omits the top 0.1% of samples for display while computing statistics over the full sample.
The demo uses synthetic traffic, with no network gateway, persistence, exchange protocol, or live-trading integration. Before comparing performance, record the chip, OS, compiler, build flags, workload, and raw output, and repeat the run.
apps/main.cpp Synthetic producer/consumer demo
benchmarks/EngineBench.cpp Google Benchmark crossing-order workload
src/ Order book, price levels, pool, and ring buffer
tests/OrderBookTest.cpp GoogleTest tests
scripts/make_plot.py Latency visualization
plots/ Historical plot
CMakeLists.txt Build configuration
Requires a C++20 compiler and CMake 3.15+. CMake downloads GoogleTest and Google Benchmark.
From the repository root:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j4
./build/unit_tests
./build/benchmarks
cd build
./hft_appTo regenerate the plot, return to the repository root, install pandas, numpy, and matplotlib, then run:
python scripts/make_plot.pyThis writes latency_distribution.png in the current directory using build/latencies.csv.
