Expand description
Unified zero-copy utilities for high-performance operations
This module provides comprehensive zero-copy data structures and utilities optimized for high-frequency trading applications where memory allocation and copying overhead can destroy microsecond-level latency requirements.
§HFT Performance Rationale
§Memory Allocation Costs
In HFT systems, dynamic allocation is prohibitively expensive:
- malloc/free overhead: 50-200ns per allocation
- Memory fragmentation: Degrades cache performance over time
- GC pressure: Garbage collection pauses can last milliseconds
- NUMA effects: Cross-node allocation adds 100+ ns latency
- TLB pressure: Virtual memory overhead affects page table lookups
§Data Copy Elimination
Zero-copy operations provide critical performance benefits:
- Memory bandwidth: Eliminates unnecessary data movement
- Cache efficiency: Keeps data in L1/L2 cache longer
- CPU utilization: Frees up CPU cycles for trading logic
- Latency reduction: Removes copy operations from critical path
§Unified Buffer Architecture
§Multi-Purpose Buffer Management
UnifiedBufferManager
│
├── Text Buffer (8KB) ─── String operations, symbol parsing
├── Binary Buffer (64KB) ─── Protocol messages, serialization
├── JSON Parse Buffer (8KB) ─── Market data parsing
├── JSON Serialize (4KB) ─── Order message creation
├── JSON Large Buffer (64KB)─── Batch operations
└── String Format (256B) ─── ID generation, logging§Buffer Size Optimization
Buffer sizes are tuned for typical HFT message patterns:
- Text operations: 8KB handles most symbol/ID operations
- Binary protocols: 64KB accommodates FIX messages and batch data
- JSON parsing: 8KB covers typical exchange WebSocket messages
- JSON serialization: 4KB handles order placement messages
- Large JSON operations: 64KB for order book snapshots
- String formatting: 256B for order IDs and timestamps
§Zero-Copy Patterns
§simd-json Integration
- In-place parsing: Modifies input buffer directly
- Borrowed values: References into original buffer
- SIMD acceleration: 2-10x faster than traditional JSON parsers
- Minimal allocation: Only allocates for complex nested structures
§String Operations
- SmartString optimization: Stack allocation for short strings
- Reference-based parsing: Avoids string copying when possible
- Symbol interning: Reuses common trading symbols
§Thread-Local Optimization
§Per-Thread Buffer Pools
thread_local! {
static BUFFER_MANAGER: RefCell<UnifiedBufferManager> = /* ... */;
}Benefits:
- Lock-free access: No synchronization overhead
- Cache affinity: Buffers stay warm in thread-local cache
- Memory locality: Reduces cache misses and false sharing
§RAII Buffer Management
buffer_manager.process_with_text_buffer(|buffer| {
// Use buffer for processing
// Automatically cleared on return
});§Performance Characteristics
§Latency Improvements
- Buffer reuse: 0ns allocation cost in steady state
- Zero-copy parsing: 50-80% reduction in JSON parsing time
- Cache efficiency: Hot buffers remain in CPU cache
- Memory bandwidth: Eliminates unnecessary data copies
§Memory Efficiency
- Predictable usage: Fixed buffer sizes prevent unbounded growth
- Reduced fragmentation: Buffer reuse eliminates heap fragmentation
- Lower memory pressure: Reduces overall heap allocation
§Integration Patterns
§Market Data Processing
// Zero-copy JSON parsing
buffer_manager.process_with_json_buffer(|buffer| {
let parsed = simd_json::to_borrowed_value(buffer)?;
// Process without additional allocation
});§Order Management
// Zero-allocation order serialization
buffer_manager.process_with_serialize_buffer(|buffer| {
simd_json::to_writer(buffer, &order_message)?;
// Send buffer contents directly
});§Safety & Reliability
§Memory Safety
- Rust ownership: Prevents use-after-free and buffer overruns
- Bounds checking: Debug builds include comprehensive bounds checks
- Lifetime management: Borrowed references cannot outlive source data
§Error Handling
- Graceful degradation: Falls back to allocation on buffer exhaustion
- Error propagation: Proper error handling throughout the stack
- Monitoring hooks: Built-in metrics for buffer utilization
Structs§
- Exchange
Json Zero Copy - Zero-copy JSON utilities specifically for exchange API responses
- Unified
Buffer Manager - Unified buffer manager optimized for HFT zero-copy operations
- Unified
Buffer Stats - Statistics for unified buffer usage
- Unified
String Utils - Comprehensive string utilities combining both modules’ functionality
- WebSocket
Json Zero Copy - Zero-copy JSON utilities specifically for WebSocket messages
- Zero
Copy Collections - Zero-copy collection utilities
- Zero
Copy Json - Zero-copy JSON parsing utilities
- Zero
Copy Message - Zero-copy message wrapper for efficient processing
- Zero
Copy Slice - Zero-copy slice wrapper that avoids unnecessary allocations
Traits§
- Borrowed
Value Ext - Extension trait for zero-copy extraction from BorrowedValue
Functions§
- with_
buffer_ manager - Backward compatibility function
- with_
json_ buffer_ manager - Backward compatibility function
- with_
unified_ buffer_ manager - Get access to the thread-local unified buffer manager
Type Aliases§
- Buffered
String Ops - A type alias for
UnifiedStringUtils. - Json
Buffer Manager - A type alias for
UnifiedBufferManager. - Zero
Copy Buffer Manager - A type alias for
UnifiedBufferManager. - Zero
Copy String Utils - A type alias for
UnifiedStringUtils.