Expand description
Zero-copy parsing helpers shared across exchanges Zero-copy helpers for exchange message processing
This module provides utilities to eliminate allocations when processing binary WebSocket messages and HTTP responses in exchange providers.
Uses SmallVec to keep small messages on the stack (zero heap allocations) while gracefully handling larger messages.
§HFT Performance Optimizations
§Zero-Allocation Architecture
The module leverages SIMD-accelerated JSON parsing through simd_json and takes
ownership of input buffers, allowing the parser to mutate data directly without
creating copies. This approach:
- Eliminates memory allocations during parsing
- Reduces memory pressure and garbage collection overhead
- Provides better cache locality by reusing the same memory
- Achieves 2-10x faster parsing compared to traditional JSON parsers
§Performance Characteristics
- Time Complexity: O(n) where n is the JSON length
- Memory Complexity: O(1) additional allocation (zero-copy parsing)
- SIMD Acceleration: Automatically uses AVX2/SSE4.2 instructions when available
- Cache Efficiency: In-place parsing improves CPU cache utilization
- Latency: Sub-microsecond parsing for typical market data messages (<1KB)
§Comparison with Alternatives
| Method | Allocations | Performance | Memory Usage |
|---|---|---|---|
serde_json::from_slice | 1-2 copies | Baseline (1x) | 2-3x input size |
simd_json::from_slice | 1 copy | 2-3x faster | 1.5x input size |
VecJsonExt::parse_json | 0 copies | 2-10x faster | 1x input size |
§Safety Considerations
This module is memory-safe and does not use any unsafe code. The zero-allocation
approach is achieved through Rust’s ownership system:
- Input buffers are consumed (moved), preventing use-after-free
- SIMD-JSON validates all input and handles malformed data gracefully
- No raw pointers or manual memory management is involved
- Thread-safe when used in single-threaded contexts
§HFT Usage Guidelines
- Use
VecJsonExtfor all incoming WebSocket messages in trading systems - Combine with
SmallVecfor messages <4KB to eliminate heap allocation entirely - Consider message pooling for even better performance in high-throughput scenarios
- Profile your specific use case as performance gains vary with message size and structure
§Error Conditions
Parsing can fail in the following cases:
- Invalid JSON Syntax: Malformed JSON structure, missing quotes, brackets, etc.
- Invalid UTF-8 Encoding: Non-UTF-8 bytes in string values
- Numeric Overflow: Numbers exceeding the range of supported numeric types
- Memory Allocation Failure: Only occurs for extremely large JSON documents
All errors are wrapped in anyhow::Result with descriptive error messages.
Structs§
- Binary
Message Processor - Optimized binary message processor for exchanges
Constants§
- STACK_
BUFFER_ SIZE - Stack size for small messages - 4KB should handle most market data messages without heap allocation
Traits§
- VecJson
Ext - Extension trait for
Vec<u8>to add zero-allocation JSON parsing
Functions§
- bytes_
to_ str - Zero-copy text extraction from byte slice
- deserialize_
from_ slice - Parse JSON directly to a type from a mutable slice
- deserialize_
from_ slice_ borrowed - Parse JSON directly to a type from an immutable slice
- deserialize_
from_ smallvec - Parse JSON directly to a type from SmallVec
- deserialize_
from_ vec - Parse JSON directly to a type from
Vec<u8> - parse_
json_ from_ slice - Parse JSON from a mutable slice
- parse_
json_ from_ smallvec - Parse JSON from SmallVec with zero allocation for small messages
- parse_
json_ from_ vec - Parse JSON from owned
Vec<u8>with zero allocation - vec_
to_ smallvec - Convert
Vec<u8>to SmallVec without allocation for small messages
Type Aliases§
- Message
Buffer - High-performance message buffer optimized for exchange data processing