Skip to main content

BudgetEnforcer

Struct BudgetEnforcer 

Source
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

Source

pub fn new(budget: ComputeBudget) -> Self

Create a new enforcer with the given budget.

The wall-clock timer starts immediately.

Source

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.

Source

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.

Source

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.

Source

pub fn elapsed_us(&self) -> u64

Wall-clock microseconds elapsed since the enforcer was created.

Source

pub fn elapsed(&self) -> Duration

Wall-clock duration elapsed since the enforcer was created.

Source

pub fn iterations_used(&self) -> usize

Number of iterations consumed so far.

Source

pub fn memory_used(&self) -> usize

Cumulative memory tracked so far (in bytes).

Source

pub fn tolerance(&self) -> f64

The tolerance target from the budget (convenience accessor).

Source

pub fn budget(&self) -> &ComputeBudget

A reference to the underlying budget configuration.

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more