pub struct ChunkPool { /* private fields */ }Expand description
Lock-free shared pool of Vec<u8> buffers used to recycle chunk
allocations across StreamingWriter instances.
Backed by a crossbeam_queue::ArrayQueue (MPMC, lock-free, fixed
capacity). Acquiring a buffer is a single atomic CAS; releasing is
the same. When the pool is empty, acquire allocates a fresh
Vec<u8>. When the pool is full, release drops the buffer.
§Lifetime model
A buffer leaves the pool on acquire, gets handed to
bytes::Bytes::from_owner wrapped in a [PooledChunk] owner,
and is released back to the pool when the last Bytes reference
is dropped — typically after the HTTP framework has flushed the
chunk to the wire. Because Bytes may be dropped on any thread
(the actix worker that wrote the chunk to the socket, not the
spawn_blocking worker that produced it), the pool MUST be
thread-safe — ArrayQueue is.
§Sizing
max_pool should match the expected concurrent in-flight chunk
count: concurrent_renders × channel_capacity in the worst case.
For the production setup (4-chunk channels, ~100 concurrent
renders), max_pool = 512 covers the working set; surplus buffers
are dropped when full so memory cannot grow unboundedly.
chunk_size should match StreamingWriter::CHUNK_TARGET + BUF_HEADROOM. When acquiring, the writer always grows the buffer
if the pool returned a smaller one (host code that mixes pool
sizes pays a one-time grow per buffer).
§Cost
acquire: 1 atomic CAS (~10 ns on x86) + anunwrap_or_elsethat allocates only on miss.release: 1 atomic CAS + drop-on-overflow.- Pool storage:
max_pool * size_of::<AtomicCell<Vec<u8>>>= ~32 bytes per slot, i.e. 512 slots = 16 KiB pool overhead.
§Example
use std::sync::Arc;
use webui::streaming::{ChunkPool, StreamingWriter};
// Construct ONE pool at server startup:
let pool = Arc::new(ChunkPool::new(512, StreamingWriter::CHUNK_TARGET));
// Each request:
let (tx, rx) = tokio::sync::mpsc::channel(StreamingWriter::DEFAULT_CHANNEL_CAPACITY);
let writer = StreamingWriter::new_pooled(tx, Arc::clone(&pool));Implementations§
Source§impl ChunkPool
impl ChunkPool
Sourcepub fn new(max_pool: usize, chunk_size: usize) -> Self
pub fn new(max_pool: usize, chunk_size: usize) -> Self
Create a new shared chunk pool. Wrap in Arc and share across
all StreamingWriter instances that should recycle their
chunk buffers.
max_pool is the maximum number of buffers held idle at once.
Surplus buffers are dropped (returned to the allocator) — this
caps total pool memory at max_pool × chunk_size.
chunk_size is the initial capacity used when allocating a
fresh buffer on a pool miss. Pre-sizing avoids a Vec-grow on
the hot path.
Sourcepub fn idle_count(&self) -> usize
pub fn idle_count(&self) -> usize
Number of buffers currently idle in the pool. Snapshot-only; useful for diagnostic metrics.
Auto Trait Implementations§
impl !Freeze for ChunkPool
impl RefUnwindSafe for ChunkPool
impl Send for ChunkPool
impl Sync for ChunkPool
impl Unpin for ChunkPool
impl UnsafeUnpin for ChunkPool
impl UnwindSafe for ChunkPool
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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more