Module generic_pool

Source
Expand description

Generic high-performance object pool for HFT applications

This module provides a generic object pool implementation designed for high-frequency trading where object allocation overhead can destroy sub-microsecond latency requirements. Uses generics to support any type while maintaining type safety and performance.

§HFT Performance Rationale

§Allocation Overhead in Trading Systems

Object allocation is a critical bottleneck in HFT applications:

  • Dynamic allocation: 50-200ns per malloc/free operation
  • Constructor overhead: Complex object initialization time
  • Destructor costs: Cleanup and memory deallocation
  • Memory fragmentation: Degrades cache performance over time
  • GC pressure: Languages with GC suffer unpredictable pauses

§Critical Path Objects

Common HFT objects that benefit from pooling:

  • Order structures: New/cancel/modify order messages
  • Trade records: Execution confirmations and market data
  • Market data snapshots: Level 2 order book states
  • Strategy signals: Alpha generation and risk calculations
  • Message buffers: Network I/O and protocol handling

§Generic Pool Architecture

§Type Safety with Performance

  • Generic implementation: Works with any Poolable + Clone type
  • Compile-time optimization: Monomorphization eliminates virtual calls
  • Zero-cost abstractions: Generic overhead eliminated at compile time
  • Type-specific pools: Each type gets its own optimized pool instance

§Memory Layout Optimization

Pool Structure:
┌─ RwLock<SmallVec<[T; 32]>> ─┐    ← Stack-allocated for small pools
│ ┌─ T ─┐ ┌─ T ─┐ ┌─ T ─┐    │    ← Pre-allocated objects
│ │obj1│ │obj2│ │obj3│ ...│    │    ← Ready for immediate use
│ └─────┘ └─────┘ └─────┘    │
└──────────────────────────────┘

§SmallVec Optimization

Uses SmallVec<[T; 32]> for storage:

  • Stack allocation: Small pools avoid heap allocation
  • Cache efficiency: Contiguous memory layout
  • Growth capability: Expands to heap when needed
  • SIMD-friendly: Aligned access patterns when possible

§Poolable Trait Design

§Safe Pool Management

pub trait Poolable {
    fn new_for_pool() -> Self;     // Create invalid/empty object
    fn reset_for_pool(&mut self);  // Reset to poolable state
}

§Security & Safety

  • Invalid state creation: new_for_pool creates obviously invalid objects
  • Data sanitization: reset_for_pool clears sensitive information
  • Memory safety: No unsafe code, guaranteed Rust safety
  • Type enforcement: Compile-time checks prevent misuse

§Performance Characteristics

§Latency Metrics

  • Object borrowing: 5-20ns (RwLock read + pop operation)
  • Object return: 10-30ns (reset + RwLock write + push)
  • Pool warmup: One-time cost during initialization
  • Cache misses: ~100ns when pool cold or oversized

§Throughput Optimization

  • High concurrency: RwLock allows multiple concurrent readers
  • Batch processing: Warmup function for optimal memory layout
  • Memory reuse: Eliminates allocation/deallocation overhead

§Thread Safety Model

§RwLock-Based Synchronization

  • Read-heavy optimization: Multiple concurrent borrowers
  • Write serialization: Returns are serialized but typically brief
  • Parking lot RwLock: Faster than std::sync with better performance
  • Lock-free alternatives: Consider for ultra-low latency needs

§Usage Patterns

// High-frequency pattern
let obj = pool.borrow();  // Fast read lock
// ... use object ...
pool.return_object(obj);  // Brief write lock

§Integration with HFT Systems

§Trading Engine Integration

  • Order management: Pool order structures for rapid reuse
  • Market data: Pool snapshot objects for reduced allocation
  • Strategy objects: Pool calculation buffers and intermediate results
  • Network buffers: Pool message structures for protocol handling

§Performance Monitoring

Built-in statistics for optimization:

  • Borrow/return counts: Pool utilization metrics
  • Empty pool events: Allocation fallback frequency
  • Peak usage tracking: Capacity planning data
  • Allocation timing: High-precision latency measurement

Structs§

GenericPool
Generic high-performance object pool optimized for HFT latency requirements
PoolFactory
Thread-safe global pool factory
PoolStats
Pool performance statistics

Traits§

Poolable
Trait for objects that can be safely used in object pools