Skip to content

Improve worst-case performance of HashSet.is_subset #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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