Skip to content

Commit

Permalink
Rollup merge of rust-lang#66280 - stepancheg:union, r=alexcrichton
Browse files Browse the repository at this point in the history
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 `>`.
  • Loading branch information
JohnTitor authored Nov 12, 2019
2 parents 8e0265c + 04a237b commit 6bdd1be
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/libstd/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ impl<T, S> HashSet<T, S>
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn union<'a>(&'a self, other: &'a HashSet<T, S>) -> Union<'a, T, S> {
if self.len() <= other.len() {
if self.len() >= other.len() {
Union {
iter: self.iter().chain(other.difference(self)),
}
Expand Down

0 comments on commit 6bdd1be

Please sign in to comment.