Skip to main content

MlxBufferPool

Struct MlxBufferPool 

Source
pub struct MlxBufferPool<'d> { /* private fields */ }
Expand description

Arena-style buffer pool that reuses Metal buffer allocations.

§Design

  • Buffers are bucketed by their allocated size rounded up to the nearest power of two. This reduces fragmentation at the cost of occasionally over-allocating by up to 2x.
  • release() returns a single buffer; reset() returns all outstanding buffers.
  • The pool holds a reference to the MlxDevice so it can allocate fresh buffers when the free list is empty.

Implementations§

Source§

impl<'d> MlxBufferPool<'d>

Source

pub fn new(device: &'d MlxDevice) -> Self

Create a new empty buffer pool backed by the given device.

Source

pub fn alloc( &mut self, byte_len: usize, dtype: DType, shape: Vec<usize>, ) -> Result<MlxBuffer>

Allocate a buffer from the pool.

If a free buffer of compatible size exists in the pool, it is reused (with updated dtype/shape metadata). Otherwise a new Metal buffer is allocated from the device.

The actual Metal buffer size will be rounded up to the nearest power of two for bucketing purposes.

Source

pub fn release(&mut self, buffer: MlxBuffer)

Return a buffer to the pool’s free list for future reuse.

The Metal memory is not deallocated — it stays resident on the GPU for fast reuse.

Source

pub fn free_count(&self) -> usize

Return all free buffers’ count (for diagnostics).

Source

pub fn free_bytes(&self) -> usize

Total number of bytes held in the free list.

Source

pub fn clear(&mut self)

Clear all free buffers, releasing Metal memory.

Auto Trait Implementations§

§

impl<'d> Freeze for MlxBufferPool<'d>

§

impl<'d> RefUnwindSafe for MlxBufferPool<'d>

§

impl<'d> Send for MlxBufferPool<'d>

§

impl<'d> Sync for MlxBufferPool<'d>

§

impl<'d> Unpin for MlxBufferPool<'d>

§

impl<'d> UnsafeUnpin for MlxBufferPool<'d>

§

impl<'d> UnwindSafe for MlxBufferPool<'d>

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.