From 8823bf0b403b0ebf348e1f3fa59d74f2843f0c8b Mon Sep 17 00:00:00 2001 From: Stein Somers Date: Wed, 9 Jan 2019 22:19:54 +0100 Subject: Fix poor worst case performance of is_disjoint --- src/libstd/collections/hash/set.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 627846411bc..c55dd049ec6 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -599,7 +599,11 @@ impl HashSet /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn is_disjoint(&self, other: &HashSet) -> bool { - self.iter().all(|v| !other.contains(v)) + if self.len() <= other.len() { + self.iter().all(|v| !other.contains(v)) + } else { + other.iter().all(|v| !self.contains(v)) + } } /// Returns `true` if the set is a subset of another, @@ -1510,7 +1514,6 @@ mod test_set { let mut a = HashSet::new(); let mut b = HashSet::new(); assert!(a.intersection(&b).next().is_none()); - assert!(b.intersection(&a).next().is_none()); assert!(a.insert(11)); assert!(a.insert(1)); -- cgit 1.4.1-3-g733a5