Skip to content

Commit

Permalink
Merge #61 #62
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

62: Remove incorrect debug_assert r=Amanieu a=Amanieu

Fixes #60 

Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
  • Loading branch information
bors[bot] and Amanieu committed Apr 12, 2019
3 parents 9068eb7 + 9f558f5 + 7225250 commit fcbe59d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 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
7 changes: 2 additions & 5 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ fn special_is_empty(ctrl: u8) -> bool {
#[inline]
#[allow(clippy::cast_possible_truncation)]
fn h1(hash: u64) -> usize {
#[cfg(target_pointer_width = "32")]
{
debug_assert!(hash <= u64::from(u32::max_value()));
}
hash as usize // truncation
// On 32-bit platforms we simply ignore the higher hash bits.
hash as usize
}

/// Secondary hash function, saved in the low 7 bits of the control byte.
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 fcbe59d

Please sign in to comment.