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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use crate::{Context, SizeOf};
use alloc::{
    collections::{BinaryHeap, LinkedList, VecDeque},
    ffi::CString,
    string::String,
    vec::Vec,
};
use core::mem::size_of;

impl SizeOf for String {
    #[inline]
    fn size_of_children(&self, context: &mut Context) {
        if self.capacity() != 0 {
            context
                .add_vectorlike(self.len(), self.capacity(), size_of::<u8>())
                .add_distinct_allocation();
        }
    }
}

impl SizeOf for CString {
    fn size_of_children(&self, context: &mut Context) {
        let length = self.to_bytes_with_nul().len();
        if length != 0 {
            context
                .add_arraylike(length, size_of::<u8>())
                .add_distinct_allocation();
        }
    }
}

impl<T> SizeOf for Vec<T>
where
    T: SizeOf,
{
    #[inline]
    fn size_of_children(&self, context: &mut Context) {
        if self.capacity() != 0 {
            context
                .add_vectorlike(self.len(), self.capacity(), size_of::<T>())
                .add_distinct_allocation();

            self.as_slice().size_of_children(context);
        }
    }
}

impl<T> SizeOf for VecDeque<T>
where
    T: SizeOf,
{
    fn size_of_children(&self, context: &mut Context) {
        if self.capacity() != 0 {
            context
                .add_vectorlike(self.len(), self.capacity(), size_of::<T>())
                .add_distinct_allocation();

            let (left, right) = self.as_slices();
            left.size_of_children(context);
            right.size_of_children(context);
        }
    }
}

impl<T> SizeOf for BinaryHeap<T>
where
    T: SizeOf,
{
    fn size_of_children(&self, context: &mut Context) {
        if self.capacity() != 0 {
            context
                .add_vectorlike(self.len(), self.capacity(), size_of::<T>())
                .add_distinct_allocation();

            self.iter()
                .for_each(|element| element.size_of_children(context));
        }
    }
}

impl<T> SizeOf for LinkedList<T>
where
    T: SizeOf,
{
    fn size_of_children(&self, context: &mut Context) {
        let length = self.len();

        if length != 0 {
            // Record each node as a `{ T, *const (), *const () }`
            context
                .add_arraylike(length, size_of::<T>() + (size_of::<*const ()>() * 2))
                .add_distinct_allocations(length);

            self.iter()
                .for_each(|element| element.size_of_children(context));
        }
    }
}

// A btree node has 2*B - 1 (K,V) pairs and (usize, u16, u16)
// overhead, and an internal btree node additionally has 2*B
// `usize` overhead.
// A node can contain between B - 1 and 2*B - 1 elements, so
// we assume it has the midpoint 3/2*B - 1.
pub(crate) mod btree {
    use crate::{Context, SizeOf};
    use alloc::collections::{BTreeMap, BTreeSet};
    use core::mem::size_of;

    // Constants from rust's source:
    // https://doc.rust-lang.org/src/alloc/collections/btree/node.rs.html#43-45
    const B: usize = 6;
    const BTREE_MAX: usize = 2 * B - 1;
    const BTREE_MIN: usize = B - 1;

    // A fake btree node, this isn't 100% accurate since the real btree node can
    // have a different layout, but it should be close enough
    #[allow(dead_code)]
    struct FakeNode<K, V> {
        parent: *const (),
        parent_idx: u16,
        len: u16,
        keys: [K; BTREE_MAX],
        values: [V; BTREE_MAX],
    }

    // TODO: Figure out the unused capacity as well
    // TODO: Estimate the number of allocated buckets each btree makes
    pub(crate) const fn estimate_btree_size<K, V>(length: usize) -> usize {
        length * size_of::<FakeNode<K, V>>() * 2 / (BTREE_MAX + BTREE_MIN)
    }

    impl<K> SizeOf for BTreeSet<K>
    where
        K: SizeOf,
    {
        fn size_of_children(&self, context: &mut Context) {
            if !self.is_empty() {
                context
                    .add(estimate_btree_size::<K, ()>(self.len()))
                    // FIXME: Estimate the number of allocated buckets
                    .add_distinct_allocation();

                self.iter().for_each(|key| key.size_of_children(context));
            }
        }
    }

    impl<K, V> SizeOf for BTreeMap<K, V>
    where
        K: SizeOf,
        V: SizeOf,
    {
        fn size_of_children(&self, context: &mut Context) {
            if !self.is_empty() {
                context
                    .add(estimate_btree_size::<K, V>(self.len()))
                    // FIXME: Estimate the number of allocated buckets
                    .add_distinct_allocation();

                self.iter().for_each(|(key, value)| {
                    key.size_of_children(context);
                    value.size_of_children(context);
                });
            }
        }
    }
}