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
MlxDeviceso it can allocate fresh buffers when the free list is empty.
Implementations§
Source§impl<'d> MlxBufferPool<'d>
impl<'d> MlxBufferPool<'d>
Sourcepub fn new(device: &'d MlxDevice) -> Self
pub fn new(device: &'d MlxDevice) -> Self
Create a new empty buffer pool backed by the given device.
Sourcepub fn alloc(
&mut self,
byte_len: usize,
dtype: DType,
shape: Vec<usize>,
) -> Result<MlxBuffer>
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.
Sourcepub fn release(&mut self, buffer: MlxBuffer)
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.
Sourcepub fn free_count(&self) -> usize
pub fn free_count(&self) -> usize
Return all free buffers’ count (for diagnostics).
Sourcepub fn free_bytes(&self) -> usize
pub fn free_bytes(&self) -> usize
Total number of bytes held in the free list.
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> 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