Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zero-Allocation Limit Order Book

A limit order book and matching engine in Rust for high-frequency trading. Processes 44 million orders per second on Apple M4.

Features

  • Price-time priority matching
  • 64-byte cache-aligned order structs
  • Pre-allocated slab memory (16 million orders, 1GB)
  • Lock-free SPSC ring buffer for message passing
  • Zero heap allocation on the hot path

Build

cargo build --release

Test

cargo test

Benchmark

cargo bench

Performance

Measured on Apple M4:

Metric Result
Throughput 44.3 million orders/sec
Order struct size 64 bytes
Slab capacity 16,777,216 orders

Project Structure

src/
├── lib.rs           Module exports
├── order.rs         Order struct (64-byte aligned)
├── slab.rs          Pre-allocated memory arena
├── price_level.rs   Price level aggregation
├── orderbook.rs     Bid/ask matching logic
├── ring_buffer.rs   Lock-free SPSC queue
└── engine.rs        Matching engine

Technical Details

Order Layout

The Order struct fits exactly in one cache line:

Field Type Bytes
id u64 8
price u64 8
quantity u64 8
remaining u64 8
timestamp u64 8
next u32 4
prev u32 4
side u8 1
order_type u8 1
padding [u8; 6] 6
Total 64

Slab Allocator

Pre-allocates all memory at startup. Uses 32-bit indices instead of 64-bit pointers. Lock-free allocation via compare-and-swap.

Ring Buffer

Single-producer single-consumer queue with Release/Acquire memory fences. Cache-padded indices prevent false sharing.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages