pub struct BudgetEnforcer { /* private fields */ }Expand description
Enforces wall-time, iteration, and memory budgets during a solve.
Create one at the start of a solve and call the check_* methods at each
iteration or before allocating scratch space. The enforcer is intentionally
non-Clone so that each solve owns exactly one.
§Example
use ruvector_solver::budget::BudgetEnforcer;
use ruvector_solver::types::ComputeBudget;
let budget = ComputeBudget::default();
let mut enforcer = BudgetEnforcer::new(budget);
// At the top of each solver iteration:
enforcer.check_iteration().unwrap();
// Before allocating scratch memory:
enforcer.check_memory(1024).unwrap();Implementations§
Source§impl BudgetEnforcer
impl BudgetEnforcer
Sourcepub fn new(budget: ComputeBudget) -> Self
pub fn new(budget: ComputeBudget) -> Self
Create a new enforcer with the given budget.
The wall-clock timer starts immediately.
Sourcepub fn with_memory_limit(budget: ComputeBudget, memory_limit: usize) -> Self
pub fn with_memory_limit(budget: ComputeBudget, memory_limit: usize) -> Self
Create an enforcer with a custom memory ceiling.
Use this when the caller knows the available memory and wants to enforce a tighter or looser bound than the default 256 MiB.
Sourcepub fn check_iteration(&mut self) -> Result<(), SolverError>
pub fn check_iteration(&mut self) -> Result<(), SolverError>
Check whether the next iteration is within budget.
Must be called once per iteration, at the top of the loop body. Increments the internal iteration counter and checks both the iteration limit and the wall-clock time limit.
§Errors
Returns SolverError::BudgetExhausted if either the iteration count
or wall-clock time has been exceeded.
Sourcepub fn check_memory(&mut self, additional: usize) -> Result<(), SolverError>
pub fn check_memory(&mut self, additional: usize) -> Result<(), SolverError>
Check whether an additional memory allocation is within budget.
Call this before performing the allocation. The additional parameter
is the number of bytes the caller intends to allocate. If the allocation
would push cumulative usage over the memory ceiling, the call fails
without modifying the internal counter.
On success the internal counter is incremented by additional.
§Errors
Returns SolverError::BudgetExhausted if the allocation would exceed
the memory limit.
Sourcepub fn elapsed_us(&self) -> u64
pub fn elapsed_us(&self) -> u64
Wall-clock microseconds elapsed since the enforcer was created.
Sourcepub fn iterations_used(&self) -> usize
pub fn iterations_used(&self) -> usize
Number of iterations consumed so far.
Sourcepub fn memory_used(&self) -> usize
pub fn memory_used(&self) -> usize
Cumulative memory tracked so far (in bytes).
Sourcepub fn budget(&self) -> &ComputeBudget
pub fn budget(&self) -> &ComputeBudget
A reference to the underlying budget configuration.