Rollup merge of #66280 - stepancheg:union, r=alexcrichton

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 `>`.
This commit is contained in:
Yuki Okushi 2019-11-12 16:36:15 +09:00 committed by GitHub
commit 6bdd1beca6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)),
}