Skip to content

Commit dfbba65

Browse files
authored
Rollup merge of #73627 - ssomers:btree_iter_min_max, r=Mark-Simulacrum
Shortcuts for min/max on double-ended BTreeMap/BTreeSet iterators Closes #59947: a performance tweak that might benefit some. Optimizes `min` and `max ` on all btree double-ended iterators that do not drop, i.e. the iterators created by: - `BTreeMap::iter` - `BTreeMap::iter_mut` - `BTreeMap::keys` and `BTreeSet::iter` - `BTreeMap::range` and `BTreeSet::range` - `BTreeMap::range_mut` Also in these (currently) single-ended iterators, but obviously for `min` only: - `BTreeSet::difference` - `BTreeSet::intersection` - `BTreeSet::symmetric_difference` - `BTreeSet::union` Did not do this in iterators created by `into_iter` to preserve drop order, as outlined in #62316. Did not do this in iterators created by `drain_filter`, possibly to preserve drop order, possibly to preserve predicate invocation, mostly to not have to think about it too hard (I guess maybe it wouldn't be a change for `min`, which is the only shortcut possible in this single-ended iterator).
2 parents 92af945 + 42062a5 commit dfbba65

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

src/liballoc/collections/btree/map.rs

+40
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,14 @@ impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V> {
13961396
fn last(mut self) -> Option<(&'a K, &'a V)> {
13971397
self.next_back()
13981398
}
1399+
1400+
fn min(mut self) -> Option<(&'a K, &'a V)> {
1401+
self.next()
1402+
}
1403+
1404+
fn max(mut self) -> Option<(&'a K, &'a V)> {
1405+
self.next_back()
1406+
}
13991407
}
14001408

14011409
#[stable(feature = "fused", since = "1.26.0")]
@@ -1458,6 +1466,14 @@ impl<'a, K: 'a, V: 'a> Iterator for IterMut<'a, K, V> {
14581466
fn last(mut self) -> Option<(&'a K, &'a mut V)> {
14591467
self.next_back()
14601468
}
1469+
1470+
fn min(mut self) -> Option<(&'a K, &'a mut V)> {
1471+
self.next()
1472+
}
1473+
1474+
fn max(mut self) -> Option<(&'a K, &'a mut V)> {
1475+
self.next_back()
1476+
}
14611477
}
14621478

14631479
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1595,6 +1611,14 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
15951611
fn last(mut self) -> Option<&'a K> {
15961612
self.next_back()
15971613
}
1614+
1615+
fn min(mut self) -> Option<&'a K> {
1616+
self.next()
1617+
}
1618+
1619+
fn max(mut self) -> Option<&'a K> {
1620+
self.next_back()
1621+
}
15981622
}
15991623

16001624
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1768,6 +1792,14 @@ impl<'a, K, V> Iterator for Range<'a, K, V> {
17681792
fn last(mut self) -> Option<(&'a K, &'a V)> {
17691793
self.next_back()
17701794
}
1795+
1796+
fn min(mut self) -> Option<(&'a K, &'a V)> {
1797+
self.next()
1798+
}
1799+
1800+
fn max(mut self) -> Option<(&'a K, &'a V)> {
1801+
self.next_back()
1802+
}
17711803
}
17721804

17731805
#[stable(feature = "map_values_mut", since = "1.10.0")]
@@ -1853,6 +1885,14 @@ impl<'a, K, V> Iterator for RangeMut<'a, K, V> {
18531885
fn last(mut self) -> Option<(&'a K, &'a mut V)> {
18541886
self.next_back()
18551887
}
1888+
1889+
fn min(mut self) -> Option<(&'a K, &'a mut V)> {
1890+
self.next()
1891+
}
1892+
1893+
fn max(mut self) -> Option<(&'a K, &'a mut V)> {
1894+
self.next_back()
1895+
}
18561896
}
18571897

18581898
impl<'a, K, V> RangeMut<'a, K, V> {

src/liballoc/collections/btree/set.rs

+35
Original file line numberDiff line numberDiff line change
@@ -1291,12 +1291,22 @@ impl<'a, T> Iterator for Iter<'a, T> {
12911291
fn next(&mut self) -> Option<&'a T> {
12921292
self.iter.next()
12931293
}
1294+
12941295
fn size_hint(&self) -> (usize, Option<usize>) {
12951296
self.iter.size_hint()
12961297
}
1298+
12971299
fn last(mut self) -> Option<&'a T> {
12981300
self.next_back()
12991301
}
1302+
1303+
fn min(mut self) -> Option<&'a T> {
1304+
self.next()
1305+
}
1306+
1307+
fn max(mut self) -> Option<&'a T> {
1308+
self.next_back()
1309+
}
13001310
}
13011311
#[stable(feature = "rust1", since = "1.0.0")]
13021312
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
@@ -1321,6 +1331,7 @@ impl<T> Iterator for IntoIter<T> {
13211331
fn next(&mut self) -> Option<T> {
13221332
self.iter.next().map(|(k, _)| k)
13231333
}
1334+
13241335
fn size_hint(&self) -> (usize, Option<usize>) {
13251336
self.iter.size_hint()
13261337
}
@@ -1359,6 +1370,14 @@ impl<'a, T> Iterator for Range<'a, T> {
13591370
fn last(mut self) -> Option<&'a T> {
13601371
self.next_back()
13611372
}
1373+
1374+
fn min(mut self) -> Option<&'a T> {
1375+
self.next()
1376+
}
1377+
1378+
fn max(mut self) -> Option<&'a T> {
1379+
self.next_back()
1380+
}
13621381
}
13631382

13641383
#[stable(feature = "btree_range", since = "1.17.0")]
@@ -1429,6 +1448,10 @@ impl<'a, T: Ord> Iterator for Difference<'a, T> {
14291448
};
14301449
(self_len.saturating_sub(other_len), Some(self_len))
14311450
}
1451+
1452+
fn min(mut self) -> Option<&'a T> {
1453+
self.next()
1454+
}
14321455
}
14331456

14341457
#[stable(feature = "fused", since = "1.26.0")]
@@ -1460,6 +1483,10 @@ impl<'a, T: Ord> Iterator for SymmetricDifference<'a, T> {
14601483
// the number of elements to less than half the range of usize.
14611484
(0, Some(a_len + b_len))
14621485
}
1486+
1487+
fn min(mut self) -> Option<&'a T> {
1488+
self.next()
1489+
}
14631490
}
14641491

14651492
#[stable(feature = "fused", since = "1.26.0")]
@@ -1516,6 +1543,10 @@ impl<'a, T: Ord> Iterator for Intersection<'a, T> {
15161543
IntersectionInner::Answer(Some(_)) => (1, Some(1)),
15171544
}
15181545
}
1546+
1547+
fn min(mut self) -> Option<&'a T> {
1548+
self.next()
1549+
}
15191550
}
15201551

15211552
#[stable(feature = "fused", since = "1.26.0")]
@@ -1541,6 +1572,10 @@ impl<'a, T: Ord> Iterator for Union<'a, T> {
15411572
// No checked_add - see SymmetricDifference::size_hint.
15421573
(max(a_len, b_len), Some(a_len + b_len))
15431574
}
1575+
1576+
fn min(mut self) -> Option<&'a T> {
1577+
self.next()
1578+
}
15441579
}
15451580

15461581
#[stable(feature = "fused", since = "1.26.0")]

src/liballoc/tests/btree/map.rs

+35
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,41 @@ fn test_iter_mixed() {
309309
test(size, map.into_iter());
310310
}
311311

312+
#[test]
313+
fn test_iter_min_max() {
314+
let mut a = BTreeMap::new();
315+
assert_eq!(a.iter().min(), None);
316+
assert_eq!(a.iter().max(), None);
317+
assert_eq!(a.iter_mut().min(), None);
318+
assert_eq!(a.iter_mut().max(), None);
319+
assert_eq!(a.range(..).min(), None);
320+
assert_eq!(a.range(..).max(), None);
321+
assert_eq!(a.range_mut(..).min(), None);
322+
assert_eq!(a.range_mut(..).max(), None);
323+
assert_eq!(a.keys().min(), None);
324+
assert_eq!(a.keys().max(), None);
325+
assert_eq!(a.values().min(), None);
326+
assert_eq!(a.values().max(), None);
327+
assert_eq!(a.values_mut().min(), None);
328+
assert_eq!(a.values_mut().max(), None);
329+
a.insert(1, 42);
330+
a.insert(2, 24);
331+
assert_eq!(a.iter().min(), Some((&1, &42)));
332+
assert_eq!(a.iter().max(), Some((&2, &24)));
333+
assert_eq!(a.iter_mut().min(), Some((&1, &mut 42)));
334+
assert_eq!(a.iter_mut().max(), Some((&2, &mut 24)));
335+
assert_eq!(a.range(..).min(), Some((&1, &42)));
336+
assert_eq!(a.range(..).max(), Some((&2, &24)));
337+
assert_eq!(a.range_mut(..).min(), Some((&1, &mut 42)));
338+
assert_eq!(a.range_mut(..).max(), Some((&2, &mut 24)));
339+
assert_eq!(a.keys().min(), Some(&1));
340+
assert_eq!(a.keys().max(), Some(&2));
341+
assert_eq!(a.values().min(), Some(&24));
342+
assert_eq!(a.values().max(), Some(&42));
343+
assert_eq!(a.values_mut().min(), Some(&mut 24));
344+
assert_eq!(a.values_mut().max(), Some(&mut 42));
345+
}
346+
312347
fn range_keys(map: &BTreeMap<i32, i32>, range: impl RangeBounds<i32>) -> Vec<i32> {
313348
map.range(range)
314349
.map(|(&k, &v)| {

src/liballoc/tests/btree/set.rs

+31
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,37 @@ fn test_hash() {
3333
assert_eq!(hash(&x), hash(&y));
3434
}
3535

36+
#[test]
37+
fn test_iter_min_max() {
38+
let mut a = BTreeSet::new();
39+
assert_eq!(a.iter().min(), None);
40+
assert_eq!(a.iter().max(), None);
41+
assert_eq!(a.range(..).min(), None);
42+
assert_eq!(a.range(..).max(), None);
43+
assert_eq!(a.difference(&BTreeSet::new()).min(), None);
44+
assert_eq!(a.difference(&BTreeSet::new()).max(), None);
45+
assert_eq!(a.intersection(&a).min(), None);
46+
assert_eq!(a.intersection(&a).max(), None);
47+
assert_eq!(a.symmetric_difference(&BTreeSet::new()).min(), None);
48+
assert_eq!(a.symmetric_difference(&BTreeSet::new()).max(), None);
49+
assert_eq!(a.union(&a).min(), None);
50+
assert_eq!(a.union(&a).max(), None);
51+
a.insert(1);
52+
a.insert(2);
53+
assert_eq!(a.iter().min(), Some(&1));
54+
assert_eq!(a.iter().max(), Some(&2));
55+
assert_eq!(a.range(..).min(), Some(&1));
56+
assert_eq!(a.range(..).max(), Some(&2));
57+
assert_eq!(a.difference(&BTreeSet::new()).min(), Some(&1));
58+
assert_eq!(a.difference(&BTreeSet::new()).max(), Some(&2));
59+
assert_eq!(a.intersection(&a).min(), Some(&1));
60+
assert_eq!(a.intersection(&a).max(), Some(&2));
61+
assert_eq!(a.symmetric_difference(&BTreeSet::new()).min(), Some(&1));
62+
assert_eq!(a.symmetric_difference(&BTreeSet::new()).max(), Some(&2));
63+
assert_eq!(a.union(&a).min(), Some(&1));
64+
assert_eq!(a.union(&a).max(), Some(&2));
65+
}
66+
3667
fn check<F>(a: &[i32], b: &[i32], expected: &[i32], f: F)
3768
where
3869
F: FnOnce(&BTreeSet<i32>, &BTreeSet<i32>, &mut dyn FnMut(&i32) -> bool) -> bool,

0 commit comments

Comments
 (0)