Debian Codegraph @

debian/rust-vendor/indexmap/benches/faststring.rs

raw
1 #![feature(test)]
2
3 extern crate test;
4
5 use test::Bencher;
6
7 use indexmap::IndexMap;
8
9 use std::collections::HashMap;
10
11 use std::hash::{Hash, Hasher};
12
13 use std::borrow::Borrow;
14 use std::ops::Deref;
15
16 /// Use a consistently seeded Rng for benchmark stability
17 fn small_rng() -> fastrand::Rng {
18 let seed = u64::from_le_bytes(*b"indexmap");
19 fastrand::Rng::with_seed(seed)
20 }
21
22 #[derive(PartialEq, Eq, Copy, Clone)]
23 #[repr(transparent)]
24 pub struct OneShot<T: ?Sized>(pub T);
25
26 impl Hash for OneShot<str> {
27 fn hash<H: Hasher>(&self, h: &mut H) {
28 h.write(self.0.as_bytes())
29 }
30 }
31
32 impl<'a, S> From<&'a S> for &'a OneShot<str>
33 where
34 S: AsRef<str>,
35 {
36 #[allow(unsafe_code)]
37 fn from(s: &'a S) -> Self {
38 let s: &str = s.as_ref();
39 // SAFETY: OneShot is a `repr(transparent)` wrapper
40 unsafe { &*(s as *const str as *const OneShot<str>) }
41 }
42 }
43
44 impl Hash for OneShot<String> {
45 fn hash<H: Hasher>(&self, h: &mut H) {
46 h.write(self.0.as_bytes())
47 }
48 }
49
50 impl Borrow<OneShot<str>> for OneShot<String> {
51 fn borrow(&self) -> &OneShot<str> {
52 <&OneShot<str>>::from(&self.0)
53 }
54 }
55
56 impl<T> Deref for OneShot<T> {
57 type Target = T;
58 fn deref(&self) -> &T {
59 &self.0
60 }
61 }
62
63 fn shuffled_keys<I>(iter: I) -> Vec<I::Item>
64 where
65 I: IntoIterator,
66 {
67 let mut v = Vec::from_iter(iter);
68 let mut rng = small_rng();
69 rng.shuffle(&mut v);
70 v
71 }
72
73 #[bench]
74 fn insert_hashmap_string_10_000(b: &mut Bencher) {
75 let c = 10_000;
76 b.iter(|| {
77 let mut map = HashMap::with_capacity(c);
78 for x in 0..c {
79 map.insert(x.to_string(), ());
80 }
81 map
82 });
83 }
84
85 #[bench]
86 fn insert_hashmap_string_oneshot_10_000(b: &mut Bencher) {
87 let c = 10_000;
88 b.iter(|| {
89 let mut map = HashMap::with_capacity(c);
90 for x in 0..c {
91 map.insert(OneShot(x.to_string()), ());
92 }
93 map
94 });
95 }
96
97 #[bench]
98 fn insert_indexmap_string_10_000(b: &mut Bencher) {
99 let c = 10_000;
100 b.iter(|| {
101 let mut map = IndexMap::with_capacity(c);
102 for x in 0..c {
103 map.insert(x.to_string(), ());
104 }
105 map
106 });
107 }
108
109 #[bench]
110 fn lookup_hashmap_10_000_exist_string(b: &mut Bencher) {
111 let c = 10_000;
112 let mut map = HashMap::with_capacity(c);
113 let keys = shuffled_keys(0..c);
114 for &key in &keys {
115 map.insert(key.to_string(), 1);
116 }
117 let lookups = (5000..c).map(|x| x.to_string()).collect::<Vec<_>>();
118 b.iter(|| {
119 let mut found = 0;
120 #[expect(clippy::unnecessary_get_then_check)]
121 for key in &lookups {
122 found += map.get(key).is_some() as i32;
123 }
124 found
125 });
126 }
127
128 #[bench]
129 fn lookup_hashmap_10_000_exist_string_oneshot(b: &mut Bencher) {
130 let c = 10_000;
131 let mut map = HashMap::with_capacity(c);
132 let keys = shuffled_keys(0..c);
133 for &key in &keys {
134 map.insert(OneShot(key.to_string()), 1);
135 }
136 let lookups = (5000..c)
137 .map(|x| OneShot(x.to_string()))
138 .collect::<Vec<_>>();
139 b.iter(|| {
140 let mut found = 0;
141 #[expect(clippy::unnecessary_get_then_check)]
142 for key in &lookups {
143 found += map.get(key).is_some() as i32;
144 }
145 found
146 });
147 }
148
149 #[bench]
150 fn lookup_indexmap_10_000_exist_string(b: &mut Bencher) {
151 let c = 10_000;
152 let mut map = IndexMap::with_capacity(c);
153 let keys = shuffled_keys(0..c);
154 for &key in &keys {
155 map.insert(key.to_string(), 1);
156 }
157 let lookups = (5000..c).map(|x| x.to_string()).collect::<Vec<_>>();
158 b.iter(|| {
159 let mut found = 0;
160 for key in &lookups {
161 found += map.get(key).is_some() as i32;
162 }
163 found
164 });
165 }
166
167 #[bench]
168 fn lookup_indexmap_10_000_exist_string_oneshot(b: &mut Bencher) {
169 let c = 10_000;
170 let mut map = IndexMap::with_capacity(c);
171 let keys = shuffled_keys(0..c);
172 for &key in &keys {
173 map.insert(OneShot(key.to_string()), 1);
174 }
175 let lookups = (5000..c)
176 .map(|x| OneShot(x.to_string()))
177 .collect::<Vec<_>>();
178 b.iter(|| {
179 let mut found = 0;
180 for key in &lookups {
181 found += map.get(key).is_some() as i32;
182 }
183 found
184 });
185 }