-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyper_core_engine.cpp
More file actions
1290 lines (1077 loc) · 49.7 KB
/
hyper_core_engine.cpp
File metadata and controls
1290 lines (1077 loc) · 49.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ═══════════════════════════════════════════════════════════════════════
* Hyper-Core HFT Matching Engine
* Version: 1.0.0 (Production Architecture)
* Standard: C++20 (Concepts, Atomics, constexpr)
* ═══════════════════════════════════════════════════════════════════════
*
* Architecture:
*
* ┌───────────────────┐ SPSC RingBuffer ┌──────────────────┐
* │ GatewaySimulator │ ────────(lock-free)────────▶ │ MatcherThread │
* │ (Producer Thread) │ │ (Pinned to Core) │
* └───────────────────┘ └────────┬─────────┘
* │
* ┌─────────────────┼────────────┐
* ▼ ▼ ▼
* ┌────────────┐ ┌────────────┐
* ┌──────────┐ │ ObjectPool │ │ OrderBook │ │ Stats │ │ (Order) │
* │(Intrusive) │ │ Counters │ └──────┬──────┘ └────────────┘ └──────────┘ │
* ┌──────┴──────┐
* │ MemoryArena │
* │ (Bump Alloc)│
* └─────────────┘
*
* Components:
* 1. MemoryArena -> Bump allocator, zero-fragmentation
* 2. ObjectPool<T> -> Intrusive free-list, zero-alloc hot path
* 3. LockFreeRingBuffer<T> -> SPSC with cache-line isolation
* 4. Order / OrderMessage -> Cache-line-aligned data structures
* 5. PriceLevel -> Intrusive linked list of orders at one price
* 6. OrderBook -> Bid/Ask sides, price-time matching
* 7. MatcherThread -> Pinned busy-spin event loop
* 8. GatewaySimulator -> Synthetic order generator
*
* Decisions:
* | Decision | Alternative | Rationale |
* |------------------------|--------------------|−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−|
* | SPSC RingBuffer | mutex + queue | Zero contention, no
* syscalls | | alignas(64) head/tail | default alignment | Eliminates
* false sharing | | acquire/release fences | seq_cst |
* Minimal fence overhead | | Intrusive List Levels |
* std::vector<Order*> | Zero-alloc, unbounded capacity | | Placement-new Pool
* | new/delete | Zero heap alloc on hot path | | Fixed-point prices
* | double | Deterministic comparison | | Bump allocator
* Arena | malloc per object | O(1) alloc, zero fragmentation |
*
* Dependencies: None (stdlib only)
* Compiler: g++ -std=c++20 -O2 -Wall -Wextra -pthread
*/
// ═══════════════════════════════════════════════════════════════════════
// 1. INCLUDES
// ═══════════════════════════════════════════════════════════════════════
#include <array>
#include <atomic>
#include <cassert>
#include <chrono>
#include <concepts>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <new>
#include <random>
#include <thread>
#include <type_traits>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#else
#include <pthread.h>
#include <sched.h>
#endif
// ═══════════════════════════════════════════════════════════════════════
// 2. CONSTANTS & CONFIGURATION
// ═══════════════════════════════════════════════════════════════════════
namespace config {
inline constexpr std::size_t CACHE_LINE_SIZE = 64;
inline constexpr std::size_t RING_BUFFER_CAPACITY = 1
<< 16; // 65536, power-of-2
inline constexpr std::size_t ARENA_SIZE_BYTES = 64 * 1024 * 1024; // 64 MB
inline constexpr std::size_t MAX_ORDERS = 500'000;
inline constexpr std::size_t MAX_PRICE_LEVELS = 10'000;
inline constexpr std::size_t MAX_ORDERS_PER_LEVEL = 1'024;
inline constexpr std::size_t ORDER_ID_MAP_SIZE = 1 << 20; // 1M slots
inline constexpr int MATCHER_CORE_ID = 1; // pin to core 1
inline constexpr int64_t PRICE_MULTIPLIER = 10'000; // fixed-point: 4 decimals
inline constexpr int64_t MID_PRICE = 1'000'000; // $100.0000 in fixed-point
inline constexpr std::size_t GATEWAY_ORDER_COUNT = 200'000;
inline constexpr double LIMIT_ORDER_RATIO = 0.70;
inline constexpr double MARKET_ORDER_RATIO = 0.20;
// Cancel ratio = 1.0 - LIMIT - MARKET = 0.10
} // namespace config
// ═══════════════════════════════════════════════════════════════════════
// 3. C++20 CONCEPTS
// ═══════════════════════════════════════════════════════════════════════
/// T must be trivially copyable and destructible for raw memory operations.
template <typename T>
concept TriviallySafe =
std::is_trivially_copyable_v<T> && std::is_trivially_destructible_v<T>;
/// T must fit within a reasonable size bound for stack/pool allocation.
template <typename T>
concept BoundedSize = sizeof(T) <= 4096;
/// Combined constraint for ObjectPool and RingBuffer element types.
template <typename T>
concept PoolEligible = TriviallySafe<T> && BoundedSize<T>;
/// Callable that accepts a reference to T (for ring buffer consumer callbacks).
template <typename F, typename T>
concept ConsumerOf = std::invocable<F, T &>;
// ═══════════════════════════════════════════════════════════════════════
// 4. MEMORY ARENA — Bump Allocator (zero fragmentation)
// ═══════════════════════════════════════════════════════════════════════
/// Pre-allocates a single contiguous block. O(1) bump-pointer allocation.
/// No individual deallocation — entire arena freed in destructor.
///
/// Design:
/// - Single allocation at construction via aligned_alloc
/// - Bump pointer advances monotonically
/// - Alignment respected per-allocation via pointer arithmetic
/// - Thread safety: NOT thread-safe (used exclusively by matcher thread)
class MemoryArena {
public:
explicit MemoryArena(std::size_t size_bytes)
: capacity_(size_bytes), offset_(0) {
// Allocate cache-line-aligned backing store
base_ = static_cast<std::byte *>(
#ifdef _WIN32
_aligned_malloc(size_bytes, config::CACHE_LINE_SIZE)
#else
std::aligned_alloc(config::CACHE_LINE_SIZE, size_bytes)
#endif
);
if (!base_) {
std::cerr << "[FATAL] MemoryArena: allocation failed (" << size_bytes
<< " bytes)\n";
std::abort();
}
std::memset(base_, 0, size_bytes);
}
~MemoryArena() {
#ifdef _WIN32
_aligned_free(base_);
#else
std::free(base_);
#endif
}
// ─────────── Non-copyable, non-movable ───────────
MemoryArena(const MemoryArena &) = delete;
MemoryArena &operator=(const MemoryArena &) = delete;
MemoryArena(MemoryArena &&) = delete;
MemoryArena &operator=(MemoryArena &&) = delete;
// ─────────── API ───────────
/// Allocate `count` objects of type T with proper alignment. O(1).
template <typename T> [[nodiscard]] T *allocate(std::size_t count = 1) {
constexpr std::size_t alignment = alignof(T);
const std::size_t total_bytes = sizeof(T) * count;
// Align the current offset
std::size_t aligned_offset = (offset_ + alignment - 1) & ~(alignment - 1);
if (aligned_offset + total_bytes > capacity_) [[unlikely]] {
std::cerr << "[FATAL] MemoryArena: out of memory ("
<< aligned_offset + total_bytes << " > " << capacity_ << ")\n";
std::abort();
}
T *ptr = reinterpret_cast<T *>(base_ + aligned_offset);
offset_ = aligned_offset + total_bytes;
return ptr;
}
/// Reset arena to beginning (invalidates all prior allocations).
void reset() noexcept { offset_ = 0; }
// ─────────── Stats ───────────
[[nodiscard]] std::size_t used() const noexcept { return offset_; }
[[nodiscard]] std::size_t remaining() const noexcept {
return capacity_ - offset_;
}
[[nodiscard]] std::size_t capacity() const noexcept { return capacity_; }
private:
std::byte *base_;
std::size_t capacity_;
std::size_t offset_;
};
// ═══════════════════════════════════════════════════════════════════════
// 5. OBJECT POOL — Zero-Allocation Recycler
// ═══════════════════════════════════════════════════════════════════════
/// Intrusive free-list object pool backed by MemoryArena.
/// acquire() = O(1), release() = O(1). No heap allocation on hot path.
///
/// Design:
/// - Pre-allocates N slots from arena at construction
/// - Free-list stored as stack of indices (cache-friendly, no pointer
/// chasing)
/// - acquire() pops index, returns pointer via placement-new
/// - release() pushes index back, calls trivial destructor
///
/// Constraint: T must be TriviallySafe && BoundedSize (PoolEligible concept).
template <PoolEligible T> class ObjectPool {
public:
ObjectPool(MemoryArena &arena, std::size_t max_objects)
: max_objects_(max_objects), free_count_(max_objects) {
// Allocate contiguous storage for objects
storage_ = arena.allocate<T>(max_objects);
// Allocate free-list index stack
free_stack_ = arena.allocate<uint32_t>(max_objects);
// Initialize free stack: all indices available
for (std::size_t i = 0; i < max_objects; ++i) {
free_stack_[i] = static_cast<uint32_t>(i);
}
}
// ─────────── API ───────────
/// Acquire a pre-allocated object. O(1). Returns nullptr if pool exhausted.
[[nodiscard]] T *acquire() noexcept {
if (free_count_ == 0) [[unlikely]] {
return nullptr;
}
--free_count_;
uint32_t idx = free_stack_[free_count_];
T *slot = &storage_[idx];
// Placement-new: construct in pre-allocated memory
return ::new (static_cast<void *>(slot)) T{};
}
/// Release an object back to the pool. O(1).
void release(T *obj) noexcept {
if (!obj) [[unlikely]]
return;
// Calculate index from pointer arithmetic
auto idx = static_cast<uint32_t>(obj - storage_);
assert(idx < max_objects_ && "release: pointer not from this pool");
// Trivial destructor (guaranteed by concept), no-op but semantically
// correct
obj->~T();
free_stack_[free_count_] = idx;
++free_count_;
}
// ─────────── Stats ───────────
[[nodiscard]] std::size_t available() const noexcept { return free_count_; }
[[nodiscard]] std::size_t in_use() const noexcept {
return max_objects_ - free_count_;
}
[[nodiscard]] std::size_t capacity() const noexcept { return max_objects_; }
private:
T *storage_;
uint32_t *free_stack_;
std::size_t max_objects_;
std::size_t free_count_;
};
// ═══════════════════════════════════════════════════════════════════════
// 6. LOCK-FREE RING BUFFER — SPSC (Single Producer Single Consumer)
// ═══════════════════════════════════════════════════════════════════════
/// Cache-line-isolated SPSC ring buffer with acquire/release semantics.
///
/// Design:
/// - Power-of-2 capacity for branchless modulo (bitwise AND)
/// - head_ (consumer) and tail_ (producer) on separate cache lines
/// - Producer: store(tail_, release) — data visible before index advances
/// - Consumer: load(tail_, acquire) — sees data written before tail advanced
/// - No CAS loops needed (single producer, single consumer)
///
/// Complexity: push() O(1), pop() O(1)
/// Latency: ~5-15ns per operation (no syscalls, no contention)
template <PoolEligible T> class LockFreeRingBuffer {
static_assert((config::RING_BUFFER_CAPACITY &
(config::RING_BUFFER_CAPACITY - 1)) == 0,
"Ring buffer capacity must be a power of 2");
public:
explicit LockFreeRingBuffer(MemoryArena &arena)
: mask_(config::RING_BUFFER_CAPACITY - 1) {
buffer_ = arena.allocate<T>(config::RING_BUFFER_CAPACITY);
}
// ─────────── Producer API (single thread) ───────────
/// Push an element. Returns false if buffer is full (back-pressure).
[[nodiscard]] bool push(const T &item) noexcept {
const uint64_t current_tail = tail_.value.load(std::memory_order_relaxed);
const uint64_t next_tail = current_tail + 1;
// Full check: if next_tail catches up to head, buffer is full
if (next_tail - head_.value.load(std::memory_order_acquire) > mask_)
[[unlikely]] {
return false;
}
buffer_[current_tail & mask_] = item;
// Release: ensure the data write is visible before tail advances
tail_.value.store(next_tail, std::memory_order_release);
return true;
}
// ─────────── Consumer API (single thread) ───────────
/// Pop an element. Returns false if buffer is empty.
[[nodiscard]] bool pop(T &out) noexcept {
const uint64_t current_head = head_.value.load(std::memory_order_relaxed);
// Empty check: head has caught up to tail
if (current_head >= tail_.value.load(std::memory_order_acquire))
[[unlikely]] {
return false;
}
out = buffer_[current_head & mask_];
// Release: ensure the read is complete before head advances
head_.value.store(current_head + 1, std::memory_order_release);
return true;
}
// ─────────── Stats ───────────
[[nodiscard]] std::size_t size() const noexcept {
auto t = tail_.value.load(std::memory_order_relaxed);
auto h = head_.value.load(std::memory_order_relaxed);
return static_cast<std::size_t>(t - h);
}
[[nodiscard]] bool empty() const noexcept { return size() == 0; }
private:
// ── Cache-line-isolated atomic counters ──
// Each on its own 64-byte cache line to prevent false sharing
struct alignas(config::CACHE_LINE_SIZE) AlignedAtomic {
std::atomic<uint64_t> value{0};
// Padding is implicit: alignas(64) ensures the struct occupies
// a full cache line, so the next member starts on a new line.
};
AlignedAtomic head_; // Written by consumer ONLY
AlignedAtomic tail_; // Written by producer ONLY
T *buffer_;
uint64_t mask_;
};
// ═══════════════════════════════════════════════════════════════════════
// 7. ORDER TYPES & DATA STRUCTURES
// ═══════════════════════════════════════════════════════════════════════
enum class Side : uint8_t {
BID = 0, // Buy
ASK = 1, // Sell
};
enum class OrderType : uint8_t {
LIMIT = 0,
MARKET = 1,
CANCEL = 2,
};
/// Core order structure — designed to fit in a single cache line (64 bytes).
///
/// Layout:
/// id 8B offset 0
/// instrument_id 8B offset 8
/// price 8B offset 16 (fixed-point, price * PRICE_MULTIPLIER)
/// quantity 4B offset 24
/// remaining_qty 4B offset 28
/// timestamp 8B offset 32
/// side 1B offset 40
/// type 1B offset 41
/// active 1B offset 42
/// (5B implicit) offset 43 (compiler padding for 8B-aligned next)
/// next 8B offset 48 (intrusive list pointer)
///
/// Total: 56 bytes usable, alignas(64) pads to 64B cache line
struct alignas(config::CACHE_LINE_SIZE) Order {
uint64_t id = 0;
uint64_t instrument_id = 0;
int64_t price = 0; // Fixed-point: real_price * 10000
uint32_t quantity = 0;
uint32_t remaining_qty = 0;
uint64_t timestamp = 0; // Nanosecond timestamp
Side side = Side::BID;
OrderType type = OrderType::LIMIT;
uint8_t active = 0; // 1 = live, 0 = cancelled/filled
// Intrusive linked list pointer for PriceLevel — eliminates std::vector
// and its hidden malloc on the hot path. Compiler inserts 5B padding
// between active(offset 42) and next(offset 48) for 8B alignment.
Order *next = nullptr;
};
static_assert(sizeof(Order) == config::CACHE_LINE_SIZE,
"Order must be exactly one cache line");
static_assert(std::is_trivially_copyable_v<Order>,
"Order must be trivially copyable for pool/ring");
/// Message envelope for the ring buffer.
/// Contains either an order pointer (for add/match) or an order ID (for
/// cancel).
struct OrderMessage {
OrderType type = OrderType::LIMIT;
Order *order = nullptr; // Non-owning pointer from ObjectPool
uint64_t cancel_id = 0; // Only used for CANCEL messages
// Padding for trivial copy
uint8_t _pad[7] = {};
};
static_assert(std::is_trivially_copyable_v<OrderMessage>,
"OrderMessage must be trivially copyable for ring buffer");
// ═══════════════════════════════════════════════════════════════════════
// 8. INTRUSIVE ORDER LIST — Zero-Allocation FIFO Linked List
// ═══════════════════════════════════════════════════════════════════════
/// Singly-linked intrusive list for Order nodes.
/// The `next` pointer lives inside Order itself (no external allocation).
///
/// Design:
/// - push_back: O(1), sets tail->next = node, advances tail. ZERO alloc.
/// - match: O(K), walks from head, fills orders FIFO.
/// - compact: O(N), unlinks inactive nodes (periodic, not hot path).
/// - All nodes come from ObjectPool — no new/delete, no malloc.
///
/// This replaces std::vector<Order*> to eliminate hidden reallocation
/// when a price level exceeds its reserved capacity (quote stuffing defense).
class IntrusiveOrderList {
public:
IntrusiveOrderList() = default;
// ─────────── API ───────────
/// Append an order to the tail. O(1), zero allocation.
void push_back(Order *order) noexcept {
order->next = nullptr;
if (tail_) {
tail_->next = order;
} else {
head_ = order;
}
tail_ = order;
++count_;
}
/// Match against orders in FIFO order up to `qty` units.
/// Returns total quantity filled.
uint32_t match(uint32_t qty) noexcept {
uint32_t filled = 0;
Order *current = head_;
while (current && qty > 0) {
if (current->active && current->remaining_qty > 0) {
uint32_t fill_qty = std::min(current->remaining_qty, qty);
current->remaining_qty -= fill_qty;
qty -= fill_qty;
filled += fill_qty;
if (current->remaining_qty == 0) {
current->active = 0;
}
}
current = current->next;
}
return filled;
}
/// Unlink inactive/filled nodes from the list (periodic cleanup).
/// O(N) where N = list length. NOT on the hot path.
void compact() noexcept {
Order *prev = nullptr;
Order *current = head_;
while (current) {
Order *next_node = current->next;
if (!current->active || current->remaining_qty == 0) {
// Unlink this node
if (prev) {
prev->next = next_node;
} else {
head_ = next_node;
}
if (current == tail_) {
tail_ = prev;
}
current->next = nullptr;
--count_;
} else {
prev = current;
}
current = next_node;
}
}
// ─────────── Accessors ───────────
[[nodiscard]] bool empty() const noexcept { return head_ == nullptr; }
[[nodiscard]] std::size_t size() const noexcept { return count_; }
[[nodiscard]] Order *head() const noexcept { return head_; }
private:
Order *head_ = nullptr;
Order *tail_ = nullptr;
std::size_t count_ = 0;
};
// ═══════════════════════════════════════════════════════════════════════
// 9. PRICE LEVEL — Intrusive list of orders at one price point
// ═══════════════════════════════════════════════════════════════════════
/// Orders at a single price level maintained as an intrusive linked list.
/// Orders are FIFO (price-time priority). Cancellation sets remaining_qty=0
/// (lazy delete — avoids list surgery on hot path).
///
/// Key invariant: add_order is ALWAYS O(1) with ZERO heap allocation,
/// regardless of how many orders are at this price level.
///
/// Complexity: add O(1) guaranteed, match O(K) where K = fills per level
class PriceLevel {
public:
PriceLevel() = default;
explicit PriceLevel(int64_t price) : price_(price) {}
// ─────────── API ───────────
void add_order(Order *order) noexcept {
orders_.push_back(order);
cached_qty_ += order->remaining_qty;
}
/// Match against this level up to `qty` units.
/// Returns total quantity filled at this level.
uint32_t match(uint32_t qty) noexcept {
uint32_t filled = orders_.match(qty);
cached_qty_ -= filled;
return filled;
}
/// Decrement cached quantity (for external cancellation).
void reduce_qty(uint32_t amount) noexcept {
if (amount <= cached_qty_)
cached_qty_ -= amount;
else
cached_qty_ = 0;
}
/// Remove fully filled/cancelled orders (periodic cleanup, not on hot path).
void compact() noexcept { orders_.compact(); }
// ─────────── Accessors ───────────
[[nodiscard]] int64_t price() const noexcept { return price_; }
[[nodiscard]] bool empty() const noexcept { return orders_.empty(); }
[[nodiscard]] uint32_t total_qty() const noexcept { return cached_qty_; }
[[nodiscard]] std::size_t order_count() const noexcept {
return orders_.size();
}
private:
int64_t price_ = 0;
uint32_t cached_qty_ = 0; // O(1) total quantity tracking
IntrusiveOrderList orders_;
};
// ═══════════════════════════════════════════════════════════════════════
// 10. ORDER BOOK — Bid/Ask with Price-Time Matching
// ═══════════════════════════════════════════════════════════════════════
/// Cache-friendly order book with flat vector price levels.
///
/// Design:
/// - Bids and asks stored as vectors of PriceLevel, indexed by normalized
/// price
/// - Price normalization: index = (price - base_price) mapped to [0,
/// MAX_PRICE_LEVELS)
/// - Order ID -> Order* lookup via flat array (O(1) cancel)
/// - Matching: walk best bid vs best ask, fill at aggressor price
///
/// Complexity:
/// add_order: O(1) guaranteed (index + intrusive list push_back, ZERO alloc)
/// cancel: O(1) (lookup + set inactive)
/// match: O(L * K) where L = crossing levels, K = orders per level
class OrderBook {
public:
OrderBook() {
bid_levels_.resize(config::MAX_PRICE_LEVELS);
ask_levels_.resize(config::MAX_PRICE_LEVELS);
// Initialize price levels with their prices
for (std::size_t i = 0; i < config::MAX_PRICE_LEVELS; ++i) {
auto price = static_cast<int64_t>(i) * config::PRICE_MULTIPLIER / 100;
bid_levels_[i] = PriceLevel(price);
ask_levels_[i] = PriceLevel(price);
}
// Order ID map: heap-allocated flat array for O(1) lookup
// Allocated once at construction (not on hot path)
id_map_.resize(config::ORDER_ID_MAP_SIZE, nullptr);
}
// ─────────── API ───────────
/// Add a limit order to the book.
void add_order(Order *order) {
std::size_t level_idx = price_to_index(order->price);
if (level_idx >= config::MAX_PRICE_LEVELS) [[unlikely]]
return;
order->active = 1;
// Register in ID map for O(1) cancel
auto map_idx = order->id & (config::ORDER_ID_MAP_SIZE - 1);
id_map_[map_idx] = order;
if (order->side == Side::BID) {
bid_levels_[level_idx].add_order(order);
if (level_idx > best_bid_idx_)
best_bid_idx_ = level_idx;
} else {
ask_levels_[level_idx].add_order(order);
if (best_ask_idx_ == 0 || level_idx < best_ask_idx_) {
best_ask_idx_ = level_idx;
}
}
}
/// Cancel an order by ID. O(1).
/// Updates PriceLevel cached_qty_ to prevent stale-quantity infinite loops.
bool cancel_order(uint64_t order_id) {
auto map_idx = order_id & (config::ORDER_ID_MAP_SIZE - 1);
Order *order = id_map_[map_idx];
if (!order || order->id != order_id || !order->active) {
return false;
}
// Update cached quantity on the correct price level BEFORE zeroing
std::size_t level_idx = price_to_index(order->price);
if (level_idx < config::MAX_PRICE_LEVELS) {
if (order->side == Side::BID) {
bid_levels_[level_idx].reduce_qty(order->remaining_qty);
} else {
ask_levels_[level_idx].reduce_qty(order->remaining_qty);
}
}
order->active = 0;
order->remaining_qty = 0;
id_map_[map_idx] = nullptr;
++cancel_count_;
return true;
}
/// Match crossing orders (bid >= ask). Returns total filled quantity.
uint64_t match() {
uint64_t total_filled = 0;
while (best_bid_idx_ > 0 && best_ask_idx_ > 0 &&
best_bid_idx_ < config::MAX_PRICE_LEVELS &&
best_ask_idx_ < config::MAX_PRICE_LEVELS) {
auto &bid_level = bid_levels_[best_bid_idx_];
auto &ask_level = ask_levels_[best_ask_idx_];
// Crossing condition: best bid price >= best ask price
if (bid_level.price() < ask_level.price())
break;
uint32_t bid_qty = bid_level.total_qty();
uint32_t ask_qty = ask_level.total_qty();
if (bid_qty == 0) {
// No active bids at this level, move down
if (best_bid_idx_ > 0)
--best_bid_idx_;
else
break;
continue;
}
if (ask_qty == 0) {
// No active asks at this level, move up
++best_ask_idx_;
continue;
}
// Match: fill the smaller side
uint32_t match_qty = std::min(bid_qty, ask_qty);
bid_level.match(match_qty);
ask_level.match(match_qty);
total_filled += match_qty;
++match_count_;
// Update best levels if exhausted
if (bid_level.total_qty() == 0 && best_bid_idx_ > 0)
--best_bid_idx_;
if (ask_level.total_qty() == 0)
++best_ask_idx_;
}
return total_filled;
}
/// Match a market order immediately against the book.
uint64_t match_market(Order *order) {
uint64_t filled = 0;
if (order->side == Side::BID) {
// Market buy: match against asks (ascending)
for (std::size_t i = best_ask_idx_; i < config::MAX_PRICE_LEVELS; ++i) {
if (order->remaining_qty == 0)
break;
uint32_t fill = ask_levels_[i].match(order->remaining_qty);
order->remaining_qty -= fill;
filled += fill;
if (ask_levels_[i].total_qty() == 0 && i == best_ask_idx_) {
++best_ask_idx_;
}
}
} else {
// Market sell: match against bids (descending)
for (std::size_t i = best_bid_idx_; i < config::MAX_PRICE_LEVELS; --i) {
if (order->remaining_qty == 0)
break;
uint32_t fill = bid_levels_[i].match(order->remaining_qty);
order->remaining_qty -= fill;
filled += fill;
if (bid_levels_[i].total_qty() == 0 && i == best_bid_idx_) {
if (best_bid_idx_ > 0)
--best_bid_idx_;
else
break;
}
if (i == 0)
break;
}
}
if (filled > 0)
++match_count_;
return filled;
}
// ─────────── Stats ───────────
[[nodiscard]] uint64_t match_count() const noexcept { return match_count_; }
[[nodiscard]] uint64_t cancel_count() const noexcept { return cancel_count_; }
[[nodiscard]] int64_t best_bid_price() const noexcept {
if (best_bid_idx_ < config::MAX_PRICE_LEVELS) {
return bid_levels_[best_bid_idx_].price();
}
return 0;
}
[[nodiscard]] int64_t best_ask_price() const noexcept {
if (best_ask_idx_ < config::MAX_PRICE_LEVELS) {
return ask_levels_[best_ask_idx_].price();
}
return 0;
}
private:
/// Convert fixed-point price to level index.
[[nodiscard]] static std::size_t price_to_index(int64_t price) noexcept {
// Normalize: price / (PRICE_MULTIPLIER/100) gives index
auto idx = static_cast<std::size_t>(price * 100 / config::PRICE_MULTIPLIER);
return std::min(idx, config::MAX_PRICE_LEVELS - 1);
}
std::vector<PriceLevel> bid_levels_;
std::vector<PriceLevel> ask_levels_;
// Flat O(1) order ID -> Order* map (heap-allocated once, not on hot path)
std::vector<Order *> id_map_;
std::size_t best_bid_idx_ = 0;
std::size_t best_ask_idx_ = 0;
uint64_t match_count_ = 0;
uint64_t cancel_count_ = 0;
};
// ═══════════════════════════════════════════════════════════════════════
// 10. CPU PINNING — Platform-specific thread affinity
// ═══════════════════════════════════════════════════════════════════════
namespace platform {
/// Pin the calling thread to a specific CPU core.
/// Prevents context switches and ensures cache locality.
inline bool pin_thread_to_core(int core_id) {
#ifdef _WIN32
DWORD_PTR mask = static_cast<DWORD_PTR>(1) << core_id;
HANDLE thread = GetCurrentThread();
DWORD_PTR prev = SetThreadAffinityMask(thread, mask);
return prev != 0;
#else
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
return pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) ==
0;
#endif
}
/// Get current timestamp in nanoseconds (monotonic clock).
[[nodiscard]] inline uint64_t timestamp_ns() noexcept {
auto now = std::chrono::steady_clock::now();
return static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
now.time_since_epoch())
.count());
}
} // namespace platform
// ═══════════════════════════════════════════════════════════════════════
// 11. ENGINE STATISTICS — Atomic counters for cross-thread reporting
// ═══════════════════════════════════════════════════════════════════════
struct alignas(config::CACHE_LINE_SIZE) EngineStats {
std::atomic<uint64_t> orders_received{0};
std::atomic<uint64_t> orders_processed{0};
std::atomic<uint64_t> total_fills{0};
std::atomic<uint64_t> ring_buffer_full_count{0};
std::atomic<uint64_t> pool_exhausted_count{0};
std::atomic<bool> running{true};
};
// ═══════════════════════════════════════════════════════════════════════
// 12. MATCHER THREAD — Pinned busy-spin event loop
// ═══════════════════════════════════════════════════════════════════════
/// The core matching engine loop.
///
/// Design:
/// - Pinned to a dedicated CPU core (no context switches)
/// - Busy-spin: no sleep(), no yield() — minimum latency
/// - All memory from pre-allocated ObjectPool (zero heap alloc)
/// - Processes OrderMessages from the SPSC ring buffer
///
/// Hot path: pop() -> dispatch -> add/cancel/match -> stats update
/// Expected latency per order: < 1 microsecond
class MatcherThread {
public:
MatcherThread(LockFreeRingBuffer<OrderMessage> &ring_buffer,
ObjectPool<Order> &order_pool, EngineStats &stats, int core_id)
: ring_buffer_(ring_buffer), order_pool_(order_pool), stats_(stats),
core_id_(core_id) {}
/// Main entry point — runs until stats_.running becomes false.
void operator()() {
// ── Step 1: Pin to dedicated core ──
if (platform::pin_thread_to_core(core_id_)) {
// Pinning successful (silently continue)
} else {
std::cerr << "[WARN] MatcherThread: failed to pin to core " << core_id_
<< "\n";
}
// ── Step 2: Busy-spin event loop ──
OrderMessage msg{};
uint64_t loop_count = 0;
constexpr uint64_t COMPACT_INTERVAL = 100'000;
while (stats_.running.load(std::memory_order_relaxed)) {
if (ring_buffer_.pop(msg)) {
process_message(msg);
stats_.orders_processed.fetch_add(1, std::memory_order_relaxed);
}
// No sleep, no yield — pure busy-spin for minimum latency
// Periodic maintenance (not on critical path)
++loop_count;
if ((loop_count & (COMPACT_INTERVAL - 1)) == 0) [[unlikely]] {
// Could do periodic level compaction here
// book_.compact() — omitted for hot-path purity
}
}
// ── Step 3: Drain remaining messages ──
while (ring_buffer_.pop(msg)) {
process_message(msg);
stats_.orders_processed.fetch_add(1, std::memory_order_relaxed);
}
}
private:
void process_message(const OrderMessage &msg) {
switch (msg.type) {
case OrderType::LIMIT: {
book_.add_order(msg.order);
uint64_t fills = book_.match();
if (fills > 0) {
stats_.total_fills.fetch_add(fills, std::memory_order_relaxed);
}
break;
}
case OrderType::MARKET: {
uint64_t fills = book_.match_market(msg.order);
stats_.total_fills.fetch_add(fills, std::memory_order_relaxed);
// Market orders are fully processed, release back to pool
order_pool_.release(msg.order);
break;
}
case OrderType::CANCEL: {
book_.cancel_order(msg.cancel_id);
break;
}
}
}
LockFreeRingBuffer<OrderMessage> &ring_buffer_;
ObjectPool<Order> &order_pool_;
EngineStats &stats_;
int core_id_;
OrderBook book_;
};
// ═══════════════════════════════════════════════════════════════════════
// 13. GATEWAY SIMULATOR — Synthetic order generator (Producer)
// ═══════════════════════════════════════════════════════════════════════
/// Simulates an order gateway feeding the matching engine.
///
/// Generates realistic order flow:
/// - 70% Limit orders (normal price distribution around mid-price)
/// - 20% Market orders (immediate execution)
/// - 10% Cancel orders (cancel previously sent orders)
/// - Zipfian instrument distribution (few hot instruments)
class GatewaySimulator {
public:
GatewaySimulator(LockFreeRingBuffer<OrderMessage> &ring_buffer,
ObjectPool<Order> &order_pool, EngineStats &stats,
std::size_t total_orders)
: ring_buffer_(ring_buffer), order_pool_(order_pool), stats_(stats),
total_orders_(total_orders),
rng_(42) // Deterministic seed for reproducibility
{}
/// Main entry point — generates and pushes orders.
void operator()() {
uint64_t next_id = 1;
for (std::size_t i = 0; i < total_orders_; ++i) {
if (!stats_.running.load(std::memory_order_relaxed))
break;
double roll = dist_uniform_(rng_);
OrderMessage msg{};
if (roll < config::LIMIT_ORDER_RATIO) {
// ── Limit Order ──
Order *order = order_pool_.acquire();
if (!order) [[unlikely]] {
stats_.pool_exhausted_count.fetch_add(1, std::memory_order_relaxed);
continue;
}
fill_limit_order(order, next_id++);
msg.type = OrderType::LIMIT;
msg.order = order;
} else if (roll <
config::LIMIT_ORDER_RATIO + config::MARKET_ORDER_RATIO) {