Skip to content

Commit 28982b9

Browse files
committed
Improve large set - small set performance
Also remove S: Default bound on Assign ops
1 parent 6e0aec2 commit 28982b9

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/set.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ where
15711571
impl<T, S> BitOrAssign<&HashSet<T, S>> for HashSet<T, S>
15721572
where
15731573
T: Eq + Hash + Clone,
1574-
S: BuildHasher + Default,
1574+
S: BuildHasher,
15751575
{
15761576
/// Modifies this set to contain the union of `self` and `rhs`.
15771577
///
@@ -1605,7 +1605,7 @@ where
16051605
impl<T, S> BitAndAssign<&HashSet<T, S>> for HashSet<T, S>
16061606
where
16071607
T: Eq + Hash + Clone,
1608-
S: BuildHasher + Default,
1608+
S: BuildHasher,
16091609
{
16101610
/// Modifies this set to contain the intersection of `self` and `rhs`.
16111611
///
@@ -1635,7 +1635,7 @@ where
16351635
impl<T, S> BitXorAssign<&HashSet<T, S>> for HashSet<T, S>
16361636
where
16371637
T: Eq + Hash + Clone,
1638-
S: BuildHasher + Default,
1638+
S: BuildHasher,
16391639
{
16401640
/// Modifies this set to contain the symmetric difference of `self` and `rhs`.
16411641
///
@@ -1671,7 +1671,7 @@ where
16711671
impl<T, S> AddAssign<&HashSet<T, S>> for HashSet<T, S>
16721672
where
16731673
T: Eq + Hash + Clone,
1674-
S: BuildHasher + Default,
1674+
S: BuildHasher,
16751675
{
16761676
/// Modifies this set to contain the union of `self` and `rhs`.
16771677
///
@@ -1705,7 +1705,7 @@ where
17051705
impl<T, S> SubAssign<&HashSet<T, S>> for HashSet<T, S>
17061706
where
17071707
T: Eq + Hash + Clone,
1708-
S: BuildHasher + Default,
1708+
S: BuildHasher,
17091709
{
17101710
/// Modifies this set to contain the difference of `self` and `rhs`.
17111711
///
@@ -1728,7 +1728,13 @@ where
17281728
/// assert_eq!(i, expected.len());
17291729
/// ```
17301730
fn sub_assign(&mut self, rhs: &HashSet<T, S>) {
1731-
self.retain(|item| !rhs.contains(item));
1731+
if rhs.len() < self.len() {
1732+
for item in rhs {
1733+
self.remove(item);
1734+
}
1735+
} else {
1736+
self.retain(|item| !rhs.contains(item));
1737+
}
17321738
}
17331739
}
17341740

0 commit comments

Comments
 (0)