Skip to content

Commit d162c63

Browse files
committed
Avoid unnecessary branch
1 parent e4aca8d commit d162c63

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

src/raw/mod.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,46 +1491,42 @@ impl<T> RawIter<T> {
14911491
if let Some(index) = self.iter.current_group.lowest_set_bit() {
14921492
let next_bucket = self.iter.data.next_n(index);
14931493
use core::cmp::Ordering;
1494-
match b.as_ptr().cmp(&next_bucket.as_ptr()) {
1495-
Ordering::Greater => {
1496-
// The toggled bucket is "before" the bucket the iterator would yield next.
1497-
// We therefore don't need to do anything --- the iterator has already
1498-
// passed the bucket in question.
1499-
//
1500-
// The item count must already be correct, since a removal or insert
1501-
// "prior" to the iterator's position wouldn't affect the item count.
1502-
}
1503-
Ordering::Equal => {
1504-
// The toggled bucket was coming up next, so we just skip it.
1505-
self.iter.current_group = self.iter.current_group.remove_lowest_bit();
1506-
// It must be a removal, since its bit was set.
1494+
if b.as_ptr() > next_bucket.as_ptr() {
1495+
// The toggled bucket is "before" the bucket the iterator would yield next. We
1496+
// therefore don't need to do anything --- the iterator has already passed the
1497+
// bucket in question.
1498+
//
1499+
// The item count must already be correct, since a removal or insert "prior" to
1500+
// the iterator's position wouldn't affect the item count.
1501+
} else {
1502+
// The removed bucket is an upcoming bucket. We need to make sure it does _not_
1503+
// get yielded, and also that it's no longer included in the item count.
1504+
//
1505+
// NOTE: We can't just reload the group here, both since that might reflect
1506+
// inserts we've already passed, and because that might inadvertently unset the
1507+
// bits for _other_ removals. If we do that, we'd have to also decrement the
1508+
// item count for those other bits that we unset. But the presumably subsequent
1509+
// call to reflect for those buckets might _also_ decrement the item count.
1510+
// Instead, we _just_ flip the bit for the particular bucket the caller asked
1511+
// us to reflect.
1512+
let our_bit = offset_from(self.iter.data.as_ptr(), b.as_ptr());
1513+
let was_full = self.iter.current_group.flip(our_bit);
1514+
debug_assert_ne!(was_full, is_insert);
1515+
1516+
if is_insert {
1517+
self.items += 1;
1518+
} else {
15071519
self.items -= 1;
1508-
debug_assert!(!is_insert);
15091520
}
1510-
Ordering::Less => {
1511-
// The removed bucket is an upcoming bucket. We need to make sure it does
1512-
// _not_ get yielded, and also that it's no longer included in the item
1513-
// count.
1514-
//
1515-
// NOTE: We can't just reload the group here, both since that might reflect
1516-
// inserts we've already passed, and because that might inadvertently unset
1517-
// the bits for _other_ removals. If we do that, we'd have to also
1518-
// decrement the item count for those other bits that we unset. But the
1519-
// presumably subsequent call to reflect for those buckets might _also_
1520-
// decrement the item count. Instead, we _just_ flip the bit for the
1521-
// particular bucket the caller asked us to reflect.
1522-
let our_bit = offset_from(self.iter.data.as_ptr(), b.as_ptr());
1523-
let was_full = self.iter.current_group.flip(our_bit);
1524-
debug_assert_ne!(was_full, is_insert);
1525-
1526-
if is_insert {
1527-
self.items += 1;
1521+
1522+
if cfg!(debug_assertions) {
1523+
if b.as_ptr() == next_bucket.as_ptr() {
1524+
// The removed bucket should no longer be next
1525+
debug_assert_ne!(self.iter.current_group.lowest_set_bit(), Some(index));
15281526
} else {
1529-
self.items -= 1;
1527+
// We should not have changed what bucket comes next.
1528+
debug_assert_eq!(self.iter.current_group.lowest_set_bit(), Some(index));
15301529
}
1531-
1532-
// We should not have changed what bucket comes next.
1533-
debug_assert_eq!(self.iter.current_group.lowest_set_bit(), Some(index));
15341530
}
15351531
}
15361532
} else {

0 commit comments

Comments
 (0)