Skip to content

Commit 8823bf0

Browse files
committed
Fix poor worst case performance of is_disjoint
1 parent ccba43d commit 8823bf0

File tree

1 file changed

+5
-2
lines changed
  • src/libstd/collections/hash

1 file changed

+5
-2
lines changed

src/libstd/collections/hash/set.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,11 @@ impl<T, S> HashSet<T, S>
599599
/// ```
600600
#[stable(feature = "rust1", since = "1.0.0")]
601601
pub fn is_disjoint(&self, other: &HashSet<T, S>) -> bool {
602-
self.iter().all(|v| !other.contains(v))
602+
if self.len() <= other.len() {
603+
self.iter().all(|v| !other.contains(v))
604+
} else {
605+
other.iter().all(|v| !self.contains(v))
606+
}
603607
}
604608

605609
/// Returns `true` if the set is a subset of another,
@@ -1510,7 +1514,6 @@ mod test_set {
15101514
let mut a = HashSet::new();
15111515
let mut b = HashSet::new();
15121516
assert!(a.intersection(&b).next().is_none());
1513-
assert!(b.intersection(&a).next().is_none());
15141517

15151518
assert!(a.insert(11));
15161519
assert!(a.insert(1));

0 commit comments

Comments
 (0)