From 2c66e2891d954fcc5ebf719c8b54f5ad6a586153 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 18 Jan 2021 13:41:14 -0800 Subject: [PATCH 1/2] Fix the comparison of union set sizes --- src/set.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/set.rs b/src/set.rs index c09876ee9e..a451bcc771 100644 --- a/src/set.rs +++ b/src/set.rs @@ -736,7 +736,9 @@ where /// ``` #[cfg_attr(feature = "inline-more", inline)] pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, T, S, A> { - let (smaller, larger) = if self.len() >= other.len() { + // We'll iterate one set in full, and only the remaining difference from the other. + // Use the smaller set for the difference in order to reduce hash lookups. + let (smaller, larger) = if self.len() <= other.len() { (self, other) } else { (other, self) From af990b6e87b773bbf1a97fe8a7ceb8d9d453043a Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 18 Jan 2021 15:22:34 -0800 Subject: [PATCH 2/2] Implement the length optimization for par_union --- src/external_trait_impls/rayon/set.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/external_trait_impls/rayon/set.rs b/src/external_trait_impls/rayon/set.rs index bbdfa7799d..ee4f6e6693 100644 --- a/src/external_trait_impls/rayon/set.rs +++ b/src/external_trait_impls/rayon/set.rs @@ -198,9 +198,16 @@ where where C: UnindexedConsumer, { - self.a + // We'll iterate one set in full, and only the remaining difference from the other. + // Use the smaller set for the difference in order to reduce hash lookups. + let (smaller, larger) = if self.a.len() <= self.b.len() { + (self.a, self.b) + } else { + (self.b, self.a) + }; + larger .into_par_iter() - .chain(self.b.par_difference(self.a)) + .chain(smaller.par_difference(larger)) .drive_unindexed(consumer) } }