Debian Codegraph @

src/hash.rs

raw symbols SCIP
1 //! `DynamicUsage` impls for `HashMap` and `HashSet`.
2 //!
3 //! Rust's `HashMap` and `HashSet` are backed by the the `hashbrown` crate.
4
5 use alloc::alloc::Layout;
6 use core::mem;
7
8 #[cfg(feature = "std")]
9 use std::collections::{HashMap, HashSet};
10
11 use crate::DynamicUsage;
12
13 // The widths are sourced from here:
14 // https://github.com/rust-lang/hashbrown/blob/dbd6dbe30a4076c0ea65ca5bd57036c27f3cc7c9/src/raw/mod.rs#L15-L36
15 // https://github.com/rust-lang/hashbrown/blob/dbd6dbe30a4076c0ea65ca5bd57036c27f3cc7c9/src/raw/generic.rs#L5-L21
16 // https://github.com/rust-lang/hashbrown/blob/dbd6dbe30a4076c0ea65ca5bd57036c27f3cc7c9/src/raw/sse2.rs#L14-L19
17
18 #[cfg(all(target_feature = "sse2", target_arch = "x86", not(miri)))]
19 pub(crate) const WIDTH: usize = mem::size_of::<core::arch::x86::__m128i>();
20 #[cfg(all(target_feature = "sse2", target_arch = "x86_64", not(miri)))]
21 pub(crate) const WIDTH: usize = mem::size_of::<core::arch::x86_64::__m128i>();
22 #[cfg(all(
23 any(not(target_feature = "sse2"), miri),
24 any(
25 target_pointer_width = "64",
26 target_arch = "aarch64",
27 target_arch = "x86_64",
28 target_arch = "wasm32",
29 )
30 ))]
31 pub(crate) const WIDTH: usize = mem::size_of::<u64>();
32 #[cfg(all(
33 any(not(target_feature = "sse2"), miri),
34 target_pointer_width = "32",
35 not(target_arch = "aarch64"),
36 not(target_arch = "x86_64"),
37 not(target_arch = "wasm32"),
38 ))]
39 pub(crate) const WIDTH: usize = mem::size_of::<u32>();
40
41 fn dynamic_usage_for_capacity<K, V>(cap: usize) -> usize {
42 // The bucket calculation is sourced from here:
43 // https://github.com/rust-lang/hashbrown/blob/dbd6dbe30a4076c0ea65ca5bd57036c27f3cc7c9/src/raw/mod.rs#L187-L216
44 //
45 // hashbrown's RawTable::buckets is not accessible via the std HashMap, and
46 // HashMap::capacity is a lower bound. However, hashbrown has an invariant that
47 // the number of buckets is a power of two, so usually we'll calculate the correct
48 // memory usage, and occasionally we'll undercount by around a factor of two.
49 let buckets = {
50 if cap < 8 {
51 return if cap < 4 { 4 } else { 8 };
52 }
53 let adjusted_cap = (cap * 8) / 7;
54 adjusted_cap.next_power_of_two()
55 };
56
57 // The memory usage calculation is sourced from here:
58 // https://github.com/rust-lang/hashbrown/blob/dbd6dbe30a4076c0ea65ca5bd57036c27f3cc7c9/src/raw/mod.rs#L240-L265
59
60 let layout = Layout::new::<(K, V)>();
61 let size = layout.size();
62 let ctrl_align = usize::max(layout.align(), WIDTH);
63
64 let ctrl_offset = (size * buckets + ctrl_align - 1) & !(ctrl_align - 1);
65 ctrl_offset + buckets + WIDTH
66 }
67
68 impl<K: DynamicUsage, V: DynamicUsage> DynamicUsage for HashMap<K, V> {
69 fn dynamic_usage(&self) -> usize {
70 dynamic_usage_for_capacity::<K, V>(self.capacity())
71 + self
72 .iter()
73 .map(|(k, v)| k.dynamic_usage() + v.dynamic_usage())
74 .sum::<usize>()
75 }
76
77 fn dynamic_usage_bounds(&self) -> (usize, Option<usize>) {
78 (
79 dynamic_usage_for_capacity::<K, V>(self.capacity())
80 + self
81 .iter()
82 .map(|(k, v)| k.dynamic_usage_bounds().0 + v.dynamic_usage_bounds().0)
83 .sum::<usize>(),
84 None,
85 )
86 }
87 }
88
89 impl<T: DynamicUsage> DynamicUsage for HashSet<T> {
90 fn dynamic_usage(&self) -> usize {
91 // HashSet<T> is just HashMap<T, ()>
92 dynamic_usage_for_capacity::<T, ()>(self.capacity())
93 + self.iter().map(DynamicUsage::dynamic_usage).sum::<usize>()
94 }
95
96 fn dynamic_usage_bounds(&self) -> (usize, Option<usize>) {
97 (
98 dynamic_usage_for_capacity::<T, ()>(self.capacity())
99 + self
100 .iter()
101 .map(|k| k.dynamic_usage_bounds().0)
102 .sum::<usize>(),
103 None,
104 )
105 }
106 }
107
108 #[cfg(test)]
109 mod tests {
110 use super::*;
111
112 #[test]
113 fn hashmap() {
114 let h: HashMap<u16, u32> = HashMap::with_capacity(12);
115
116 // - Capacity of 12 -> 16 buckets
117 // - Overhead is 1 byte per bucket
118 // - Fixed overhead of WIDTH
119 let lower = 16 * (mem::size_of::<(u16, u32)>() + 1) + WIDTH;
120 assert_eq!(h.dynamic_usage(), lower);
121 assert_eq!(h.dynamic_usage_bounds(), (lower, None));
122 }
123
124 #[test]
125 fn hashset() {
126 let h: HashSet<u16> = HashSet::with_capacity(17);
127
128 // - Capacity of 17 -> 32 buckets
129 // - Overhead is 1 byte per bucket
130 // - Fixed overhead of WIDTH
131 let lower = 32 * (mem::size_of::<u16>() + 1) + WIDTH;
132 assert_eq!(h.dynamic_usage(), lower);
133 assert_eq!(h.dynamic_usage_bounds(), (lower, None));
134 }
135 }