From 3f1e0963a07375ba5b2b500f56bba7c4956334f1 Mon Sep 17 00:00:00 2001 From: Laurence Tratt Date: Mon, 31 Aug 2026 10:00:30 +0100 Subject: [PATCH] Remove inaccurate hints. `iter_set_bits` and `iter_unset_bits` don't know -- unless one does `count_set_bits` - how many bits they will yield. That means that `size_hint` on these two iterators returned the size of the entire Vob which is always `>=` the number of set/unset bits. If used in idioms along the lines of `vob.iter_set_bits().collect()` this could cause significant overallocation. An open question is whether, in general, it's better to have `size_hint` call `count_[un]set_bits`. My guess is, in general, "no", but either way, for now the best thing to do is stop handing out misleading size hints. --- src/lib.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a9b2e6d..a999c01 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1202,10 +1202,6 @@ impl Iterator for IterSetBits<'_, T> { None } - fn size_hint(&self) -> (usize, Option) { - self.range.size_hint() - } - fn count(self) -> usize { self.vob.count_set_bits(self.range) } @@ -1286,10 +1282,6 @@ impl Iterator for IterUnsetBits<'_, T> { None } - fn size_hint(&self) -> (usize, Option) { - self.range.size_hint() - } - fn count(self) -> usize { // This arithmetic is safe because (self.range.end - self.range.start) is the total number of bits, // and self.vob.count_set_bits() always returns a value less than or equal to that.