about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-04-05 12:46:48 +0200
committerGitHub <noreply@github.com>2019-04-05 12:46:48 +0200
commita3122e12d8bcc2701c26cc3d38b35a818c0d87a6 (patch)
treeab833c135f321e6bb2c8524da818b9b61248f05f
parenta11083e3c6a8eb0610a752dcf8d1af1ce5ca6dee (diff)
parent5b8bfe047123bad63ead0370c165cd9307a07caa (diff)
downloadrust-a3122e12d8bcc2701c26cc3d38b35a818c0d87a6.tar.gz
rust-a3122e12d8bcc2701c26cc3d38b35a818c0d87a6.zip
Rollup merge of #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 #59186 (and wasn't in #57043). Already covered by the existing unit test.

r? @KodrAus
-rw-r--r--src/libstd/collections/hash/set.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs
index 89d5b2ff30f..b9fcc2365fa 100644
--- a/src/libstd/collections/hash/set.rs
+++ b/src/libstd/collections/hash/set.rs
@@ -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,