Skip to content

Commit 9992044

Browse files
committed
use size hints for comparing maps
1 parent c2043b0 commit 9992044

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

benchmarks/benches/basic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn bench_basic_generation(c: &mut Criterion) {
3838
b.iter(|| {
3939
let diff = black_box(StructDiff::diff_ref(&first, &second));
4040
black_box(diff);
41-
})
41+
})
4242
});
4343
group.finish();
4444
}
@@ -47,7 +47,7 @@ fn bench_basic_full(c: &mut Criterion) {
4747
const GROUP_NAME: &str = "bench_basic";
4848
let mut rng = WyRand::new_seed(SEED);
4949
let mut first = black_box(TestBench::generate_random(&mut rng));
50-
50+
5151
let second = black_box(TestBench::generate_random(&mut rng));
5252
let mut diff: Vec<<TestBench as StructDiff>::Diff> = Vec::new();
5353
let mut group = c.benchmark_group(GROUP_NAME);
@@ -58,7 +58,7 @@ fn bench_basic_full(c: &mut Criterion) {
5858
b.iter(|| {
5959
diff = black_box(StructDiff::diff(&first, &second));
6060
black_box(first.apply_mut(diff.clone()));
61-
})
61+
})
6262
});
6363
group.finish();
6464
first.assert_eq(second, &diff);

benchmarks/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ impl TestBench {
9595
assert_eq_unordered_sort!(self.d, right.d, "{:?}", diff);
9696
assert_eq_unordered_sort!(
9797
self.e.iter().map(|x| x.0).collect::<Vec<_>>(),
98-
right.e.iter().map(|x| x.0).collect::<Vec<_>>(),
99-
"{:?}",
98+
right.e.iter().map(|x| x.0).collect::<Vec<_>>(),
99+
"{:?}",
100100
diff
101101
);
102102
assert_eq_unordered_sort!(self.f, right.f, "{:?}", diff);

src/collections/unordered_array_like.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,10 @@ impl<'a, T: Clone + 'a> From<UnorderedArrayLikeDiff<&'a T>> for UnorderedArrayLi
8585
}
8686
}
8787

88-
fn collect_into_map<
89-
'a,
90-
T: Hash + PartialEq + Eq + 'a,
91-
B: Iterator<Item = T> + ExactSizeIterator,
92-
>(
88+
fn collect_into_map<'a, T: Hash + PartialEq + Eq + 'a, B: Iterator<Item = T>>(
9389
list: B,
9490
) -> HashMap<T, usize> {
95-
let mut map: HashMap<T, usize> = HashMap::with_capacity(list.len());
91+
let mut map: HashMap<T, usize> = HashMap::with_capacity(list.size_hint().1.unwrap_or_default());
9692
for item in list {
9793
match map.get_mut(&item) {
9894
Some(count) => *count += 1,
@@ -149,7 +145,7 @@ pub fn unordered_hashcmp<
149145
'a,
150146
#[cfg(feature = "nanoserde")] T: Hash + Clone + PartialEq + Eq + SerBin + DeBin + 'a,
151147
#[cfg(not(feature = "nanoserde"))] T: Hash + Clone + PartialEq + Eq + 'a,
152-
B: Iterator<Item = &'a T> + ExactSizeIterator,
148+
B: Iterator<Item = &'a T>,
153149
>(
154150
previous: B,
155151
current: B,
@@ -168,7 +164,8 @@ pub fn unordered_hashcmp<
168164
));
169165
}
170166

171-
let mut ret: Vec<UnorderedArrayLikeChange<&T>> = vec![];
167+
let mut ret: Vec<UnorderedArrayLikeChange<&T>> =
168+
Vec::with_capacity((previous.len() + current.len()) >> 1);
172169

173170
for (k, current_count) in current.iter() {
174171
match previous.remove(k) {
@@ -207,6 +204,8 @@ pub fn unordered_hashcmp<
207204
ret.push(UnorderedArrayLikeChange::new(k, v, InsertOrRemove::Remove))
208205
}
209206

207+
ret.shrink_to_fit();
208+
210209
match ret.is_empty() {
211210
true => None,
212211
false => Some(UnorderedArrayLikeDiff(

src/collections/unordered_map_like.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ fn collect_into_key_eq_map<
111111
>(
112112
list: B,
113113
) -> HashMap<&'a K, (&'a V, usize)> {
114-
let mut map: HashMap<&K, (&V, usize)> = HashMap::new();
114+
let mut map: HashMap<&K, (&V, usize)> =
115+
HashMap::with_capacity(list.size_hint().1.unwrap_or_default());
115116
for (key, value) in list {
116117
match map.get_mut(&key) {
117118
Some((_, count)) => *count += 1,
@@ -225,7 +226,8 @@ pub fn unordered_hashcmp<
225226
)));
226227
}
227228

228-
let mut ret: Vec<UnorderedMapLikeChange<&'a K, &'a V>> = vec![];
229+
let mut ret: Vec<UnorderedMapLikeChange<&'a K, &'a V>> =
230+
Vec::with_capacity((previous.len() + current.len()) >> 1);
229231

230232
for (&k, &(v, current_count)) in current.iter() {
231233
match previous.remove(&k) {
@@ -283,6 +285,8 @@ pub fn unordered_hashcmp<
283285
))
284286
}
285287

288+
ret.shrink_to_fit();
289+
286290
match ret.is_empty() {
287291
true => None,
288292
false => Some(UnorderedMapLikeDiff(UnorderedMapLikeDiffInternal::Modify(

src/collections/unordered_map_like_recursive.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn collect_into_key_eq_map<
110110
>(
111111
list: B,
112112
) -> HashMap<&'a K, &'a V> {
113-
let mut map: HashMap<&K, &V> = HashMap::new();
113+
let mut map: HashMap<&K, &V> = HashMap::with_capacity(list.size_hint().1.unwrap_or_default());
114114
for (key, value) in list {
115115
map.insert(key, value);
116116
}
@@ -170,7 +170,8 @@ pub fn unordered_hashcmp<
170170
));
171171
}
172172

173-
let mut ret: Vec<UnorderedMapLikeRecursiveChangeRef<'a, K, V>> = vec![];
173+
let mut ret: Vec<UnorderedMapLikeRecursiveChangeRef<'a, K, V>> =
174+
Vec::with_capacity((previous.len() + current.len()) >> 1);
174175

175176
for prev_entry in previous.into_iter() {
176177
if current.remove_entry(prev_entry.0).is_none() {
@@ -188,6 +189,8 @@ pub fn unordered_hashcmp<
188189
))
189190
}
190191

192+
ret.shrink_to_fit();
193+
191194
match ret.is_empty() {
192195
true => None,
193196
false => Some(UnorderedMapLikeRecursiveDiffRef(
@@ -203,7 +206,8 @@ pub fn unordered_hashcmp<
203206
));
204207
}
205208

206-
let mut ret: Vec<UnorderedMapLikeRecursiveChangeRef<'a, K, V>> = vec![];
209+
let mut ret: Vec<UnorderedMapLikeRecursiveChangeRef<'a, K, V>> =
210+
Vec::with_capacity((previous.len() + current.len()) >> 1);
207211

208212
for prev_entry in previous.into_iter() {
209213
match current.remove_entry(prev_entry.0) {
@@ -228,6 +232,8 @@ pub fn unordered_hashcmp<
228232
))
229233
}
230234

235+
ret.shrink_to_fit();
236+
231237
match ret.is_empty() {
232238
true => None,
233239
false => Some(UnorderedMapLikeRecursiveDiffRef(
@@ -245,7 +251,7 @@ pub fn apply_unordered_hashdiffs<
245251
>(
246252
list: B,
247253
diffs: UnorderedMapLikeRecursiveDiffOwned<K, V>,
248-
) -> Box<dyn Iterator<Item = (K, V)>> {
254+
) -> Box<dyn ExactSizeIterator<Item = (K, V)>> {
249255
let diffs = match diffs {
250256
UnorderedMapLikeRecursiveDiffOwned(
251257
UnorderedMapLikeRecursiveDiffInternalOwned::Replace(replacement),

0 commit comments

Comments
 (0)