This project is a performance-optimized fork of the engineswap/orderbook — a basic C++ order book implementation designed for educational purposes.
While the original claims "orders can be executed in 4ns", real-world profiling reveals significantly higher latency and high jitter (e.g., delete operations up to 24 µs). This version prioritizes low-latency, predictable performance suitable for HFT-like workloads.
| Metric | Original (engineswap) |
This Fork (dimasikpower) |
|---|---|---|
| Market order latency | 314–353 ns | 178–185 ns |
| Modify latency | 1330–1390 ns | 287–299 ns |
| Delete latency | Up to 24,191 ns (jitter) | 320–351 ns (stable) |
| dTLB-load-misses | ~740,000 | ~73,000 (↓ 10×) |
| Memory layout | Sorted std::vector + dynamic structures |
Price-indexed array + custom PriceLevel |
| Allocation strategy | Dynamic (new/delete) |
Preallocated OrderPool + vector::reserve() |
| Access complexity | O(N) or O(log N) | O(1) per price level |
- Replaced dynamic containers with a fixed-size price-indexed array (
1–200,000cents), covering all major instruments under $2000. - Implemented FIFO semantics via a
headpointer inPriceLevel— zero-costpop_front(). - Eliminated reallocations during execution using
std::vector::reserve(). - Reduced TLB pressure by keeping data dense, cache-friendly, and page-local.
- All optimizations validated with
perfand real latency benchmarks.
💡 Trade-off: Limited price range (vs arbitrary prices in the original) — but this aligns with how real exchanges represent prices in discrete ticks and enables O(1) performance.
Run benchmarks with TLB miss profiling:
make tlb- FIFO queue matching algorithm
- Visualization
- Accepts Market & Limit orders
- Whole and partial fills
- Fast, can execute orders in 4ns
- Unit tests
The project is designed using Object-Oriented Programming (OOP) principles. It is divided into three main parts:
main.cpp: This is where user interaction is handled. Users can place market or limit orders and the program will process them accordingly.order.hpp: This file contains theOrderstruct, which represents an order. Each order has properties like price, quantity, and type (market or limit).orderbook.cpp: This file contains theOrderbookclass, which manages order objects. It uses a FIFO queue to ensure that orders are processed in the order they are received. It also has logic to execute incoming orders against the book. And finally it has logic to visualize the book.unit_tests.cpp: This file has unit tests to make sure the orderbook functions as expected.
To compile and run the program, follow these steps:
- Clone the repo:
https://github.com/dimasikpower/orderbook.git cd cpp-orderbook- Compile the program using
make - Run the program with
./main - (Optional) Run unit tests with
./unit_tests
A market buy order getting filled

Reduced market order latency from 313 ns → 180 ns (42% improvement) Eliminated TLB pressure by replacing std::deque with custom PriceLevel with vector + reserve Achieved < 80k dTLB-load-misses (vs 740k originally) via memory layout optimization Used perf, TLB profiling, and low-level CPU knowledge to guide optimizations Full FIFO semantics, thread-safe design, supports 100k+ orders


