Skip to main content

StackPool

Struct StackPool 

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

Source

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

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

pub fn acquire_with_capacity(&self, capacity: usize) -> Vec<T>

Acquire a stack with at least the requested capacity.

Source

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

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]);
Source

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

pub fn reset_stats(&self)

Reset statistics.

Source

pub fn clear(&self)

Clear the pool, releasing all cached stacks.

Trait Implementations§

Source§

impl<T: Clone> Debug for StackPool<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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