TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24470 19
I optimized my Order Matching Engine by 560% (129k → 733k ops/sec) thanks to your feedback

Hey everyone,

A while back I shared my C++ Order Matching Engine here and got some "honest" feedback about my use of std::list and global mutexes.

I took that feedback to heart and spent the last week refactoring the core. Here are the results and the specific optimizations that worked:

The Results:

Baseline: \~129,000 orders/sec (MacBook Air)
Optimized: \~733,000 orders/sec
Speedup: 5.6x

The Optimizations:

1. Data Structure: `std::list` -> `std::deque` + Tombstones
Problem: My original implementation used std::list to strictly preserve iterator validity. This killed cache locality.
Fix: Switched to `std::deque`. It offers decent cache locality (chunked allocations) and pointer stability.
Trick: Instead of erase() (which is O(N) for vector/deque), I implemented "Tombstone" deletion. Orders are marked active = false. The matching engine lazily cleans up dead orders from the front using pop_front() (O(1)).
2. Concurrency: Global Mutex -> Sharding
Problem: A single `std::mutex` protected the entire Exchange.
Fix: Implemented fine-grained locking. The Exchange now only holds a Shared (Read) lock to find the correct OrderBook. The OrderBook itself has a unique mutex. This allows massively parallel trading across different symbols.
3. The Hidden Bottleneck (Global Index)
I realized my cancelOrder(id) API required a global lookup map (`OrderId` \-> `Symbol`) to find which book an order belonged to. This map required a global lock, re-serializing my fancy sharded engine.
Fix: Changed API to cancelOrder(symbol, id). Removing that global index unlocked the final 40% performance boost.

The code is much cleaner now

I'd love to hear what you think of the new architecture. What would you optimize next? Custom Allocators? Lock-free ring buffers?

PS - I tried posting in the showcase section, but I got error "unable to create document" (maybe because I posted once recently, sorry a little new to reddit also)

Github Link - https://github.com/PIYUSH-KUMAR1809/order-matching-engine

https://redd.it/1pk5iv3
@r_cpp
GitHub GitHub - PIYUSH-KUMAR1809/order-matching-engine: High performance order matching engine High performance order matching engine. Contribute to PIYUSH-KUMAR1809/order-matching-engine development by creating an account on GitHub.
More from @r_cpp
  1. Oct 10, 2026Yantra: an LALR(1) parser generator for C++23 Yantra: an LALR(1) parser generator for C++2…
  2. Oct 10, 2026Opting in, and back out again https://dryperspective.github.io/posts/opting-in/ https://re…
  3. Oct 10, 2026I’m Building Bursztyn OS a Polish Operating System From Scratch I’m building Bursztyn OS,…
  4. Oct 9, 2026std::variant, operator== and pattern matching I was thinking, wouldn't adding equality com…
  5. Oct 8, 2026Profiles for simplicity and guarantees - Bjarne Stroustrup - CppCon 2026 https://youtu.be/…
  6. Oct 8, 2026How to Fix autoconf-style Configuration Probing https://build2.org/blog/fix-autoconf.xhtml…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →