pub struct CuBudget { /* private fields */ }Expand description
Compute-unit budget tracker.
On BPF this is backed by the sol_remaining_compute_units syscall
(SIMD-0049), so snapshots and guards read the real remaining budget.
That SIMD is Withdrawn and its feature gate has never been activated on
mainnet-beta, devnet, or testnet: a deployed program referencing the
symbol is rejected by the loader. On-chain builds therefore include this
type only with the crate’s remaining-compute-units-syscall feature,
for private clusters that activate the gate. Off-chain there is no CU
metering: remaining reports u64::MAX, so every
guard passes trivially and used reports 0.
Implementations§
Source§impl CuBudget
impl CuBudget
Sourcepub fn remaining() -> u64
pub fn remaining() -> u64
Remaining compute units for the current invocation.
On BPF, reads the sol_remaining_compute_units syscall
(SIMD-0049; not activated on any public cluster, see the type
docs). Off-chain, returns u64::MAX, host builds have no CU
meter, so guards built on this pass trivially, matching the
crate’s other host fallbacks.
Sourcepub fn snapshot() -> Self
pub fn snapshot() -> Self
Take a snapshot of the current compute budget.
Stores the remaining CU at call time (via
remaining), enabling the snapshot/check
pattern: let b = CuBudget::snapshot(); ...; b.used().
Note: this no longer emits a sol_log_compute_units log line the
way pre-SIMD-0049 versions did; it reads instead. Use
checkpoint for the logging behavior.
Sourcepub fn used(&self) -> u64
pub fn used(&self) -> u64
Compute units consumed since this snapshot was taken.
Saturates at 0 (the remaining budget can only decrease within an invocation, but saturating keeps hostile or host-side inputs from wrapping). Off-chain this is always 0.
Sourcepub fn checkpoint()
pub fn checkpoint()
Log the current compute unit consumption for profiling.
Emits via sol_log_compute_units on BPF. Use this to instrument
hot paths and identify CU bottlenecks.
Sourcepub fn require_remaining(&self, min_remaining: u64) -> ProgramResult
pub fn require_remaining(&self, min_remaining: u64) -> ProgramResult
Assert that at least min_remaining CU are available.
On BPF this is a real guard (SIMD-0049): it reads the remaining
budget and returns Err(ProgramError::Custom(ERR_INSUFFICIENT_CU))
when it is below the floor, letting the program fail cleanly (state
untouched, clear error) instead of hard-aborting mid-CPI when the
runtime exhausts the meter.
Off-chain, remaining is u64::MAX, so this
always returns Ok.