Skip to content

Latest commit

 

History

History
323 lines (270 loc) · 6.31 KB

architecture.md

File metadata and controls

323 lines (270 loc) · 6.31 KB

AbyssBook Technical Architecture

Core Components

1. Sharded Orderbook

The orderbook is horizontally sharded by price levels, enabling parallel processing and reducing contention:

pub const ShardedOrderbook = struct {
    shards: []OrderMap,
    bid_levels: []PriceLevelMap,
    ask_levels: []PriceLevelMap,
    shard_count: usize,
};

Key features:

  • Price-based sharding for optimal distribution
  • Independent shard processing
  • Lock-free operations within shards
  • Dynamic shard rebalancing

2. SIMD-Optimized Matching Engine

Utilizes CPU vector instructions for bulk order processing:

const VECTOR_WIDTH = if (builtin.cpu.arch == .x86_64) 
    @as(usize, 8) else @as(usize, 4);

const VectorizedMatch = struct {
    prices: [VECTOR_WIDTH]u64 align(32),
    amounts: [VECTOR_WIDTH]u64 align(32),
    results: [VECTOR_WIDTH]MatchResult align(32),
};

Optimizations:

  • Vectorized price comparisons
  • Batch order matching
  • SIMD-friendly memory layout
  • Automatic CPU feature detection

3. Cache-Optimized Data Structures

All critical data structures are cache-aligned and optimized:

const CACHE_LINE_SIZE = 64;
const MAX_ORDERS_PER_BATCH = 128;
const PREFETCH_DISTANCE = 8;

const PriceLevelCache = struct {
    levels: [PRICE_LEVEL_CACHE_SIZE]PriceLevel align(CACHE_LINE_SIZE),
    head: usize align(CACHE_LINE_SIZE),
    tail: usize align(CACHE_LINE_SIZE),
};

Features:

  • Cache line alignment
  • Predictive prefetching
  • Circular buffers
  • Zero-copy operations

4. Advanced Order Types

TWAP Orders

Time-Weighted Average Price execution:

pub const TWAPParams = struct {
    total_amount: u64,
    interval_seconds: u64,
    num_intervals: u64,
    start_time: i64,
    amount_per_interval: u64,
};

Trailing Stop Orders

Dynamic stop price adjustment:

pub const TrailingStopParams = struct {
    distance: u64,
    last_trigger_price: u64,
    current_stop_price: u64,
};

Peg Orders

Reference price tracking:

pub const PegParams = struct {
    peg_type: PegType,
    offset: i64,
    limit_price: ?u64,
};

5. Performance Monitoring

Built-in performance tracking:

pub const SIMDMetrics = struct {
    vector_operations: usize,
    scalar_operations: usize,
    cache_misses: usize,
    start_time: i128,
    end_time: i128,
};

Metrics tracked:

  • SIMD utilization
  • Cache efficiency
  • Latency distribution
  • Throughput rates

6. Solana Integration

Native Solana program integration:

pub const Processor = struct {
    state: *OrderbookState,
    account_data: []u8,
    clock: i64,
    state_manager: *StateManager,
};

Features:

  • Account management
  • Token transfers
  • Fee collection
  • Event emission

System Design

1. Memory Management

  • Zero-copy architecture
  • Cache-aligned structures
  • Memory pooling
  • Prefetching optimization

2. Concurrency Model

  • Lock-free operations
  • Atomic updates
  • SIMD parallelization
  • Sharded processing

3. Performance Optimization

  • Vectorized operations
  • Cache optimization
  • Batch processing
  • Predictive loading

4. Market Making Support

  • Bulk order operations
  • Sub-tick pricing
  • Advanced order types
  • Real-time updates

Implementation Details

1. Order Matching

fn matchOrderOptimized(
    self: *ShardedOrderbook,
    side: OrderSide,
    price: u64,
    amount: u64
) !MatchResult {
    var monitor = MatchingMonitor.init();
    var level_batch = PriceLevelBatch.init();
    
    // Process each shard with SIMD operations
    for (0..self.shard_count) |shard_index| {
        const levels = if (side == .Buy)
            &self.ask_levels[shard_index]
        else
            &self.bid_levels[shard_index];
            
        // Vectorized matching logic
    }
}

2. Price Level Management

fn processLevelBatchSIMD(
    batch: *PriceLevelBatch,
    remaining_amount: *u64,
    total_filled: *u64,
    last_price: *u64,
    side: OrderSide,
    monitor: *MatchingMonitor,
) !void {
    // SIMD-optimized price level processing
}

3. Settlement Processing

fn processSettleFunds(self: *Processor) !void {
    // Get pending matches
    var matches = try self.getPendingMatches();
    
    // Process each match atomically
    for (matches.items) |match| {
        // Calculate amounts
        const base_amount = match.quantity;
        const quote_amount = match.quantity * match.price;
        
        // Transfer tokens and collect fees
    }
}

Performance Considerations

1. SIMD Optimization

  • Use of CPU vector instructions
  • Batch processing of orders
  • Vectorized price comparisons
  • SIMD-friendly memory layout

2. Cache Optimization

  • Cache line alignment
  • Prefetching
  • Minimal cache misses
  • Efficient data structures

3. Memory Management

  • Zero-copy operations
  • Memory pooling
  • Efficient allocation
  • Cache-friendly layout

4. Concurrency

  • Lock-free algorithms
  • Atomic operations
  • Parallel processing
  • Sharding strategy

Future Optimizations

1. Hardware Acceleration

  • GPU acceleration
  • FPGA integration
  • Custom ASIC support
  • Network optimization

2. Advanced Features

  • Cross-chain settlement
  • Layer 2 integration
  • Custom order types
  • Enhanced analytics

3. Performance Enhancements

  • Dynamic sharding
  • Adaptive prefetching
  • ML-based optimization
  • Custom memory allocators

Security Considerations

1. Order Validation

  • Price bounds checking
  • Size validation
  • Account verification
  • Duplicate prevention

2. Settlement Safety

  • Atomic operations
  • Transaction validation
  • Balance verification
  • Error handling

3. System Protection

  • Rate limiting
  • DOS prevention
  • Error recovery
  • State consistency

Integration Guidelines

1. Initialization

// Initialize orderbook with optimal shard count
var book = try ShardedOrderbook.init(
    allocator,
    getCPUCoreCount() * 2
);

2. Order Placement

// Place order with automatic sharding
try book.placeOrder(
    side: OrderSide,
    price: u64,
    amount: u64,
    order_id: u64
);

3. Market Orders

// Execute market order with SIMD matching
const result = try book.executeMarketOrder(
    side: OrderSide,
    amount: u64
);

4. Advanced Orders

// Place TWAP order
try book.placeTWAPOrder(
    side: OrderSide,
    price: u64,
    total_amount: u64,
    num_intervals: u64,
    interval_seconds: u64
);