Skip to content

Commit 738a41b

Browse files
Rollup merge of #77449 - ssomers:btree_drain_filter_size_hint, r=Mark-Simulacrum
BTreeMap: comment why drain_filter's size_hint is somewhat pessimistic The `size_hint` of the `DrainFilter` iterator doesn't adjust as you iterate. This hardly seems important to me, but there has been a comparable PR #64383 in the past. I guess a scenario is that you first iterate half the map manually and keep most of the key/value pairs in the map, and then tell the predicate to drain most of the key/value pairs and `.collect` the iterator over the remaining half of the map. I am totally ambivalent whether this is better or not. r? @Mark-Simulacrum
2 parents 2766b72 + 3b051d0 commit 738a41b

File tree

1 file changed

+4
-0
lines changed
  • library/alloc/src/collections/btree

1 file changed

+4
-0
lines changed

library/alloc/src/collections/btree/map.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,10 @@ impl<'a, K: 'a, V: 'a> DrainFilterInner<'a, K, V> {
17831783

17841784
/// Implementation of a typical `DrainFilter::size_hint` method.
17851785
pub(super) fn size_hint(&self) -> (usize, Option<usize>) {
1786+
// In most of the btree iterators, `self.length` is the number of elements
1787+
// yet to be visited. Here, it includes elements that were visited and that
1788+
// the predicate decided not to drain. Making this upper bound more accurate
1789+
// requires maintaining an extra field and is not worth while.
17861790
(0, Some(*self.length))
17871791
}
17881792
}

0 commit comments

Comments
 (0)