Three books
- A: std::map for price levels plus std::unordered_map for orders. About 100 lines of standard containers.
- B: a std::priority_queue per side with lazy deletion, and a std::map of orders, so cancels use a tree lookup instead of a hash.
- C, ours: the whole 10,001-tick price band as one flat array (80 KB), a bitmap scanned 64 levels at a time with clz/ctz to find the next best price, and our own open-addressing order table with Fibonacci hashing, linear probing and backward-shift deletion.
Why C wins
Every operation in C does a small, fixed amount of work: one probe, one array write and, at worst, a 79-word bitmap scan. A and B depend on tree depth, cache state and malloc.
The counters show it. C retires 1.18 instructions per cycle against 0.80 for A, misses L1 about 5× less often, and allocates nothing per operation. B's cancel is slowest because walking its tree is a chain of dependent cache misses: the CPU only learns where the next node is after loading the current one.
What we would try next
- Take integer ticks in the API: llround alone is 2.0% of C's samples.
- Allocate the order table once at startup and pre-touch it, perhaps with huge pages.
- Prefetch the hash slot as soon as the id is known.
- Measure p99 and max latency, not only medians, which hide C's one spike: the table resize.