]> git.lizzy.rs Git - rust.git/commitdiff
Clean up and document unord collections a bit.
authorMichael Woerister <michaelwoerister@posteo>
Thu, 19 Jan 2023 16:37:59 +0000 (17:37 +0100)
committerMichael Woerister <michaelwoerister@posteo>
Thu, 19 Jan 2023 16:37:59 +0000 (17:37 +0100)
compiler/rustc_data_structures/src/unord.rs

index d29b49872741cffcd44de89840f9dc8a47c6eb6f..f35f18e51cb4e5339dfd8a4b88b9c291ce773256 100644 (file)
@@ -208,21 +208,24 @@ pub fn into_items(self) -> UnordItems<V, impl Iterator<Item = V>> {
         UnordItems(self.inner.into_iter())
     }
 
+    /// Returns the items of this set in stable sort order (as defined by `ToStableHashKey`).
+    ///
+    /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
+    /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
+    /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
+    /// for `V` is expensive (e.g. a `DefId -> DefPathHash` lookup).
     #[inline]
     pub fn to_sorted<HCX>(&self, hcx: &HCX, cache_sort_key: bool) -> Vec<&V>
     where
         V: ToStableHashKey<HCX>,
     {
-        let mut items: Vec<&V> = self.inner.iter().collect();
-        if cache_sort_key {
-            items.sort_by_cached_key(|k| k.to_stable_hash_key(hcx));
-        } else {
-            items.sort_unstable_by_key(|k| k.to_stable_hash_key(hcx));
-        }
-
-        items
+        to_sorted_vec(hcx, self.inner.iter(), cache_sort_key, |&x| x)
     }
 
+    /// Returns the items of this set in stable sort order (as defined by
+    /// `StableOrd`). This method is much more efficient than
+    /// `into_sorted` because it does not need to transform keys to their
+    /// `ToStableHashKey` equivalent.
     #[inline]
     pub fn to_sorted_stable_ord(&self) -> Vec<V>
     where
@@ -233,19 +236,18 @@ pub fn to_sorted_stable_ord(&self) -> Vec<V>
         items
     }
 
+    /// Returns the items of this set in stable sort order (as defined by `ToStableHashKey`).
+    ///
+    /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
+    /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
+    /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
+    /// for `V` is expensive (e.g. a `DefId -> DefPathHash` lookup).
     #[inline]
     pub fn into_sorted<HCX>(self, hcx: &HCX, cache_sort_key: bool) -> Vec<V>
     where
         V: ToStableHashKey<HCX>,
     {
-        let mut items: Vec<V> = self.inner.into_iter().collect();
-        if cache_sort_key {
-            items.sort_by_cached_key(|k| k.to_stable_hash_key(hcx));
-        } else {
-            items.sort_unstable_by_key(|k| k.to_stable_hash_key(hcx));
-        }
-
-        items
+        to_sorted_vec(hcx, self.inner.into_iter(), cache_sort_key, |x| x)
     }
 
     // We can safely extend this UnordSet from a set of unordered values because that
@@ -398,21 +400,23 @@ pub fn extend<I: Iterator<Item = (K, V)>>(&mut self, items: UnordItems<(K, V), I
         self.inner.extend(items.0)
     }
 
+    /// Returns the entries of this map in stable sort order (as defined by `ToStableHashKey`).
+    ///
+    /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
+    /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
+    /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
+    /// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
     #[inline]
     pub fn to_sorted<HCX>(&self, hcx: &HCX, cache_sort_key: bool) -> Vec<(&K, &V)>
     where
         K: ToStableHashKey<HCX>,
     {
-        let mut items: Vec<(&K, &V)> = self.inner.iter().collect();
-        if cache_sort_key {
-            items.sort_by_cached_key(|(k, _)| k.to_stable_hash_key(hcx));
-        } else {
-            items.sort_unstable_by_key(|(k, _)| k.to_stable_hash_key(hcx));
-        }
-
-        items
+        to_sorted_vec(hcx, self.inner.iter(), cache_sort_key, |&(k, _)| k)
     }
 
+    /// Returns the entries of this map in stable sort order (as defined by `StableOrd`).
+    /// This method can be much more efficient than `into_sorted` because it does not need
+    /// to transform keys to their `ToStableHashKey` equivalent.
     #[inline]
     pub fn to_sorted_stable_ord(&self) -> Vec<(K, &V)>
     where
@@ -423,32 +427,35 @@ pub fn to_sorted_stable_ord(&self) -> Vec<(K, &V)>
         items
     }
 
+    /// Returns the entries of this map in stable sort order (as defined by `ToStableHashKey`).
+    ///
+    /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
+    /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
+    /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
+    /// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
     #[inline]
     pub fn into_sorted<HCX>(self, hcx: &HCX, cache_sort_key: bool) -> Vec<(K, V)>
     where
         K: ToStableHashKey<HCX>,
     {
-        let mut items: Vec<(K, V)> = self.inner.into_iter().collect();
-        if cache_sort_key {
-            items.sort_by_cached_key(|(k, _)| k.to_stable_hash_key(hcx));
-        } else {
-            items.sort_unstable_by_key(|(k, _)| k.to_stable_hash_key(hcx));
-        }
-        items
+        to_sorted_vec(hcx, self.inner.into_iter(), cache_sort_key, |(k, _)| k)
     }
 
+    /// Returns the values of this map in stable sort order (as defined by K's
+    /// `ToStableHashKey` implementation).
+    ///
+    /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
+    /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
+    /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
+    /// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
     #[inline]
     pub fn values_sorted<HCX>(&self, hcx: &HCX, cache_sort_key: bool) -> impl Iterator<Item = &V>
     where
         K: ToStableHashKey<HCX>,
     {
-        let mut items: Vec<(&K, &V)> = self.inner.iter().collect();
-        if cache_sort_key {
-            items.sort_by_cached_key(|(k, _)| k.to_stable_hash_key(hcx));
-        } else {
-            items.sort_unstable_by_key(|(k, _)| k.to_stable_hash_key(hcx));
-        }
-        items.into_iter().map(|(_, v)| v)
+        to_sorted_vec(hcx, self.inner.iter(), cache_sort_key, |&(k, _)| k)
+            .into_iter()
+            .map(|(_, v)| v)
     }
 }
 
@@ -540,6 +547,27 @@ fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
     }
 }
 
+#[inline]
+fn to_sorted_vec<HCX, T, K, I>(
+    hcx: &HCX,
+    iter: I,
+    cache_sort_key: bool,
+    extract_key: fn(&T) -> &K,
+) -> Vec<T>
+where
+    I: Iterator<Item = T>,
+    K: ToStableHashKey<HCX>,
+{
+    let mut items: Vec<T> = iter.collect();
+    if cache_sort_key {
+        items.sort_by_cached_key(|x| extract_key(x).to_stable_hash_key(hcx));
+    } else {
+        items.sort_unstable_by_key(|x| extract_key(x).to_stable_hash_key(hcx));
+    }
+
+    items
+}
+
 fn hash_iter_order_independent<
     HCX,
     T: HashStable<HCX>,