Skip to content

Commit c6d8f02

Browse files
committed
Refactor find_potential_inner
1 parent ec878d8 commit c6d8f02

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/raw/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ impl<A: Allocator + Clone> RawTableInner<A> {
11661166
hash: u64,
11671167
eq: &mut dyn FnMut(usize) -> bool,
11681168
) -> (usize, bool) {
1169-
let mut tombstone = None;
1169+
let mut insert_slot = None;
11701170

11711171
let h2_hash = h2(hash);
11721172
let mut probe_seq = self.probe_seq(hash);
@@ -1182,22 +1182,22 @@ impl<A: Allocator + Clone> RawTableInner<A> {
11821182
}
11831183
}
11841184

1185-
let index = self.find_insert_slot_in_group(&group, &probe_seq);
1185+
// We didn't find the element we were looking for in the group, try to get an
1186+
// insertion slot from the group if we don't have one yet.
1187+
if likely(insert_slot.is_none()) {
1188+
insert_slot = self.find_insert_slot_in_group(&group, &probe_seq);
1189+
}
11861190

1187-
if likely(index.is_some()) {
1188-
// Only stop the search if the group is empty. The element might be
1189-
// in a following group.
1190-
if likely(group.match_empty().any_bit_set()) {
1191-
// Use a tombstone if we found one
1192-
return if unlikely(tombstone.is_some()) {
1193-
(tombstone.unwrap(), false)
1194-
} else {
1195-
(index.unwrap(), false)
1196-
};
1197-
} else {
1198-
// We found a tombstone, record it so we can return it as a potential
1199-
// insertion location.
1200-
tombstone = index;
1191+
// Only stop the search if the group contains at least one empty element.
1192+
// Otherwise, the element that we are looking for might be in a following group.
1193+
if likely(group.match_empty().any_bit_set()) {
1194+
// We must have found a insert slot by now, since the current group contains at
1195+
// least one. For tables smaller than the group width, there will still be an
1196+
// empty element in the current (and only) group due to the load factor.
1197+
debug_assert!(insert_slot.is_some());
1198+
match insert_slot {
1199+
Some(insert_slot) => return (insert_slot, false),
1200+
None => unsafe { hint::unreachable_unchecked() },
12011201
}
12021202
}
12031203

0 commit comments

Comments
 (0)