A clean-room HFT matching engine achieving 641ns minimum latency through flat-array storage, object pooling, and zero-copy networking.
Core components designed for maximum performance
Prices converted to integer ticks enable direct array indexing. bids[10005] represents all orders at $100.05.
Pre-allocated 100,000 Order objects. Acquire/release pattern eliminates malloc/free during trading sessions.
Raw UDP sockets with packed binary structs. Direct network-to-engine data flow without deserialization overhead.
Strong typing for Prices and Quantities prevents mixed-unit arithmetic errors at compile time.
Measured on consumer laptop (Linux/GCC -O3)
Code snippets highlighting technical excellence
// Traditional: O(log N) with std::map
// std::map<double, std::list<Order*>, std::greater<double>> bids;
// Jupiter: O(1) with flat-array indexing
std::vector<std::list<Order*>> bids; // Size: 1,000,000
std::vector<std::list<Order*>> asks;
// Price to tick conversion: $100.05 → 10005
int tick = (int)(price * 100);
// Direct O(1) access
bids[tick].push_back(newOrder);
max_bid_tick = std::max(max_bid_tick, tick);
template<typename T>
template<typename... Args>
T* Order_Pool<T>::acquire(Args&&... args) {
if(free_list.size() == 0) {
throw std::runtime_error("Order Book Size Exhausted");
}
T* ptr = free_list.back();
free_list.pop_back();
// Placement new: construct in pre-allocated memory
new (ptr) T(std::forward<Args>(args)...);
return ptr;
}
void Order_Pool<T>::release(T* obj) {
obj->~T(); // Manual destructor call
free_list.push_back(obj); // Return to pool
}
// Packed binary struct (no padding)
#pragma pack(push, 1)
class OrderMessage {
public:
MessageHeader header;
uint32_t orderID;
uint32_t price; // Network byte order (Big Endian)
uint32_t quantity;
char side; // 'B' or 'S'
};
#pragma pack(pop)
// Zero-copy deserialization
OrderMessage* msg = reinterpret_cast<OrderMessage*>(buffer);
// Network to host byte order conversion
uint32_t id = ntohl(msg->orderID);
uint32_t price_ticks = ntohl(msg->price);
uint32_t quantity = ntohl(msg->quantity);
Port to DPDK or Solarflare OpenOnload to skip Linux kernel stack and achieve even lower latencies.
Implement SPSC ring buffer to decouple network and matching threads for higher throughput.
Pin engine thread to dedicated CPU core with isolcpus to reduce context switching jitter.
Implement snapshot mechanism to rebuild order book state if UDP packets are dropped.