Skip to content

Commit e04c18d

Browse files
Markus WesterlindMarwes
authored andcommitted
refactor: Merge set_ctrl_h2 into find_insert_slot when possible
1 parent 179a942 commit e04c18d

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/raw/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,7 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
780780
// - there are no DELETED entries.
781781
// - we know there is enough space in the table.
782782
// - all elements are unique.
783-
let index = new_table.find_insert_slot(hash);
784-
new_table.set_ctrl_h2(index, hash);
783+
let index = new_table.prepare_insert_slot(hash);
785784
new_table.bucket(index).copy_from_nonoverlapping(&item);
786785
}
787786

@@ -863,15 +862,14 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
863862
#[cfg(any(feature = "raw", feature = "rustc-internal-api"))]
864863
pub fn insert_no_grow(&mut self, hash: u64, value: T) -> Bucket<T> {
865864
unsafe {
866-
let index = self.table.find_insert_slot(hash);
865+
let index = self.table.prepare_insert_slot(hash);
867866
let bucket = self.table.bucket(index);
868867

869868
// If we are replacing a DELETED entry then we don't need to update
870869
// the load counter.
871870
let old_ctrl = *self.table.ctrl(index);
872871
self.table.growth_left -= special_is_empty(old_ctrl) as usize;
873872

874-
self.table.set_ctrl_h2(index, hash);
875873
bucket.write(value);
876874
self.table.items += 1;
877875
bucket
@@ -1124,6 +1122,17 @@ impl<A: Allocator + Clone> RawTableInner<A> {
11241122
}
11251123
}
11261124

1125+
/// Searches for an empty or deleted bucket which is suitable for inserting
1126+
/// a new element and sets the hash for that slot.
1127+
///
1128+
/// There must be at least 1 empty bucket in the table.
1129+
#[inline]
1130+
unsafe fn prepare_insert_slot(&self, hash: u64) -> usize {
1131+
let index = self.find_insert_slot(hash);
1132+
self.set_ctrl_h2(index, hash);
1133+
index
1134+
}
1135+
11271136
/// Searches for an empty or deleted bucket which is suitable for inserting
11281137
/// a new element.
11291138
///
@@ -1583,8 +1592,7 @@ impl<T: Clone, A: Allocator + Clone> RawTable<T, A> {
15831592
// - there are no DELETED entries.
15841593
// - we know there is enough space in the table.
15851594
// - all elements are unique.
1586-
let index = guard_self.table.find_insert_slot(hash);
1587-
guard_self.table.set_ctrl_h2(index, hash);
1595+
let index = guard_self.table.prepare_insert_slot(hash);
15881596
guard_self.bucket(index).write(item);
15891597
}
15901598
}

0 commit comments

Comments
 (0)