Skip to main content

j2k_core/
context.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3/// Cache hit/miss counters reported by codec contexts.
4#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
5pub struct CacheStats {
6    /// Number of cache lookups that reused existing state.
7    pub hits: u64,
8    /// Number of cache lookups that had to build new state.
9    pub misses: u64,
10    /// Number of currently occupied cache slots.
11    pub occupied_slots: u64,
12    /// Number of cache entries evicted by later insertions.
13    pub evictions: u64,
14}
15
16impl CacheStats {
17    /// Construct cache statistics from explicit counters.
18    #[must_use]
19    pub const fn new(hits: u64, misses: u64) -> Self {
20        Self {
21            hits,
22            misses,
23            occupied_slots: 0,
24            evictions: 0,
25        }
26    }
27
28    /// Construct cache statistics from full counters.
29    #[must_use]
30    pub const fn with_slots(hits: u64, misses: u64, occupied_slots: u64, evictions: u64) -> Self {
31        Self {
32            hits,
33            misses,
34            occupied_slots,
35            evictions,
36        }
37    }
38}
39
40/// Reusable codec state cached across decode calls.
41pub trait CodecContext: Default + Send {
42    /// Drop cached state while keeping the context reusable.
43    fn clear(&mut self);
44
45    /// Return current cache counters, when the codec tracks them.
46    fn cache_stats(&self) -> CacheStats {
47        CacheStats::default()
48    }
49}
50
51#[cfg(test)]
52mod tests;