Skip to content

Commit

Permalink
Rollup merge of rust-lang#59665 - ssomers:hashset_revisited, r=KodrAus
Browse files Browse the repository at this point in the history
improve worst-case performance of HashSet.is_subset

One more simple optimization opportunity for HashSet that was applied in BTreeSet in rust-lang#59186 (and wasn't in rust-lang#57043). Already covered by the existing unit test.

r? @KodrAus
  • Loading branch information
Centril committed Apr 5, 2019
2 parents a11083e + 5b8bfe0 commit a3122e1
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/libstd/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,11 @@ impl<T, S> HashSet<T, S>
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_subset(&self, other: &HashSet<T, S>) -> bool {
self.iter().all(|v| other.contains(v))
if self.len() <= other.len() {
self.iter().all(|v| other.contains(v))
} else {
false
}
}

/// Returns `true` if the set is a superset of another,
Expand Down

0 comments on commit a3122e1

Please sign in to comment.