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