pub struct BytesPool { /* private fields */ }Expand description
Tiered buffer pool for zero-copy buffer management.
This pool provides 4 tiers of buffers for different size ranges:
- Small: 4KB - 64KB
- Medium: 64KB - 512KB
- Large: 512KB - 4MB
- XLarge: > 4MB
Buffers are automatically reused when returned to the pool.
§Example
ⓘ
let pool = BytesPool::new_tiered();
// Acquire a buffer (automatically selects tier based on size)
let mut buffer = pool.acquire_buffer(8192).await;
// Use the buffer...
buffer.put_slice(b"hello world");
// Return to pool (automatic when dropped)
drop(buffer);
// Next acquisition will reuse the buffer
let mut buffer2 = pool.acquire_buffer(8192).await;
assert!(pool.hit_rate() > 0.0); // Buffer was reused!Implementations§
Source§impl BytesPool
impl BytesPool
Sourcepub fn new_tiered() -> Self
pub fn new_tiered() -> Self
Sourcepub fn with_config(config: BytesPoolConfig) -> Self
pub fn with_config(config: BytesPoolConfig) -> Self
Sourcepub async fn acquire_buffer(&self, size: usize) -> PooledBuffer
pub async fn acquire_buffer(&self, size: usize) -> PooledBuffer
Acquire buffer with automatic tier selection.
Selects the appropriate tier based on requested size and blocks until a buffer is available. Reuses returned buffers when available.
§Arguments
size- Minimum capacity for the buffer
§Returns
A PooledBuffer that releases the permit and returns buffer to pool when dropped.
§Example
ⓘ
let mut buffer = pool.acquire_buffer(8192).await;Sourcepub fn try_acquire_buffer(&self, size: usize) -> Option<PooledBuffer>
pub fn try_acquire_buffer(&self, size: usize) -> Option<PooledBuffer>
Sourcepub fn metrics(&self) -> &BytesPoolMetrics
pub fn metrics(&self) -> &BytesPoolMetrics
Get pool metrics.
Sourcepub fn available_buffers(&self) -> u64
pub fn available_buffers(&self) -> u64
Get the number of available buffers in the pool.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for BytesPool
impl RefUnwindSafe for BytesPool
impl Send for BytesPool
impl Sync for BytesPool
impl Unpin for BytesPool
impl UnsafeUnpin for BytesPool
impl UnwindSafe for BytesPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more