TracePool

Struct TracePool 

Source
pub struct TracePool { /* private fields */ }
Expand description

Memory pool for reusing trace allocations to reduce overhead.

TracePool maintains a collection of cleared Trace objects that can be reused to reduce allocation overhead in MCMC and other inference algorithms.

Example:


let mut pool = TracePool::new(10); // Pool up to 10 traces

// Get traces from pool (creates new ones initially)
let trace1 = pool.get();
let trace2 = pool.get();
assert_eq!(pool.stats().misses, 2); // Both were cache misses

// Return traces to pool for reuse
pool.return_trace(trace1);
pool.return_trace(trace2);
assert_eq!(pool.stats().returns, 2);

// Next gets will reuse pooled traces (cache hits)
let trace3 = pool.get();
assert_eq!(pool.stats().hits, 1);
assert_eq!(trace3.choices.len(), 0); // Trace was cleared

Implementations§

Source§

impl TracePool

Source

pub fn new(max_size: usize) -> Self

Create a new trace pool with the specified capacity bounds.

  • max_size: Maximum number of traces to keep in the pool
  • min_size: Minimum number of traces to maintain (for shrinking)
Source

pub fn with_bounds(max_size: usize, min_size: usize) -> Self

Create a new trace pool with custom capacity bounds.

Source

pub fn get(&mut self) -> Trace

Get a trace from the pool or create new one.

Returns a cleared trace ready for use. Updates hit/miss statistics.

Source

pub fn return_trace(&mut self, trace: Trace)

Return a trace to the pool for reuse.

The trace will be cleared and made available for future gets. If the pool is full, the trace will be dropped.

Source

pub fn shrink(&mut self)

Shrink the pool to the minimum size if it’s grown too large.

This can be called periodically to reclaim memory when the pool has accumulated more traces than needed.

Source

pub fn shrink_to(&mut self, target_size: usize)

Force shrink to a specific size.

Source

pub fn clear(&mut self)

Clear all traces from the pool.

Source

pub fn stats(&self) -> &PoolStats

Get current pool statistics.

Source

pub fn reset_stats(&mut self)

Reset statistics counters.

Source

pub fn len(&self) -> usize

Current number of available traces in the pool.

Source

pub fn is_empty(&self) -> bool

Check if the pool is empty.

Source

pub fn capacity(&self) -> usize

Maximum capacity of the pool.

Source

pub fn min_capacity(&self) -> usize

Minimum size maintained during shrinking.

Auto Trait Implementations§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V