High-performance matching engine in C++23, achieving sub-100ns core latency on a million price levels.
Environment: 11th Gen Intel(R) Core(TM) i5-11400H @ 2.70GHz, MSVC (Release mode), Windows/WSL2.
Note on Benchmarking: The report shows two consecutive runs. The first (10k levels) serves as a CPU warm-up, stabilizing frequency and caches. The second run demonstrates the engine's performance on 1,000,000 price levels, maintaining sub-microsecond P99.9 latency.
This benchmark is located in bench/OrderBookBenchBuyExisted.cpp
boost::intrusive::listandabsl::flat_hash_map: Provide amortized O(1) "add", "delete", "find".- Hierarchical Bitsets: Used for best-price discovery.
- Zero-Allocation Path: No dynamic memory allocation in the hot loops (Object Pooling & Arrays).
- Asynchronous Pipeline:
OrderBook -> [SPSC] -> Logger -> [SPSC WAL] -> Outputer - Lock-free Primitives: Custom SPSC queues for minimal inter-thread jitter.
- Mechanical Sympathy: Explicit cache-line alignment (
alignas(64)) and branch prediction hints ([[likely]]). - Persistence & Recovery (WAL): Integrated a Write-Ahead Logging strategy. All incoming events are asynchronously streamed to a persistent segment. This ensures data recoverability and consistency in case of a system crash.
- Fault Tolerance & Burst Handling: The pipeline architecture uses pre-allocated lock-free SPSC queues as high-speed buffers. This provides a safety margin that absorbs traffic spikes without backpressuring the critical matching path.
- Measured using TSC (Time Stamp Counter) with serialization barriers (rdtscp) to eliminate out-of-order execution noise.
- Ticks are converted to nanoseconds only in the Outputer to minimize hot-path overhead
add_compile_definitions(MAX_ORDERS=2'100'000ULL) # Pre-allocated memory
add_compile_definitions(NANOSECONDS_PER_STATS_SNAPSHOT=2*10'000'000'000ULL) # Telemetry interval
add_compile_definitions(RESET_WAL_FILES=true) # Continue read where program stopped earlier
add_compile_definitions(CLEAR_LOGGER_STATS_AFTER_SNAPSHOT=false) # False if you want to see full statistics
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j
./benchmark_orderbookThis project uses the following libraries:
- Abseil (Google) - Used
absl::flat_hash_mapfor O(1) order tracking. - Boost.Intrusive - Used for zero-allocation intrusive lists in price buckets.
- Google Benchmark - Microbenchmarking framework for latency measurements.
- Google Test - Unit testing and verification.