Skip to main content

ChunkPool

Struct ChunkPool 

Source
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) + an unwrap_or_else that 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

Source

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.

Source

pub fn idle_count(&self) -> usize

Number of buffers currently idle in the pool. Snapshot-only; useful for diagnostic metrics.

Source

pub fn capacity(&self) -> usize

Maximum buffers the pool can hold idle.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.