Module zero_copy

Module zero_copy 

Source
Expand description

Zero-copy data handling utilities for performance-critical operations. Zero-copy message processing with minimal allocations

This module provides zero-allocation message handling using bytes::Bytes for maximum throughput and minimal memory overhead.

§When to Use ZeroCopyMessage

Most users should use Message instead.

ZeroCopyMessage is designed for extreme performance scenarios where:

  • You process millions of messages per second
  • Every allocation matters for your performance profile
  • You can defer deserialization until absolutely necessary
  • You’re willing to trade ergonomics for performance

§Message vs ZeroCopyMessage

FeatureMessageZeroCopyMessage
Ergonomics✅ Excellent⚠️ Manual
Memory✅ Good✅ Optimal
DeserializationEagerLazy
Multiple formats✅ JSON/CBOR/MessagePackJSON only
ID storageStack/StringArc (shared)
Use caseGeneral purposeUltra-high throughput

§Example Usage

use turbomcp_protocol::zero_copy::{ZeroCopyMessage, MessageId};
use bytes::Bytes;

// Create from raw bytes (no allocation)
let payload = Bytes::from(r#"{"method": "test", "id": 1}"#);
let mut msg = ZeroCopyMessage::from_bytes(MessageId::from("req-1"), payload);

// Lazy parsing - returns RawValue without full deserialization
let raw = msg.parse_json_lazy()?;
assert!(raw.get().contains("method"));

// Full deserialization when needed
let data: serde_json::Value = msg.deserialize()?;

Performance Tip: Use ZeroCopyMessage in hot paths where you need to route messages based on metadata but don’t need to parse the payload.

Modules§

fast
Fast utilities for message processing with SIMD acceleration
mmapNon-mmap
Memory-mapped file support (safe wrapper when feature is disabled)

Structs§

BufferPool
Buffer pool for reusing allocations
MessageBatch
Zero-copy message batch for efficient bulk processing
MessageMetadata
Lightweight message metadata
ZeroCopyMessage
Zero-copy message with lazy deserialization

Enums§

MessageId
Optimized message ID with Arc sharing