# philipgreat/lighting-match-engine-core architecture

> Architecture diagram of the GitHub repository philipgreat/lighting-match-engine-core, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/philipgreat/lighting-match-engine-core
- Repository: https://github.com/philipgreat/lighting-match-engine-core

## Overview

#### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Native Runtime"]
  node_main["CLI executable<br/>Rust binary<br/>[main.rs]"]
  node_config["AppConfig<br/>configuration<br/>[mod.rs]"]
end

subgraph group_engine["Matching Engine"]
  node_domain["Order domain<br/>domain model<br/>[data.rs]"]
  node_engine_state{{"EngineState<br/>engine facade<br/>[engine_state.rs]"}}
  node_book_factory["Book factory<br/>runtime selector<br/>[factory.rs]"]
  node_book_contract["Order-book abstraction<br/>book interface<br/>[mod.rs]"]
  node_dense_book["Dense order book<br/>price-level book<br/>[dense.rs]"]
  node_sparse_book["Sparse order book<br/>price-level book<br/>[sparse.rs]"]
end

subgraph group_session["Session Control"]
  node_auction_pool["Call auction pool<br/>auction book"]
  node_session_runner{{"Session runner<br/>phase orchestrator<br/>[session_runner.rs]"}}
  node_system["Demo session integration<br/>system module<br/>[mod.rs]"]
end

subgraph group_protocol["Protocol"]
  node_protocol_codec["Fixed-packet codec<br/>serialization<br/>[codec.rs]"]
  node_protocol_messages["Protocol messages<br/>message definitions<br/>[mod.rs]"]
end

subgraph group_performance["Performance"]
  node_timer["High-resolution timer<br/>timing<br/>[high_resolution.rs]"]
  node_perf_stats["Benchmark statistics<br/>measurement<br/>[perf.rs]"]
  node_stats_output["Statistics output<br/>reporting<br/>[output.rs]"]
  node_cpu_affinity["CPU affinity<br/>runtime tuning<br/>[cpu_affinity.rs]"]
end

node_main -->|"builds"| node_config
node_main -->|"runs demo session"| node_system
node_main -->|"benchmarks"| node_perf_stats
node_main -->|"reports"| node_stats_output
node_main -.->|"optionally pins"| node_cpu_affinity
node_system -->|"integrates"| node_session_runner
node_config -->|"selects book"| node_book_factory
node_book_factory -->|"creates implementation"| node_book_contract
node_dense_book -->|"implements"| node_book_contract
node_sparse_book -->|"implements"| node_book_contract
node_engine_state -->|"owns selected book"| node_book_contract
node_engine_state -->|"processes"| node_domain
node_session_runner -->|"continuous matching"| node_engine_state
node_session_runner -->|"routes auction orders"| node_auction_pool
node_auction_pool -->|"auction outcomes"| node_engine_state
node_auction_pool -->|"accepts limit orders"| node_domain
node_protocol_codec -->|"encodes"| node_protocol_messages
node_protocol_codec -.->|"serializes outcomes"| node_domain
node_perf_stats -->|"measures with"| node_timer
node_perf_stats -.->|"times matching"| node_engine_state
node_stats_output -->|"formats"| node_perf_stats

click node_main "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/main.rs"
click node_config "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/config/mod.rs"
click node_domain "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/types/data.rs"
click node_engine_state "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/types/engine_state.rs"
click node_book_factory "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/orderbook/factory.rs"
click node_book_contract "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/orderbook/mod.rs"
click node_dense_book "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/orderbook/dense.rs"
click node_sparse_book "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/orderbook/sparse.rs"
click node_auction_pool "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/orderbook/call_auction_pool.rs"
click node_session_runner "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/system/session_runner.rs"
click node_system "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/system/mod.rs"
click node_protocol_codec "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/protocol/codec.rs"
click node_protocol_messages "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/protocol/mod.rs"
click node_timer "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/timer/high_resolution.rs"
click node_perf_stats "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/stats/perf.rs"
click node_stats_output "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/stats/output.rs"
click node_cpu_affinity "https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/system/cpu_affinity.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_config toneBlue
class node_domain,node_engine_state,node_book_factory,node_book_contract,node_dense_book,node_sparse_book toneAmber
class node_auction_pool,node_session_runner,node_system toneMint
class node_protocol_codec,node_protocol_messages toneRose
class node_timer,node_perf_stats,node_stats_output,node_cpu_affinity toneIndigo
```

## Components

### Native Runtime

- [CLI executable](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/main.rs): Rust binary · Parses options, seeds orders, drives flows, and reports results. · `src/main.rs`
- [AppConfig](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/config/mod.rs): configuration · Builds runtime configuration from CLI flags and environment settings. · `src/config/mod.rs`

### Matching Engine

- [Order domain](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/types/data.rs): domain model · Orders, sides, price modes, trades, and matching data. · `src/types/data.rs`
- [EngineState](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/types/engine_state.rs): engine facade · Owns the active book and exposes order processing and matching. · `src/types/engine_state.rs`
- [Book factory](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/orderbook/factory.rs): runtime selector · Creates one concrete in-memory book for the configured product. · `src/orderbook/factory.rs`
- [Order-book abstraction](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/orderbook/mod.rs): book interface · `src/orderbook/mod.rs`
- [Dense order book](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/orderbook/dense.rs): price-level book · In-memory representation for dense price spaces. · `src/orderbook/dense.rs`
- [Sparse order book](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/orderbook/sparse.rs): price-level book · In-memory representation for sparse price spaces. · `src/orderbook/sparse.rs`

### Session Control

- [Call auction pool](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/orderbook/call_auction_pool.rs): auction book · Pools eligible limit orders for auction matching. · `src/orderbook/call_auction_pool.rs`
- [Session runner](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/system/session_runner.rs): phase orchestrator · Routes orders across opening auction, continuous trading, and optional closing auction. · `src/system/session_runner.rs`
- [Demo session integration](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/system/mod.rs): system module · `src/system/mod.rs`

### Protocol

- [Fixed-packet codec](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/protocol/codec.rs): serialization · Encodes submit, cancel, trade, statistics, and error messages into 64-byte packets. · `src/protocol/codec.rs`
- [Protocol messages](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/protocol/mod.rs): message definitions · `src/protocol/mod.rs`

### Performance

- [High-resolution timer](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/timer/high_resolution.rs): timing · `src/timer/high_resolution.rs`
- [Benchmark statistics](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/stats/perf.rs): measurement · Collects matching latency and benchmark results. · `src/stats/perf.rs`
- [Statistics output](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/stats/output.rs): reporting · `src/stats/output.rs`
- [CPU affinity](https://github.com/philipgreat/lighting-match-engine-core/blob/main/src/system/cpu_affinity.rs): runtime tuning · Pins the matching thread to an available CPU core. · `src/system/cpu_affinity.rs`

## Connections

- 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
