@@ -1267,7 +1267,7 @@ impl<A: Allocator + Clone> RawTableInner<A> {
12671267 hash : u64 ,
12681268 eq : & mut dyn FnMut ( usize ) -> bool ,
12691269 ) -> ( usize , bool ) {
1270- let mut tombstone = None ;
1270+ let mut insert_slot = None ;
12711271
12721272 let h2_hash = h2 ( hash) ;
12731273 let mut probe_seq = self . probe_seq ( hash) ;
@@ -1283,22 +1283,22 @@ impl<A: Allocator + Clone> RawTableInner<A> {
12831283 }
12841284 }
12851285
1286- let index = self . find_insert_slot_in_group ( & group, & probe_seq) ;
1286+ // We didn't find the element we were looking for in the group, try to get an
1287+ // insertion slot from the group if we don't have one yet.
1288+ if likely ( insert_slot. is_none ( ) ) {
1289+ insert_slot = self . find_insert_slot_in_group ( & group, & probe_seq) ;
1290+ }
12871291
1288- if likely ( index. is_some ( ) ) {
1289- // Only stop the search if the group is empty. The element might be
1290- // in a following group.
1291- if likely ( group. match_empty ( ) . any_bit_set ( ) ) {
1292- // Use a tombstone if we found one
1293- return if unlikely ( tombstone. is_some ( ) ) {
1294- ( tombstone. unwrap ( ) , false )
1295- } else {
1296- ( index. unwrap ( ) , false )
1297- } ;
1298- } else {
1299- // We found a tombstone, record it so we can return it as a potential
1300- // insertion location.
1301- tombstone = index;
1292+ // Only stop the search if the group contains at least one empty element.
1293+ // Otherwise, the element that we are looking for might be in a following group.
1294+ if likely ( group. match_empty ( ) . any_bit_set ( ) ) {
1295+ // We must have found a insert slot by now, since the current group contains at
1296+ // least one. For tables smaller than the group width, there will still be an
1297+ // empty element in the current (and only) group due to the load factor.
1298+ debug_assert ! ( insert_slot. is_some( ) ) ;
1299+ match insert_slot {
1300+ Some ( insert_slot) => return ( insert_slot, false ) ,
1301+ None => unsafe { hint:: unreachable_unchecked ( ) } ,
13021302 }
13031303 }
13041304
0 commit comments