Skip to content

Commit 0316f9e

Browse files
committed
Tweak insert for code size
1 parent 8560169 commit 0316f9e

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

src/map.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,12 +1264,13 @@ where
12641264
.reserve(1, make_hasher::<K, _, V, S>(&self.hash_builder));
12651265

12661266
unsafe {
1267-
match self.table.find_potential(hash, equivalent_key(&k)) {
1268-
Ok(bucket) => Some(mem::replace(&mut bucket.as_mut().1, v)),
1269-
Err(index) => {
1270-
self.table.insert_potential(hash, (k, v), index);
1271-
None
1272-
}
1267+
let (index, found) = self.table.find_potential(hash, equivalent_key(&k));
1268+
1269+
if found {
1270+
Some(mem::replace(&mut self.table.bucket(index).as_mut().1, v))
1271+
} else {
1272+
self.table.insert_potential(hash, (k, v), index);
1273+
None
12731274
}
12741275
}
12751276
}

src/raw/mod.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -915,23 +915,10 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
915915
/// Searches for an element in the table,
916916
/// or a potential slot where that element could be inserted.
917917
#[inline]
918-
pub fn find_potential(
919-
&self,
920-
hash: u64,
921-
mut eq: impl FnMut(&T) -> bool,
922-
) -> Result<Bucket<T>, usize> {
923-
unsafe {
924-
let result = self.table.find_potential_inner(hash, &mut |index| {
925-
let bucket = self.bucket(index);
926-
let elm = bucket.as_ref();
927-
eq(elm)
928-
});
929-
930-
match result {
931-
Ok(index) => Ok(self.bucket(index)),
932-
Err(index) => Err(index),
933-
}
934-
}
918+
pub fn find_potential(&self, hash: u64, mut eq: impl FnMut(&T) -> bool) -> (usize, bool) {
919+
self.table.find_potential_inner(hash, &mut |index| unsafe {
920+
eq(self.bucket(index).as_ref())
921+
})
935922
}
936923

937924
/// Inserts an element in the table at a potential slot as returned by `find_potential`.
@@ -1224,12 +1211,12 @@ impl<A: Allocator + Clone> RawTableInner<A> {
12241211
/// Searches for an element in the table, stopping at the group where `stop` returns `Some` and
12251212
/// no elements matched. Returns the bucket that matches or the result of `stop`.
12261213
#[inline]
1227-
unsafe fn search<R>(
1214+
unsafe fn search(
12281215
&self,
12291216
hash: u64,
12301217
eq: &mut dyn FnMut(usize) -> bool,
1231-
mut stop: impl FnMut(&Group, &ProbeSeq) -> Option<R>,
1232-
) -> Result<usize, R> {
1218+
mut stop: impl FnMut(&Group, &ProbeSeq) -> Option<usize>,
1219+
) -> (usize, bool) {
12331220
let h2_hash = h2(hash);
12341221
let mut probe_seq = self.probe_seq(hash);
12351222

@@ -1240,12 +1227,12 @@ impl<A: Allocator + Clone> RawTableInner<A> {
12401227
let index = (probe_seq.pos + bit) & self.bucket_mask;
12411228

12421229
if likely(eq(index)) {
1243-
return Ok(index);
1230+
return (index, true);
12441231
}
12451232
}
12461233

12471234
if let Some(stop) = stop(&group, &probe_seq) {
1248-
return Err(stop);
1235+
return (stop, false);
12491236
}
12501237

12511238
probe_seq.move_next(self.bucket_mask);
@@ -1295,7 +1282,7 @@ impl<A: Allocator + Clone> RawTableInner<A> {
12951282
&self,
12961283
hash: u64,
12971284
eq: &mut dyn FnMut(usize) -> bool,
1298-
) -> Result<usize, usize> {
1285+
) -> (usize, bool) {
12991286
unsafe {
13001287
let mut tombstone = None;
13011288
self.search(hash, eq, |group, probe_seq| {

0 commit comments

Comments
 (0)