From 04a237b9e2c327e1ad6339afd2f967bfcad38483 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Sun, 10 Nov 2019 23:47:23 +0000 Subject: [PATCH] Fix HashSet::union performance Consider this example: small_set = 0..2, large_set = 0..1000. To efficiently compute the union of these sets, we should * take all elements of the larger set * for each element of the smaller set check it is not in the larger set This is exactly what this commit does. This particular optimization was implemented a year ago, but the author mistaken `<` and `>`. --- src/libstd/collections/hash/set.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 092fb44346848..a038ee8021057 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -551,7 +551,7 @@ impl HashSet #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn union<'a>(&'a self, other: &'a HashSet) -> Union<'a, T, S> { - if self.len() <= other.len() { + if self.len() >= other.len() { Union { iter: self.iter().chain(other.difference(self)), }