Type Alias MessageBuffer

Source
pub type MessageBuffer = SmallVec<[u8; 4096]>;
Expand description

High-performance message buffer optimized for exchange data processing

MessageBuffer is a stack-first allocation strategy that eliminates heap allocations for typical exchange messages while gracefully handling larger payloads. This type alias wraps SmallVec<[u8; 4096]> to provide optimal performance characteristics for HFT applications.

§Allocation Strategy

§Stack Allocation (0-4KB messages)

Messages under 4KB (4096 bytes) are stored entirely on the stack, providing:

  • Zero heap allocations - No malloc/free overhead
  • Cache locality - Stack memory is cache-hot and predictable
  • Sub-microsecond latency - No allocator contention or fragmentation
  • Deterministic performance - No GC pressure or allocation spikes

This covers ~95% of typical exchange messages including:

  • Order book updates (L2 data)
  • Trade confirmations
  • Account balance updates
  • Most REST API responses

§Heap Spillover (>4KB messages)

When messages exceed the stack buffer, SmallVec automatically spills to heap allocation:

  • Graceful degradation - No panics or truncation
  • Transparent operation - Same API for all message sizes
  • Single allocation - Minimizes heap fragmentation
  • Growth strategy - Efficient resizing for very large messages

Large messages include:

  • Full order book snapshots
  • Historical trade data
  • Large batch responses

§Performance Characteristics

Message SizeAllocationLatencyMemory Overhead
0-4KBStack<100ns0 bytes
>4KBHeap<1μs~16 bytes

§Usage Guidelines

  • WebSocket message parsing - Primary use case for real-time market data
  • JSON deserialization - Combine with simd_json for zero-copy parsing
  • Temporary message buffers - Reusable buffers in message processors
  • Protocol encoding/decoding - Binary message transformation

§Anti-Patterns to Avoid

  • Long-term storage - Use Vec<u8> for persistent data
  • Very large messages - Consider streaming for >1MB payloads
  • Frequent reallocation - Reuse buffers when possible

§Examples

§Basic Usage

use rusty_feeder::exchange::zerocopy_helpers::{MessageBuffer, parse_json_from_smallvec};

// Small message - stays on stack
let json = r#"{"symbol":"BTC/USD","price":50000.0}"#;
let mut buffer = MessageBuffer::from_slice(json.as_bytes());
assert!(!buffer.spilled()); // Confirms stack allocation

let parsed = parse_json_from_smallvec(&mut buffer)?;
assert_eq!(parsed["symbol"], "BTC/USD");

§Reusable Buffer Pattern

use rusty_feeder::exchange::zerocopy_helpers::{MessageBuffer, parse_json_from_smallvec};

let mut buffer = MessageBuffer::new();

// Process multiple messages with same buffer
for message_data in &[/* message bytes */] {
    buffer.clear();
    buffer.extend_from_slice(message_data);
    let parsed = parse_json_from_smallvec(&mut buffer)?;
    // Process parsed data...
}

§Size Detection

use rusty_feeder::exchange::zerocopy_helpers::MessageBuffer;

let mut buffer = MessageBuffer::from_slice(b"small message");
assert!(!buffer.spilled()); // Stack allocated

let large_data = vec![0u8; 8192]; // 8KB
let mut large_buffer = MessageBuffer::from_slice(&large_data);
assert!(large_buffer.spilled()); // Heap allocated

§Integration with Zero-Copy Parsing

MessageBuffer is designed to work seamlessly with the module’s zero-copy parsing functions:

§HFT Performance Notes

For maximum performance in high-frequency trading scenarios:

  1. Reuse buffers - Avoid repeated allocation by reusing MessageBuffer instances
  2. Profile message sizes - Monitor your specific exchange’s message size distribution
  3. Consider pooling - Use object pools for very high-throughput scenarios
  4. Monitor spillover - Track spilled() calls to optimize buffer sizing

§Memory Layout

Stack Frame (4KB):
┌─────────────────────────────────────┐
│ MessageBuffer                       │
│ ┌─────────────────────────────────┐ │
│ │ [u8; 4096] inline_data         │ │ ← Stack allocation
│ │ len: usize                     │ │
│ │ capacity: usize                │ │
│ └─────────────────────────────────┘ │
└─────────────────────────────────────┘

Heap (only when spilled):
┌─────────────────────────────────────┐
│ Vec<u8> heap_data                   │ ← Heap allocation
│ (ptr, len, capacity)                │
└─────────────────────────────────────┘

§Thread Safety

MessageBuffer is Send but not Sync. Each thread should maintain its own buffer instances for optimal performance and to avoid contention.

Aliased Type§

pub struct MessageBuffer { /* private fields */ }