Module zerocopy_helpers

Source
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

MethodAllocationsPerformanceMemory Usage
serde_json::from_slice1-2 copiesBaseline (1x)2-3x input size
simd_json::from_slice1 copy2-3x faster1.5x input size
VecJsonExt::parse_json0 copies2-10x faster1x 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 VecJsonExt for all incoming WebSocket messages in trading systems
  • Combine with SmallVec for 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§

BinaryMessageProcessor
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§

VecJsonExt
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§

MessageBuffer
High-performance message buffer optimized for exchange data processing