INNER CODE UNIT · Rust
retain_snap
RyanCodrai/turbovec · turbovec-python/src/lib.rs:400
fn retain_snap(snap: &mut Vec<f32>, prev: &std::sync::atomic::AtomicUsize, want: usize) {
let prev = prev.swap(want, std::sync::atomic::Ordering::Relaxed);
let target = prev.saturating_add(prev / 2);
if snap.capacity() > target.saturating_mul(2) {
snap.truncate(target);
snap.shrink_to(target);
}
}
// `module` makes `__module__` report the extension module the class
// actually lives in, so a class reference round-trips through pickle and
// through any registry that records `f"{cls.__module__}.{cls.__name__}"`
// (it read `builtins.TurboQuantIndex` before, which resolves nowhere).
// `weakref` lets an index sit in a `weakref.WeakValueDictionary` — the
// standard way to key a per-tenant cache without pinning the memory
// (#340). Both classes carry the same options.
#[pyclass(frozen, module = "turbovec._turbovec", weakref)]
struct TurboQuantIndex {