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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#[cfg(feature = "cache-efficiency-debug")]
mod impl_ {
    /// A helper to collect useful information about cache efficiency.
    pub struct Debug {
        owner: String,
        hits: usize,
        puts: usize,
        misses: usize,
    }

    impl Debug {
        /// Create a new instance
        #[inline]
        pub fn new(owner: String) -> Self {
            Debug {
                owner,
                hits: 0,
                puts: 0,
                misses: 0,
            }
        }
        /// Count cache insertions
        #[inline]
        pub fn put(&mut self) {
            self.puts += 1;
        }
        /// Count hits
        #[inline]
        pub fn hit(&mut self) {
            self.hits += 1;
        }
        /// Count misses
        #[inline]
        pub fn miss(&mut self) {
            self.misses += 1;
        }
    }

    impl Drop for Debug {
        fn drop(&mut self) {
            let hits = self.hits;
            let misses = self.misses;
            let ratio = hits as f32 / misses as f32;
            eprintln!(
                "{}[{:0x}]: {} / {} (hits/misses) = {:.02}%, puts = {}",
                self.owner,
                self as *const _ as usize,
                hits,
                misses,
                ratio * 100.0,
                self.puts
            );
        }
    }
}
#[cfg(not(feature = "cache-efficiency-debug"))]
mod impl_ {
    /// The disabled, zero size do-nothing equivalent
    pub struct Debug;

    impl Debug {
        /// Create a new instance
        #[inline]
        pub fn new(_owner: String) -> Self {
            Debug
        }
        /// noop
        pub fn put(&mut self) {}
        /// noop
        pub fn hit(&mut self) {}
        /// noop
        pub fn miss(&mut self) {}
    }
}

pub use impl_::Debug;