Skip to main content
RaoufResearchOS
English
Sydney, AustraliaCurrent Sydney Time—
Press Escape to close this window.
Inside NanoMatch: Matching Data Structures and Synthetic Benchmarks
Back

C++

Inside NanoMatch: Matching Data Structures and Synthetic Benchmarks

Expected O(1) order-ID lookup, O(log M) cancellation, per-call latency measurements and a standalone pool allocator in a C++20 matching-engine prototype.

Matching orders with explicit data structures

NanoMatch is a C++20 price-time matching engine with Limit, Market, Immediate-or-Cancel (IOC) and Fill-or-Kill (FOK) orders. A MatchingEngine routes requests to independent books per symbol. CSV input/output and deterministic synthetic benchmarks make the implementation inspectable.

Updated 17 November 2026. NanoMatch pull request #3 fixed defects found in review and rewrote the benchmarks. The figures, test counts and source links on this page follow the repository after that change.

Three cooperating structures

LayerStructureRole
Price levelsstd::map<Price, PriceLevel>Orders prices; new levels require O(log M) tree insertion and the best level is available through begin()
Per-level queuestd::list<Order>Maintains FIFO time priority and permits constant-time erase by a stored iterator
Order-ID indexstd::unordered_map<OrderID, OrderLocation>Provides expected O(1) ID lookup and stores the price, side and queue iterator

ID lookup and cancellation have different costs. After the hash lookup, cancel_order() calls BookSide::remove_order(), which searches the price-level tree with std::map::find. Cancellation therefore includes O(log M) work. Reducing an order's quantity keeps its place in the queue; any other modification cancels and re-adds the order, preserving its type and losing its previous time priority.

Matching and order types

The matching loop takes orders from the best opposing price level in FIFO order. Limit remainders can rest on the book; unfilled Market and IOC quantities do not. FOK performs an available-quantity check before matching. These are implemented prototype behaviours, not certification of exchange-grade operation.

What the pool allocator demonstrates

The repository includes a fixed-capacity free-list PoolAllocator<T> and a separate benchmark comparing allocation with new/delete. It pre-allocates storage and reuses slots.

The matching book still stores orders in a default std::list, with ordinary map, hash-map and trade-vector storage. The pool's presence does not establish an allocation-free matching hot path or prove that it removes tail-latency spikes from order processing.

Read the benchmark as a workload

The README reports these results for Release builds using -O3 -march=native, all taken on 17 November 2026:

Repository-reported metricValueScope
Throughput7.6 to 8.2 million operations/s on GitHub-hosted runners (GCC 15.2); 11.7 million on an idle Apple M2Median of five runs over 5M synthetic add_order / cancel_order calls, about one in five a cancel of an order resting at that moment; single thread
Add that restsp50 120 ns, p99 220 ns, p99.9 290 nsPer-call latency benchmark, second CI run; every sample includes about 20 ns of clock reads
Add that trades on arrivalp50 170 ns, p99 441 ns, p99.9 621 nsSame benchmark and run
Cancel of a resting orderp50 90 ns, p99 160 ns, p99.9 210 nsSame benchmark and run

The benchmarks these figures replace cancelled the same ID over and over, so most of those cancels were lookups that missed and cost almost nothing, and the latency figure covered adds only. The rewritten benchmarks cancel an order that is resting at that moment and time adds that rest, adds that trade and cancels separately. Read the numbers with their limits: the CI runs are on shared virtual machines and the two differ by about 8%; buys and sells are drawn from the same price range, so the book stays shallow and long queues are not exercised; and only the order book is timed, not parsing, validation or I/O. They are not portable performance guarantees and not a claim about matching under realistic load.

Tests and evidence boundaries

The repository defines 170 Google Test cases across 20 test files. They include a regression test for each defect found in review and a randomized differential test that compares the book with a naive reference after every request. CI runs them under AddressSanitizer and UndefinedBehaviorSanitizer, alongside compiler/build matrices, cppcheck and formatting checks. This page reports the repository's counts and its CI configuration; the suite was not rerun separately for it.

Pinned sources

End of articleID: nanomatch-deep-dive

Search

Find a project, paper, write-up, or page.

30 / 44 · Search results

↑ ↓ Workspace NavigationEnter OpenEsc Close