Skip to content

Commit

Permalink
Improve large set - small set performance
Browse files Browse the repository at this point in the history
Also remove S: Default bound on Assign ops
  • Loading branch information
ToMe25 committed Jun 2, 2024
1 parent 8898a5a commit c35af22
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ where
impl<T, S> BitOrAssign<&HashSet<T, S>> for HashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
S: BuildHasher,
{
/// Modifies this set to contain the union of `self` and `rhs`.
///
Expand Down Expand Up @@ -1605,7 +1605,7 @@ where
impl<T, S> BitAndAssign<&HashSet<T, S>> for HashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
S: BuildHasher,
{
/// Modifies this set to contain the intersection of `self` and `rhs`.
///
Expand Down Expand Up @@ -1635,7 +1635,7 @@ where
impl<T, S> BitXorAssign<&HashSet<T, S>> for HashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
S: BuildHasher,
{
/// Modifies this set to contain the symmetric difference of `self` and `rhs`.
///
Expand Down Expand Up @@ -1671,7 +1671,7 @@ where
impl<T, S> AddAssign<&HashSet<T, S>> for HashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
S: BuildHasher,
{
/// Modifies this set to contain the union of `self` and `rhs`.
///
Expand Down Expand Up @@ -1705,7 +1705,7 @@ where
impl<T, S> SubAssign<&HashSet<T, S>> for HashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
S: BuildHasher,
{
/// Modifies this set to contain the difference of `self` and `rhs`.
///
Expand All @@ -1728,7 +1728,13 @@ where
/// assert_eq!(i, expected.len());
/// ```
fn sub_assign(&mut self, rhs: &HashSet<T, S>) {
self.retain(|item| !rhs.contains(item));
if rhs.len() < self.len() {
for item in rhs {
self.remove(item);
}
} else {
self.retain(|item| !rhs.contains(item));
}
}
}

Expand Down

0 comments on commit c35af22

Please sign in to comment.