ax-codec-bytes
Buffer pooling and integration with the bytes crate for ax-codec. This crate provides memory-efficient buffer management and zero-copy operations with bytes::Bytes.
Features
- Thread-local buffer pooling - Reduces allocations for small buffers
bytescrate integration - Zero-copy operations withBytesandBytesMutsmallvecintegration - Stack-allocated small vectors (optional)no_stdcompatible (withallocfeature)
Installation
Add to your Cargo.toml:
[]
= { = "0.1", = ["std"] }
Feature Flags
std- Enables std library support (default)bytes- Enables integration withbytescratesmallvec- Enables integration withsmallveccrate
Thread-Local Buffer Pool
BufferPool
Thread-local pool for reusing buffers:
use ;
use Arc;
let pool = new;
let mut writer = with_pool;
writer.write_all.unwrap;
let data = writer.finish; // Takes ownership, doesn't recycle
// Or recycle explicitly:
let mut writer = with_pool;
writer.write_all.unwrap;
writer.recycle; // Returns buffer to pool
Custom Pool Configuration
use BufferPool;
let pool = with_capacity;
Pool Statistics
use BufferPool;
let pool = new;
println!;
println!;
bytes Crate Integration
BytesReader
Zero-copy reader for bytes::Bytes:
use BytesReader;
use ;
use Bytes;
let bytes = from;
let mut reader = new;
let value: u32 = decode.unwrap;
println!;
BytesMutWriter
Writer for bytes::BytesMut:
use BytesMutWriter;
use Encode;
let mut writer = with_capacity;
42u32.encode.unwrap;
let bytes = writer.freeze; // Convert to immutable Bytes
Decode from Bytes
Helper function for decoding directly from Bytes:
use bytes_impl;
use Bytes;
let mut buf = from;
let value: u32 = decode_from_bytes.unwrap;
Validate from Bytes
Validate wire format without allocation:
use bytes_impl;
use Bytes;
let mut buf = from;
.unwrap;
smallvec Integration
SmallVecBuf
Stack-allocated small vectors for zero-allocation small collections:
use SmallVecBuf;
use ;
// Encode
let mut writer = new;
let data: = from;
data.encode.unwrap;
// Decode
let mut reader = new;
let decoded: = decode.unwrap;
Memory Management
Automatic Recycling
PooledWriter automatically recycles its buffer on drop if not explicitly finished:
use ;
use Arc;
let pool = new;
assert_eq!; // Buffer returned to pool
Manual Control
Take ownership of the buffer instead of recycling:
use ;
use Arc;
let pool = new;
let mut writer = with_pool;
writer.write_all.unwrap;
let data = writer.finish; // Takes ownership, doesn't recycle
assert_eq!; // Buffer not returned to pool
Performance Considerations
When to Use Pooling
Buffer pooling is beneficial when:
- Creating many small buffers (≤ 64 KB)
- Buffers have similar lifetimes
- You want to reduce heap allocations
When Not to Use Pooling
Avoid pooling when:
- Buffers are very large (> 64 KB)
- Buffers need to be shared across threads
- Memory pressure is a concern (pool holds onto memory)
Thread Safety
BufferPoolusesArc<Mutex<>>for thread-safe access- Each thread has its own thread-local pool in ax-codec-core
- Choose based on your use case:
ax-codec-bytes::BufferPool- Thread-safe, shared poolax-codec-core::pool- Thread-local pool (faster, not shared)
Examples
Zero-Copy with Bytes
use BytesReader;
use View;
use Bytes;
let bytes = from;
let mut reader = new;
let msg = view.unwrap;
println!;
Efficient Network Buffering
use ;
use BufferWriter;
use Arc;
let pool = new;
// Reuse buffers for multiple messages
for i in 0..100
License
MIT