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 clearedImplementations§
Source§impl TracePool
impl TracePool
Sourcepub fn new(max_size: usize) -> Self
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 poolmin_size: Minimum number of traces to maintain (for shrinking)
Sourcepub fn with_bounds(max_size: usize, min_size: usize) -> Self
pub fn with_bounds(max_size: usize, min_size: usize) -> Self
Create a new trace pool with custom capacity bounds.
Sourcepub fn get(&mut self) -> Trace
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.
Sourcepub fn return_trace(&mut self, trace: Trace)
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.
Sourcepub fn shrink(&mut self)
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.
Sourcepub fn reset_stats(&mut self)
pub fn reset_stats(&mut self)
Reset statistics counters.
Sourcepub fn min_capacity(&self) -> usize
pub fn min_capacity(&self) -> usize
Minimum size maintained during shrinking.