Leka
A low-latency C++20 price-time-priority limit order book and matching engine
▶ Replay viewer
Loading...
Searching...
No Matches

Provides stable, pooled storage for Order objects. More...

#include <order_pool.hpp>

Classes

struct  Page

Public Member Functions

 OrderPool ()
 Creates an empty pool without allocating a page.
 OrderPool (OrderPool &&)=delete
OrderPooloperator= (OrderPool &&)=delete
 OrderPool (const OrderPool &)=delete
OrderPooloperator= (const OrderPool &)=delete
 ~OrderPool ()
 Destroys all live orders and releases every allocated page.
Orderallocate (OrderId orderId, Price price, Quantity originalQuantity, Timestamp timestamp, OrderSide orderSide, OrderType orderType, SequenceNumber sequenceNumber)
 Constructs an Order in a stable pool slot.
Orderallocate (OrderId orderId, Price price, Quantity originalQuantity, Quantity remainingQuantity, Timestamp timestamp, OrderSide orderSide, OrderType orderType, SequenceNumber sequenceNumber)
 Constructs an Order with explicit original and remaining quantities.
void release (Order *order)
 Destroys an order and returns its slot to the free list.
void reserve (std::size_t orderCount)
 Pre-allocates enough pages to hold at least orderCount live orders without allocate() creating a page on its own.
std::size_t getPageCount () const
 Returns the number of allocated pages.
std::size_t getLiveOrderCount () const
 Returns the number of live orders in the pool.

Static Public Attributes

static constexpr std::size_t PageSize = 64 * 1024
static constexpr std::size_t SlotsPerPage = PageSize / sizeof(Order)

Detailed Description

Provides stable, pooled storage for Order objects.

Storage is allocated in fixed 64 KiB pages. Orders are constructed in place, never compacted, and released slots are reused without moving live orders.

Definition at line 23 of file order_pool.hpp.

Constructor & Destructor Documentation

◆ OrderPool()

lob::OrderPool::OrderPool ( )
default

Creates an empty pool without allocating a page.

References allocate(), OrderPool(), release(), and reserve().

Referenced by OrderPool().

◆ ~OrderPool()

lob::OrderPool::~OrderPool ( )

Destroys all live orders and releases every allocated page.

Definition at line 75 of file order_pool.cpp.

Member Function Documentation

◆ allocate() [1/2]

Order * lob::OrderPool::allocate ( OrderId orderId,
Price price,
Quantity originalQuantity,
Timestamp timestamp,
OrderSide orderSide,
OrderType orderType,
SequenceNumber sequenceNumber )

Constructs an Order in a stable pool slot.

Returns
A pointer owned by this pool until release() is called.

Definition at line 137 of file order_pool.cpp.

References allocate().

Referenced by allocate(), and OrderPool().

◆ allocate() [2/2]

Order * lob::OrderPool::allocate ( OrderId orderId,
Price price,
Quantity originalQuantity,
Quantity remainingQuantity,
Timestamp timestamp,
OrderSide orderSide,
OrderType orderType,
SequenceNumber sequenceNumber )

Constructs an Order with explicit original and remaining quantities.

Definition at line 144 of file order_pool.cpp.

◆ release()

void lob::OrderPool::release ( Order * order)

Destroys an order and returns its slot to the free list.

Releases a live slot after validating pool ownership and allocation state.

Definition at line 183 of file order_pool.cpp.

Referenced by OrderPool().

◆ reserve()

void lob::OrderPool::reserve ( std::size_t orderCount)

Pre-allocates enough pages to hold at least orderCount live orders without allocate() creating a page on its own.

A page is created lazily, on the first allocate() call that finds no free slot, via mmap — a syscall with a genuinely unbounded tail (page fault handling, kernel scheduling), which is why it shows up as the multi-order-of-magnitude outliers in a latency histogram (see ARCH_DECISIONS.md ADR-008). Calling this once, before trading begins, for a known or comfortably over-estimated maximum order count moves every one of those mmap calls out of the hot path entirely, the same trade PriceLadder already makes for price levels: pay a bounded, known cost once, in exchange for a flat tail afterward. Exceeding orderCount still falls back to the lazy per-allocation behavior rather than failing.

Only ever creates pages, never removes them: reserving a smaller count than a previous call is a no-op rather than shrinking capacity that may already hold live orders.

Definition at line 175 of file order_pool.cpp.

Referenced by OrderPool().

◆ getPageCount()

std::size_t lob::OrderPool::getPageCount ( ) const
inline

Returns the number of allocated pages.

Definition at line 71 of file order_pool.hpp.

◆ getLiveOrderCount()

std::size_t lob::OrderPool::getLiveOrderCount ( ) const
inline

Returns the number of live orders in the pool.

Definition at line 73 of file order_pool.hpp.

Member Data Documentation

◆ PageSize

std::size_t lob::OrderPool::PageSize = 64 * 1024
staticconstexpr

Definition at line 25 of file order_pool.hpp.

◆ SlotsPerPage

std::size_t lob::OrderPool::SlotsPerPage = PageSize / sizeof(Order)
staticconstexpr

Definition at line 26 of file order_pool.hpp.


The documentation for this class was generated from the following files: