Debian Codegraph @

debian/missing-sources/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 for key in &lookups {
121 found += map.get(key).is_some() as i32;
122 }
123 found
124 });
125 }
126
127 #[bench]
128 fn lookup_hashmap_10_000_exist_string_oneshot(b: &mut Bencher) {
129 let c = 10_000;
130 let mut map = HashMap::with_capacity(c);
131 let keys = shuffled_keys(0..c);
132 for &key in &keys {
133 map.insert(OneShot(key.to_string()), 1);
134 }
135 let lookups = (5000..c)
136 .map(|x| OneShot(x.to_string()))
137 .collect::<Vec<_>>();
138 b.iter(|| {
139 let mut found = 0;
140 for key in &lookups {
141 found += map.get(key).is_some() as i32;
142 }
143 found
144 });
145 }
146
147 #[bench]
148 fn lookup_indexmap_10_000_exist_string(b: &mut Bencher) {
149 let c = 10_000;
150 let mut map = IndexMap::with_capacity(c);
151 let keys = shuffled_keys(0..c);
152 for &key in &keys {
153 map.insert(key.to_string(), 1);
154 }
155 let lookups = (5000..c).map(|x| x.to_string()).collect::<Vec<_>>();
156 b.iter(|| {
157 let mut found = 0;
158 for key in &lookups {
159 found += map.get(key).is_some() as i32;
160 }
161 found
162 });
163 }
164
165 #[bench]
166 fn lookup_indexmap_10_000_exist_string_oneshot(b: &mut Bencher) {
167 let c = 10_000;
168 let mut map = IndexMap::with_capacity(c);
169 let keys = shuffled_keys(0..c);
170 for &key in &keys {
171 map.insert(OneShot(key.to_string()), 1);
172 }
173 let lookups = (5000..c)
174 .map(|x| OneShot(x.to_string()))
175 .collect::<Vec<_>>();
176 b.iter(|| {
177 let mut found = 0;
178 for key in &lookups {
179 found += map.get(key).is_some() as i32;
180 }
181 found
182 });
183 }