Skip to main content

tenferro_tensor/
cache.rs

1//! Cache accounting primitives shared by tensor backends and facade runtimes.
2
3/// Entry and retained-byte accounting for one cache.
4///
5/// `retained_bytes` reports the cache-owned logical payload estimate. It does
6/// not include allocator arena slack, operating-system RSS, or memory retained
7/// by unrelated process allocators.
8///
9/// # Examples
10///
11/// ```
12/// use tenferro_tensor::CacheStats;
13///
14/// let stats = CacheStats {
15///     entries: 2,
16///     retained_bytes: 128,
17/// };
18/// assert_eq!(stats.entries, 2);
19/// assert_eq!(stats.retained_bytes, 128);
20/// ```
21#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
22pub struct CacheStats {
23    /// Number of cache entries currently retained.
24    pub entries: usize,
25    /// Cache-owned retained payload estimate in bytes.
26    pub retained_bytes: usize,
27}
28
29impl CacheStats {
30    /// Return an empty stats snapshot.
31    ///
32    /// # Examples
33    ///
34    /// ```
35    /// use tenferro_tensor::CacheStats;
36    ///
37    /// let stats = CacheStats::empty();
38    /// assert_eq!(stats.entries, 0);
39    /// assert_eq!(stats.retained_bytes, 0);
40    /// ```
41    pub fn empty() -> Self {
42        Self::default()
43    }
44}
45
46/// Control surface required for backend runtime caches owned by higher-level runtimes.
47///
48/// Backend caches use this trait so higher-level runtimes and executors can
49/// clear and inspect the cache without knowing backend-specific entry types.
50///
51/// # Examples
52///
53/// ```
54/// use tenferro_tensor::{CacheStats, RuntimeCacheControl};
55///
56/// let mut cache = ();
57/// assert_eq!(cache.stats(), CacheStats::empty());
58/// cache.clear();
59/// assert_eq!(cache.stats().entries, 0);
60/// ```
61pub trait RuntimeCacheControl: Default {
62    /// Remove every retained cache entry.
63    fn clear(&mut self);
64
65    /// Snapshot retained entries and retained bytes.
66    fn stats(&self) -> CacheStats;
67}
68
69impl RuntimeCacheControl for () {
70    fn clear(&mut self) {}
71
72    fn stats(&self) -> CacheStats {
73        CacheStats::empty()
74    }
75}
76
77#[cfg(test)]
78mod tests;