Skip to content

Commit

Permalink
Merge #61
Browse files Browse the repository at this point in the history
61: Improve worst-case performance of HashSet.is_subset r=Amanieu a=Amanieu

Ported from rust-lang/rust#59665

Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
  • Loading branch information
bors[bot] and Amanieu committed Apr 13, 2019
2 parents 9068eb7 + 9facea7 commit ab3e4ea
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/external_trait_impls/rayon/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ where
///
/// This method runs in a potentially parallel fashion.
pub fn par_is_subset(&self, other: &Self) -> bool {
self.into_par_iter().all(|x| other.contains(x))
if self.len() <= other.len() {
self.into_par_iter().all(|x| other.contains(x))
} else {
false
}
}

/// Returns `true` if the set is a superset of another,
Expand Down
4 changes: 2 additions & 2 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,8 +1646,8 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
{
unsafe {
let elem = self.table.insert(hash, (key, value), |x| hasher(&x.0));
let &mut (ref mut key, ref mut value) = elem.as_mut();
(key, value)
let &mut (ref mut k, ref mut v) = elem.as_mut();
(k, v)
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn h2(hash: u64) -> u8 {
/// (skipping over 1 group), then 3 groups (skipping over 2 groups), and so on.
///
/// Proof that the probe will visit every group in the table:
/// https://fgiesen.wordpress.com/2015/02/22/triangular-numbers-mod-2n/
/// <https://fgiesen.wordpress.com/2015/02/22/triangular-numbers-mod-2n/>
struct ProbeSeq {
bucket_mask: usize,
pos: usize,
Expand Down Expand Up @@ -298,7 +298,7 @@ impl<T> Bucket<T> {
} else {
self.ptr.add(offset)
};
Bucket { ptr }
Self { ptr }
}
#[inline]
pub unsafe fn drop(&self) {
Expand All @@ -321,7 +321,7 @@ impl<T> Bucket<T> {
&mut *self.as_ptr()
}
#[inline]
pub unsafe fn copy_from_nonoverlapping(&self, other: Bucket<T>) {
pub unsafe fn copy_from_nonoverlapping(&self, other: &Self) {
self.as_ptr().copy_from_nonoverlapping(other.as_ptr(), 1);
}
}
Expand Down Expand Up @@ -745,7 +745,7 @@ impl<T> RawTable<T> {
// element into the new slot and clear the old control
// byte.
guard.set_ctrl(i, EMPTY);
guard.bucket(new_i).copy_from_nonoverlapping(item);
guard.bucket(new_i).copy_from_nonoverlapping(&item);
continue 'outer;
} else {
// If the target slot is occupied, swap the two elements
Expand Down Expand Up @@ -802,7 +802,7 @@ impl<T> RawTable<T> {
// - all elements are unique.
let index = new_table.find_insert_slot(hash);
new_table.set_ctrl(index, h2(hash));
new_table.bucket(index).copy_from_nonoverlapping(item);
new_table.bucket(index).copy_from_nonoverlapping(&item);
}

// We successfully copied all elements without panicking. Now replace
Expand Down Expand Up @@ -935,12 +935,12 @@ impl<T> RawTable<T> {
/// should be dropped using a `RawIter` before freeing the allocation.
#[inline]
pub fn into_alloc(self) -> Option<(NonNull<u8>, Layout)> {
let alloc = if !self.is_empty_singleton() {
let alloc = if self.is_empty_singleton() {
None
} else {
let (layout, _) = calculate_layout::<T>(self.buckets())
.unwrap_or_else(|| unsafe { hint::unreachable_unchecked() });
Some((self.ctrl.cast(), layout))
} else {
None
};
mem::forget(self);
alloc
Expand Down
6 changes: 5 additions & 1 deletion src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,11 @@ where
/// assert_eq!(set.is_subset(&sup), false);
/// ```
pub fn is_subset(&self, other: &Self) -> bool {
self.iter().all(|v| other.contains(v))
if self.len() <= other.len() {
self.iter().all(|v| other.contains(v))
} else {
false
}
}

/// Returns `true` if the set is a superset of another,
Expand Down

0 comments on commit ab3e4ea

Please sign in to comment.