use super::model::TelcoClassifier;
use anyhow::{Context, Result};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
use tracing::debug;
pub(crate) struct RingbackModel {
bytes: &'static [u8],
}
impl RingbackModel {
pub(crate) fn id(&self) -> usize {
self.bytes.as_ptr() as usize
}
pub(crate) fn size(&self) -> usize {
self.bytes.len()
}
pub(crate) fn classifier(&self) -> TelcoClassifier<'static> {
TelcoClassifier::new(self.bytes)
}
}
type Registry = Mutex<HashMap<PathBuf, Arc<RingbackModel>>>;
fn registry() -> &'static Registry {
static MODELS: OnceLock<Registry> = OnceLock::new();
MODELS.get_or_init(|| Mutex::new(HashMap::new()))
}
fn lock_registry() -> std::sync::MutexGuard<'static, HashMap<PathBuf, Arc<RingbackModel>>> {
registry()
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
pub(crate) fn load(path: impl AsRef<Path>) -> Result<Arc<RingbackModel>> {
let path = path.as_ref();
let key = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
if let Some(model) = lock_registry().get(&key) {
return Ok(model.clone());
}
let data = std::fs::read(&key).with_context(|| {
format!(
"Failed to load ringback model weights from {}",
key.display()
)
})?;
let mut models = lock_registry();
if let Some(model) = models.get(&key) {
return Ok(model.clone());
}
let bytes: &'static [u8] = Box::leak(data.into_boxed_slice());
let model = Arc::new(RingbackModel { bytes });
debug!(path = %key.display(), size = model.size(), "ringback model weights loaded");
models.insert(key, model.clone());
Ok(model)
}
#[cfg(test)]
mod tests {
use super::*;
fn weights_path() -> Option<PathBuf> {
crate::media::ringback_detection::test_support::weights_path()
}
#[test]
fn load_is_cached_per_path() {
let Some(path) = weights_path() else {
eprintln!("skipping: telcoclassifier_weights.bin not found");
return;
};
let a = load(&path).unwrap();
let b = load(&path).unwrap();
assert!(
Arc::ptr_eq(&a, &b),
"repeated loads must reuse the same weights blob"
);
assert_eq!(a.id(), b.id());
assert!(a.size() > 1024 * 1024, "weights look truncated");
}
#[test]
fn load_missing_file_reports_path() {
let err = load("./definitely-not-a-weights-file.bin")
.err()
.expect("missing weights must fail");
let msg = format!("{err:#}");
assert!(msg.contains("definitely-not-a-weights-file.bin"), "{msg}");
}
#[test]
fn repeated_loads_never_leak_a_second_copy() {
let Some(path) = weights_path() else {
eprintln!("skipping: telcoclassifier_weights.bin not found");
return;
};
let first = load(&path).unwrap();
for i in 0..32 {
let again = load(&path).unwrap();
assert!(
Arc::ptr_eq(&first, &again),
"load #{i} produced a new weights copy"
);
}
}
}