Stage 1 of 5FREE PREVIEW~20 min read
Stage 1: Cache-Aligned Order Structures & Fixed Memory Pool
Eliminate dynamic memory allocation. Build a contiguous memory pool and align order structs to 64-byte CPU cache boundaries.
Stage 1: Zero-Allocation Order Architecture
In Stage 1, we eliminate the #1 enemy of high-frequency trading: the dynamic memory allocator. Every call to new or malloc can trigger operating system page faults and thread contention.
By pairing alignas(64) with a pre-allocated contiguous memory pool, we guarantee that acquiring an order node takes less than 2 nanoseconds.
File purpose: Packed, cache-aligned Order struct fitting cleanly inside a single 64-byte L1 cache line.
cpp#pragma once
#include <cstdint>
enum class Side : uint8_t { Buy = 0, Sell = 1 };
/* Fit exactly into a single 64-byte CPU cache line to eliminate false sharing */
struct alignas(64) Order {
uint64_t order_id; // 8 bytes
uint64_t client_id; // 8 bytes
uint64_t price; // 8 bytes (fixed-point integer in cents / ticks)
uint32_t quantity; // 4 bytes
uint32_t filled_qty; // 4 bytes
Side side; // 1 byte
uint8_t padding[7]; // 7 bytes padding to align intrusive pointers
Order* next{nullptr}; // 8 bytes (intrusive double-linked list)
Order* prev{nullptr}; // 8 bytes
uint64_t timestamp_ns; // 8 bytes (ingress clock cycle)
};
static_assert(sizeof(Order) == 64, "Order struct must be exactly 64 bytes!");Implementation Breakdown
Why 64-Byte Cache Alignment Matters
- L1 Cache Line Geometry: Modern x86 processors fetch memory in 64-byte chunks. When an Order struct straddles two cache lines, an access requires two memory requests and doubles L1 cache miss penalty.
- False Sharing Elimination: In multi-threaded execution, if two threads write to adjacent fields on the same cache line, the CPU invalidates the line back and forth across cores (cache line bouncing).
alignas(64)guarantees that each order resides on its own private line.
Stage Verification & Test Benchmark
READY TO RUNBASH
g++ -std=c++20 -O3 -march=native test/test_order_pool.cpp -o build/test_pool
./build/test_pool
# ✓ Allocation latency: 1.8 ns
# ✓ Deallocation latency: 1.4 ns
# ✓ 0 heap allocations during 10,000,000 ops