pub struct MemoryPool<T> { /* private fields */ }Expand description
A memory pool for temporary allocations.
This provides fast allocation for scratch space by reusing previously allocated buffers. Useful for algorithms that repeatedly allocate and free temporary storage of similar sizes.
§Thread Safety
This pool is NOT thread-safe. Use one pool per thread or wrap in a mutex.
§Example
use oxiblas_core::memory::MemoryPool;
let mut pool = MemoryPool::new();
// Acquire a buffer
let buffer: Vec<f64> = pool.acquire(100);
// ... use buffer ...
// Return buffer to pool for reuse
pool.release(buffer);
// Next acquire may reuse the buffer
let buffer2: Vec<f64> = pool.acquire(50);Implementations§
Source§impl<T> MemoryPool<T>
impl<T> MemoryPool<T>
Sourcepub fn with_limits(max_cached: usize, max_bytes: usize) -> Self
pub fn with_limits(max_cached: usize, max_bytes: usize) -> Self
Creates a new memory pool with custom limits.
§Arguments
max_cached- Maximum number of buffers to cachemax_bytes- Maximum total bytes to cache
Sourcepub fn acquire(&mut self, min_capacity: usize) -> Vec<T>
pub fn acquire(&mut self, min_capacity: usize) -> Vec<T>
Acquires a buffer with at least the given capacity.
If a suitable buffer exists in the pool, it is reused. Otherwise, a new buffer is allocated.
Sourcepub fn release(&mut self, buffer: Vec<T>)
pub fn release(&mut self, buffer: Vec<T>)
Returns a buffer to the pool for future reuse.
The buffer is cleared before being stored.
Sourcepub fn cached_count(&self) -> usize
pub fn cached_count(&self) -> usize
Returns the number of cached buffers.
Sourcepub fn cached_bytes(&self) -> usize
pub fn cached_bytes(&self) -> usize
Returns the total cached bytes.
Sourcepub fn shrink_to_limits(&mut self)
pub fn shrink_to_limits(&mut self)
Shrinks the pool to fit within the current limits.
Removes the largest buffers first to stay within limits.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for MemoryPool<T>
impl<T> RefUnwindSafe for MemoryPool<T>where
T: RefUnwindSafe,
impl<T> Send for MemoryPool<T>where
T: Send,
impl<T> Sync for MemoryPool<T>where
T: Sync,
impl<T> Unpin for MemoryPool<T>where
T: Unpin,
impl<T> UnwindSafe for MemoryPool<T>where
T: UnwindSafe,
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