MemoryPool

Struct MemoryPool 

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

Source

pub fn new() -> Self

Creates a new memory pool with default limits.

Source

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 cache
  • max_bytes - Maximum total bytes to cache
Source

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.

Source

pub fn release(&mut self, buffer: Vec<T>)

Returns a buffer to the pool for future reuse.

The buffer is cleared before being stored.

Source

pub fn cached_count(&self) -> usize

Returns the number of cached buffers.

Source

pub fn cached_bytes(&self) -> usize

Returns the total cached bytes.

Source

pub fn clear(&mut self)

Clears all cached buffers.

Source

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§

Source§

impl<T> Default for MemoryPool<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.