use super::types::CoralBinary;
static NATIVE_BINARY_CACHE: std::sync::LazyLock<
std::sync::RwLock<std::collections::HashMap<(String, String), CoralBinary>>,
> = std::sync::LazyLock::new(|| std::sync::RwLock::new(std::collections::HashMap::new()));
#[must_use]
pub fn cached_native_binary(shader_hash: &str, arch: &str) -> Option<CoralBinary> {
let cache = NATIVE_BINARY_CACHE
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner);
cache
.get(&(shader_hash.to_owned(), arch.to_owned()))
.cloned()
}
pub fn cache_native_binary(shader_hash: &str, arch: &str, binary: CoralBinary) {
let mut cache = NATIVE_BINARY_CACHE
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner);
cache.insert((shader_hash.to_owned(), arch.to_owned()), binary);
}
#[must_use]
pub fn cached_native_binary_any_arch(shader_hash: &str) -> Option<CoralBinary> {
let cache = NATIVE_BINARY_CACHE
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner);
cache
.iter()
.find(|((hash, _), _)| hash == shader_hash)
.map(|(_, binary)| binary.clone())
}
#[must_use]
pub fn shader_hash(source: &str) -> String {
blake3::hash(source.as_bytes()).to_hex().to_string()
}