use dashmap::DashMap;
use once_cell::sync::Lazy;
use smallvec::SmallVec;
use solana_sdk::instruction::Instruction;
use solana_compute_budget_interface::ComputeBudgetInstruction;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct ComputeBudgetCacheKey {
unit_price: u64,
unit_limit: u32,
}
static COMPUTE_BUDGET_CACHE: Lazy<DashMap<ComputeBudgetCacheKey, SmallVec<[Instruction; 2]>>> =
Lazy::new(|| DashMap::new());
#[inline(always)]
pub fn compute_budget_instructions(
unit_price: u64,
unit_limit: u32,
) -> SmallVec<[Instruction; 2]> {
let cache_key = ComputeBudgetCacheKey {
unit_price: unit_price,
unit_limit: unit_limit,
};
if let Some(cached_insts) = COMPUTE_BUDGET_CACHE.get(&cache_key) {
return cached_insts.clone();
}
let mut insts = SmallVec::<[Instruction; 2]>::new();
if unit_price > 0 {
insts.push(ComputeBudgetInstruction::set_compute_unit_price(unit_price));
}
if unit_limit > 0 {
insts.push(ComputeBudgetInstruction::set_compute_unit_limit(unit_limit));
}
let insts_clone = insts.clone();
COMPUTE_BUDGET_CACHE.insert(cache_key, insts_clone);
insts
}