pub struct StackPool<T: Clone> { /* private fields */ }Expand description
A pool of reusable stacks to reduce allocation overhead.
§Examples
use adze_stack_pool_core::StackPool;
let pool: StackPool<u32> = StackPool::new(4);
let mut stack = pool.acquire();
stack.push(42);
pool.release(stack);
let reused = pool.acquire();
assert!(reused.is_empty()); // cleared on reuse
assert_eq!(pool.stats().reuse_count, 1);Implementations§
Source§impl<T: Clone> StackPool<T>
impl<T: Clone> StackPool<T>
Sourcepub fn new(max_pool_size: usize) -> Self
pub fn new(max_pool_size: usize) -> Self
Create a new stack pool with the specified maximum size.
§Examples
use adze_stack_pool_core::StackPool;
let pool: StackPool<i32> = StackPool::new(8);
assert_eq!(pool.stats().total_allocations, 0);Sourcepub fn acquire(&self) -> Vec<T>
pub fn acquire(&self) -> Vec<T>
Acquire a stack from the pool, or allocate a new one if pool is empty.
§Examples
use adze_stack_pool_core::StackPool;
let pool: StackPool<u32> = StackPool::new(4);
let stack = pool.acquire();
assert_eq!(stack.capacity(), 256);
assert_eq!(pool.stats().pool_misses, 1);Sourcepub fn acquire_with_capacity(&self, capacity: usize) -> Vec<T>
pub fn acquire_with_capacity(&self, capacity: usize) -> Vec<T>
Acquire a stack with at least the requested capacity.
Sourcepub fn release(&self, stack: Vec<T>)
pub fn release(&self, stack: Vec<T>)
Return a stack to the pool for reuse.
§Examples
use adze_stack_pool_core::StackPool;
let pool: StackPool<u32> = StackPool::new(4);
let stack = pool.acquire();
pool.release(stack);
assert_eq!(pool.stats().max_pool_depth, 1);Sourcepub fn clone_stack(&self, source: &[T]) -> Vec<T>
pub fn clone_stack(&self, source: &[T]) -> Vec<T>
Clone a stack, potentially using a pooled stack for the destination.
§Examples
use adze_stack_pool_core::StackPool;
let pool: StackPool<u32> = StackPool::new(4);
let original = vec![1, 2, 3];
let cloned = pool.clone_stack(&original);
assert_eq!(cloned, vec![1, 2, 3]);Sourcepub fn stats(&self) -> PoolStats
pub fn stats(&self) -> PoolStats
Get current pool statistics.
§Examples
use adze_stack_pool_core::StackPool;
let pool: StackPool<u32> = StackPool::new(4);
let _ = pool.acquire();
let stats = pool.stats();
assert_eq!(stats.total_allocations, 1);
assert_eq!(stats.pool_misses, 1);Sourcepub fn reset_stats(&self)
pub fn reset_stats(&self)
Reset statistics.
Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for StackPool<T>
impl<T> !RefUnwindSafe for StackPool<T>
impl<T> Send for StackPool<T>where
T: Send,
impl<T> !Sync for StackPool<T>
impl<T> Unpin for StackPool<T>where
T: Unpin,
impl<T> UnsafeUnpin for StackPool<T>
impl<T> UnwindSafe for StackPool<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