How philipgreat/lighting-match-engine-core works
Purpose and executable entry point
This is a Rust, single-product, in-memory matching-engine core. It is primarily a CLI-driven demo/benchmark executable, not a network service: src/main.rs parses runtime options, constructs engine state, seeds orders, drives matching/auction paths, and reports latency/statistics. Build metadata and dependencies are in Cargo.toml and build.rs.
Configuration and engine assembly
src/config/mod.rs converts CLI flags and selected environment settings into AppConfig (product identity, book type, tick/base price, level/order limits, trade cap, benchmark sizing). src/orderbook/factory.rs selects the concrete book at startup, creating a hard runtime boundary: one engine instance owns one selected in-memory book for one product.
Shared domain model and state
Order, price-mode, side, trade, and related matching data are defined in src/types/data.rs. src/types/engine_state.rs owns the active order book and exposes the order-processing/matching surface used by the runner and benchmark. This is the core control boundary: callers submit domain orders; the engine mutates book state and produces match/trade outcomes.
Order-book implementations and matching
The order-book abstraction lives in src/orderbook/mod.rs. Two interchangeable implementations support different price-space characteristics:
• src/orderbook/dense.rs: dense in-memory price-level representation.
• src/orderbook/sparse.rs: sparse price-level representation.
• src/orderbook/call_auction_pool.rs: pools eligible limit orders for auction matching.
Continuous trading applies price/time-priority behavior within the selected book. Market and limit price modes exist in shared types; auction intake is limited to limit orders.
Session and auction orchestration
src/system/session_runner.rs coordinates market-session transitions: opening auction, continuous matching, and optional closing auction. It routes orders between EngineState and the auction pool according to session phase, then executes auction-related flows. src/system/mod.rs provides the demo-session integration used from the executable.
Protocol boundary
src/protocol/codec.rs encodes submit, cancel, trade, statistics, and reject/error messages into fixed 64-byte packets; message definitions are exposed through src/protocol/mod.rs. This is a serialization boundary only: no socket listener, transport loop, OMS, market-data system, or risk service is present.
Performance runtime and observability
High-resolution timing is provided by src/timer/high_resolution.rs and benchmark/reporting code by src/stats/perf.rs and src/stats/output.rs. The executable can use match-timing paths to measure core matching latency. src/system/cpu_affinity.rs can pin the matching thread to an available CPU core, reducing scheduler variability. The operational runtime is a native Rust process; no deployed infrastructure or external service dependency is represented in the repository.
Components 17
Native Runtime
- CLI executable · Parses options, seeds orders, drives flows, and reports results.
src/main.rs - AppConfig · Builds runtime configuration from CLI flags and environment settings.
src/config/mod.rs
Matching Engine
- Order domain · Orders, sides, price modes, trades, and matching data.
src/types/data.rs - EngineState · Owns the active book and exposes order processing and matching.
src/types/engine_state.rs - Book factory · Creates one concrete in-memory book for the configured product.
src/orderbook/factory.rs - Order-book abstraction
src/orderbook/mod.rs - Dense order book · In-memory representation for dense price spaces.
src/orderbook/dense.rs - Sparse order book · In-memory representation for sparse price spaces.
src/orderbook/sparse.rs
Session Control
- Call auction pool · Pools eligible limit orders for auction matching.
src/orderbook/call_auction_pool.rs - Session runner · Routes orders across opening auction, continuous trading, and optional closing auction.
src/system/session_runner.rs - Demo session integration
src/system/mod.rs
Protocol
- Fixed-packet codec · Encodes submit, cancel, trade, statistics, and error messages into 64-byte packets.
src/protocol/codec.rs - Protocol messages
src/protocol/mod.rs
Performance
- High-resolution timer
src/timer/high_resolution.rs - Benchmark statistics · Collects matching latency and benchmark results.
src/stats/perf.rs - Statistics output
src/stats/output.rs - CPU affinity · Pins the matching thread to an available CPU core.
src/system/cpu_affinity.rs
Connections 21
- CLI executable → AppConfig: builds
- CLI executable → Demo session integration: runs demo session
- CLI executable → Benchmark statistics: benchmarks
- CLI executable → Statistics output: reports
- CLI executable → CPU affinity: optionally pins
- Demo session integration → Session runner: integrates
- AppConfig → Book factory: selects book
- Book factory → Order-book abstraction: creates implementation
- Dense order book → Order-book abstraction: implements
- Sparse order book → Order-book abstraction: implements
- EngineState → Order-book abstraction: owns selected book
- EngineState → Order domain: processes
- Session runner → EngineState: continuous matching
- Session runner → Call auction pool: routes auction orders
- Call auction pool → EngineState: auction outcomes
- Call auction pool → Order domain: accepts limit orders
- Fixed-packet codec → Protocol messages: encodes
- Fixed-packet codec → Order domain: serializes outcomes
- Benchmark statistics → High-resolution timer: measures with
- Benchmark statistics → EngineState: times matching
- Statistics output → Benchmark statistics: formats