pub(crate) struct BufferPool {
pub(crate) bufs: Mutex<Vec<Vec<u8>>>,
pub(crate) buf_size: usize,
}Expand description
Recycles outgoing frame buffers from dropped AudioFrames back to the capture
thread, so the steady-state emit path allocates nothing.
INVARIANT: every buffer is born as vec![0u8; buf_size], so bytes [0, buf_size) stay
initialized for the allocation’s whole lifetime — truncate only shortens len, never
de-initializes memory. That is what makes restore’s set_len back to buf_size sound.
Fields§
§bufs: Mutex<Vec<Vec<u8>>>§buf_size: usizeImplementations§
Source§impl BufferPool
impl BufferPool
Sourcepub(crate) const MAX_POOLED: usize = 16
pub(crate) const MAX_POOLED: usize = 16
Cap on pooled buffers. Outstanding frames rarely exceed the delivery-ring capacity plus a few Python-held references; anything past this goes back to the allocator rather than growing the pool unboundedly.
Sourcepub(crate) fn new(buf_size: usize) -> Self
pub(crate) fn new(buf_size: usize) -> Self
Create an empty pool that hands out (and accepts) buf_size-byte buffers.
Sourcepub(crate) fn restore(&self, v: Vec<u8>) -> Vec<u8> ⓘ
pub(crate) fn restore(&self, v: Vec<u8>) -> Vec<u8> ⓘ
Restore a recycled buffer to full buf_size length via set_len.
Sound per the pool invariant: the bytes were written at allocation and truncation
does not de-initialize them, so extending len back to buf_size never exposes
uninitialized memory.
Sourcepub(crate) fn put(&self, v: Vec<u8>)
pub(crate) fn put(&self, v: Vec<u8>)
Return a buffer to the pool, unless it is undersized or the pool is already at
MAX_POOLED (in which case it is dropped to the allocator).
Sourcepub(crate) fn drain_into(&self, into: &mut Vec<Vec<u8>>)
pub(crate) fn drain_into(&self, into: &mut Vec<Vec<u8>>)
Move every pooled buffer into into under a single lock — the batched refill
for the sole-consumer PoolTaker, whose empty local stash is into.