git_features/
cache.rs

1#[cfg(feature = "cache-efficiency-debug")]
2mod impl_ {
3    /// A helper to collect useful information about cache efficiency.
4    pub struct Debug {
5        owner: String,
6        hits: usize,
7        puts: usize,
8        misses: usize,
9    }
10
11    impl Debug {
12        /// Create a new instance
13        #[inline]
14        pub fn new(owner: impl Into<String>) -> Self {
15            Debug {
16                owner: owner.into(),
17                hits: 0,
18                puts: 0,
19                misses: 0,
20            }
21        }
22        /// Count cache insertions
23        #[inline]
24        pub fn put(&mut self) {
25            self.puts += 1;
26        }
27        /// Count hits
28        #[inline]
29        pub fn hit(&mut self) {
30            self.hits += 1;
31        }
32        /// Count misses
33        #[inline]
34        pub fn miss(&mut self) {
35            self.misses += 1;
36        }
37    }
38
39    impl Drop for Debug {
40        fn drop(&mut self) {
41            let hits = self.hits;
42            let misses = self.misses;
43            let ratio = hits as f32 / misses as f32;
44            eprintln!(
45                "{}[{:0x}]: {} / {} (hits/misses) = {:.02}%, puts = {}",
46                self.owner,
47                self as *const _ as usize,
48                hits,
49                misses,
50                ratio * 100.0,
51                self.puts
52            );
53        }
54    }
55}
56#[cfg(not(feature = "cache-efficiency-debug"))]
57mod impl_ {
58    /// The disabled, zero size do-nothing equivalent
59    pub struct Debug;
60
61    impl Debug {
62        /// Create a new instance
63        #[inline]
64        pub fn new(_owner: impl Into<String>) -> Self {
65            Debug
66        }
67        /// noop
68        pub fn put(&mut self) {}
69        /// noop
70        pub fn hit(&mut self) {}
71        /// noop
72        pub fn miss(&mut self) {}
73    }
74}
75
76pub use impl_::Debug;