pub struct CuBudget { /* private fields */ }Expand description
Compute-unit budget tracker.
On BPF, uses sol_log_compute_units() to read the remaining budget.
Off-chain, all operations are no-ops that succeed.
Implementations§
Source§impl CuBudget
impl CuBudget
Sourcepub fn snapshot() -> Self
pub fn snapshot() -> Self
Take a snapshot of the current compute budget.
On BPF this calls sol_log_compute_units() and captures the
remaining CU from the log output. On native (off-chain), the
snapshot is 0 and all checks pass trivially.
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 conservative check: the Solana runtime does not expose a “get remaining CU” syscall, so this method logs the current usage and returns Ok. The real enforcement is that the runtime itself will abort if CU is exhausted.
The value of this method is that it makes the CU concern VISIBLE in the code and provides a hook point for future runtime features that may expose remaining CU programmatically.
Off-chain, this always returns Ok.