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 <[email protected]>
  • Loading branch information
bors[bot] and Amanieu committed Apr 13, 2019
2 parents 9068eb7 + b18db6c commit 8bb1591
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 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
8 changes: 4 additions & 4 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 @@ -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 8bb1591

Please sign in to comment.