Skip to content

Commit a3122e1

Browse files
authored
Rollup merge of rust-lang#59665 - ssomers:hashset_revisited, r=KodrAus
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
2 parents a11083e + 5b8bfe0 commit a3122e1

File tree

1 file changed

+5
-1
lines changed
  • src/libstd/collections/hash

1 file changed

+5
-1
lines changed

src/libstd/collections/hash/set.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,11 @@ impl<T, S> HashSet<T, S>
627627
/// ```
628628
#[stable(feature = "rust1", since = "1.0.0")]
629629
pub fn is_subset(&self, other: &HashSet<T, S>) -> bool {
630-
self.iter().all(|v| other.contains(v))
630+
if self.len() <= other.len() {
631+
self.iter().all(|v| other.contains(v))
632+
} else {
633+
false
634+
}
631635
}
632636

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

0 commit comments

Comments
 (0)