Skip to content

Commit def8aba

Browse files
committed
Auto merge of #228 - cuviper:union-length, r=Amanieu
Fix the union length comparision, and implement it for par_union The `(smaller, larger)` comparison in `union` was backwards. It flipped in commit 80ce422 syncing with `std`, but the comparison is used differently there. While we're at it, `par_union` can use the same trick.
2 parents 496b1bc + af990b6 commit def8aba

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/external_trait_impls/rayon/set.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,16 @@ where
198198
where
199199
C: UnindexedConsumer<Self::Item>,
200200
{
201-
self.a
201+
// We'll iterate one set in full, and only the remaining difference from the other.
202+
// Use the smaller set for the difference in order to reduce hash lookups.
203+
let (smaller, larger) = if self.a.len() <= self.b.len() {
204+
(self.a, self.b)
205+
} else {
206+
(self.b, self.a)
207+
};
208+
larger
202209
.into_par_iter()
203-
.chain(self.b.par_difference(self.a))
210+
.chain(smaller.par_difference(larger))
204211
.drive_unindexed(consumer)
205212
}
206213
}

src/set.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,9 @@ where
736736
/// ```
737737
#[cfg_attr(feature = "inline-more", inline)]
738738
pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, T, S, A> {
739-
let (smaller, larger) = if self.len() >= other.len() {
739+
// We'll iterate one set in full, and only the remaining difference from the other.
740+
// Use the smaller set for the difference in order to reduce hash lookups.
741+
let (smaller, larger) = if self.len() <= other.len() {
740742
(self, other)
741743
} else {
742744
(other, self)

0 commit comments

Comments
 (0)