Skip to content

Commit 9f558f5

Browse files
committed
Improve worst-case performance of HashSet.is_subset
Ported from rust-lang/rust#59665
1 parent 9068eb7 commit 9f558f5

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/external_trait_impls/rayon/set.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,11 @@ where
256256
///
257257
/// This method runs in a potentially parallel fashion.
258258
pub fn par_is_subset(&self, other: &Self) -> bool {
259-
self.into_par_iter().all(|x| other.contains(x))
259+
if self.len() <= other.len() {
260+
self.into_par_iter().all(|x| other.contains(x))
261+
} else {
262+
false
263+
}
260264
}
261265

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

src/set.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,11 @@ where
640640
/// assert_eq!(set.is_subset(&sup), false);
641641
/// ```
642642
pub fn is_subset(&self, other: &Self) -> bool {
643-
self.iter().all(|v| other.contains(v))
643+
if self.len() <= other.len() {
644+
self.iter().all(|v| other.contains(v))
645+
} else {
646+
false
647+
}
644648
}
645649

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

0 commit comments

Comments
 (0)