pub struct ZeroAllocParser { /* private fields */ }Expand description
Zero-allocation message parser with lock-free data structures
Combines pre-allocated buffer pools with lock-free caching for maximum performance in HFT market data processing. Designed for sub-microsecond JSON parsing latency.
§Architecture Components
- Type cache: Lock-free message type recognition using DashMap
- Buffer pool: Mutex-protected buffer recycling (minimal contention)
- Statistics: Atomic counters for lock-free performance monitoring
- Configuration: Tunable buffer sizes and pool limits
§Usage Patterns
- High-frequency parsing: Use
parse_message_with_closurefor zero-copy processing - Persistent results: Use
parse_messagewhen data must outlive the parsing call - Thread-local access: Use global functions for maximum performance
Implementations§
Source§impl ZeroAllocParser
impl ZeroAllocParser
Sourcepub fn with_capacity(initial_buffers: usize, max_buffer_size: usize) -> Self
pub fn with_capacity(initial_buffers: usize, max_buffer_size: usize) -> Self
Create parser with specific buffer settings
Sourcepub fn parse_message(
&self,
text: &str,
) -> Result<(MessageType, OwnedValue), &'static str>
pub fn parse_message( &self, text: &str, ) -> Result<(MessageType, OwnedValue), &'static str>
Parse WebSocket message with zero allocations when possible
Returns the parsed message type and owned JSON value. Uses thread-local buffers and caching for maximum performance.
Sourcepub fn parse_message_with_closure<T, F>(
&self,
text: &str,
f: F,
) -> Result<T, &'static str>where
F: FnOnce(MessageType, &BorrowedValue<'_>) -> T,
pub fn parse_message_with_closure<T, F>(
&self,
text: &str,
f: F,
) -> Result<T, &'static str>where
F: FnOnce(MessageType, &BorrowedValue<'_>) -> T,
Parse WebSocket message with zero allocations using borrowed values
The closure receives a BorrowedValue that is only valid within the closure’s lifetime. This method is more efficient than parse_message() for cases where the parsed data doesn’t need to outlive the parsing call.
§Safety
The BorrowedValue passed to the closure is only valid within the closure’s execution. It must not be stored or used after the closure returns.
Sourcepub fn stats(&self) -> ParserStats
pub fn stats(&self) -> ParserStats
Get current parser statistics
Sourcepub fn reset_stats(&self)
pub fn reset_stats(&self)
Reset parser statistics
Sourcepub fn extract_symbol<'a>(&self, json: &'a OwnedValue) -> Option<&'a str>
pub fn extract_symbol<'a>(&self, json: &'a OwnedValue) -> Option<&'a str>
Extract symbol from message (for owned values)
Sourcepub fn extract_timestamp(&self, json: &OwnedValue) -> Option<u64>
pub fn extract_timestamp(&self, json: &OwnedValue) -> Option<u64>
Extract timestamp from message
Sourcepub fn extract_price<'a>(&self, json: &'a OwnedValue) -> Option<&'a str>
pub fn extract_price<'a>(&self, json: &'a OwnedValue) -> Option<&'a str>
Extract price from message
Sourcepub fn extract_quantity<'a>(&self, json: &'a OwnedValue) -> Option<&'a str>
pub fn extract_quantity<'a>(&self, json: &'a OwnedValue) -> Option<&'a str>
Extract quantity from message