Skip to content

Commit 681fc6a

Browse files
author
Markus Westerlind
committed
Shrink try_insert_no_grow
1 parent 87c3367 commit 681fc6a

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/raw/mod.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -865,15 +865,13 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
865865
#[cfg_attr(feature = "inline-more", inline)]
866866
pub fn try_insert_no_grow(&mut self, hash: u64, value: T) -> Result<Bucket<T>, T> {
867867
unsafe {
868-
let index = self.table.find_insert_slot(hash);
869-
let old_ctrl = *self.table.ctrl(index);
870-
if unlikely(self.table.growth_left == 0 && special_is_empty(old_ctrl)) {
871-
Err(value)
872-
} else {
873-
self.table.record_item_insert_at(index, old_ctrl, hash);
874-
let bucket = self.bucket(index);
875-
bucket.write(value);
876-
Ok(bucket)
868+
match self.table.prepare_insert_no_grow(hash) {
869+
Ok(index) => {
870+
let bucket = self.bucket(index);
871+
bucket.write(value);
872+
Ok(bucket)
873+
}
874+
Err(()) => Err(value),
877875
}
878876
}
879877
}
@@ -1266,6 +1264,21 @@ impl<A: Allocator + Clone> RawTableInner<A> {
12661264
}
12671265
}
12681266

1267+
/// Returns the index of a bucket for which a value must be inserted if there is enough rooom
1268+
/// in the table, otherwise returns error
1269+
#[cfg(feature = "raw")]
1270+
#[inline]
1271+
unsafe fn prepare_insert_no_grow(&mut self, hash: u64) -> Result<usize, ()> {
1272+
let index = self.find_insert_slot(hash);
1273+
let old_ctrl = *self.ctrl(index);
1274+
if unlikely(self.growth_left == 0 && special_is_empty(old_ctrl)) {
1275+
Err(())
1276+
} else {
1277+
self.record_item_insert_at(index, old_ctrl, hash);
1278+
Ok(index)
1279+
}
1280+
}
1281+
12691282
#[inline]
12701283
unsafe fn record_item_insert_at(&mut self, index: usize, old_ctrl: u8, hash: u64) {
12711284
self.growth_left -= special_is_empty(old_ctrl) as usize;

0 commit comments

Comments
 (0)