Allow for more efficient sorting when exporting Unord collections.

This commit is contained in:
Michael Woerister 2023-01-18 10:47:31 +01:00
parent c3d2573120
commit 72ee14ce39
7 changed files with 99 additions and 34 deletions

View File

@ -177,7 +177,7 @@ fn exported_symbols_provider_local(
// Can we skip the later sorting?
let mut symbols: Vec<_> = tcx.with_stable_hashing_context(|hcx| {
tcx.reachable_non_generics(LOCAL_CRATE)
.to_sorted(&hcx)
.to_sorted(&hcx, true)
.into_iter()
.map(|(&def_id, &info)| (ExportedSymbol::NonGeneric(def_id), info))
.collect()

View File

@ -14,7 +14,7 @@
use crate::{
fingerprint::Fingerprint,
stable_hasher::{HashStable, StableHasher, ToStableHashKey},
stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey},
};
/// `UnordItems` is the order-less version of `Iterator`. It only contains methods
@ -158,6 +158,7 @@ pub struct UnordSet<V: Eq + Hash> {
}
impl<V: Eq + Hash> Default for UnordSet<V> {
#[inline]
fn default() -> Self {
Self { inner: FxHashSet::default() }
}
@ -207,6 +208,46 @@ pub fn into_items(self) -> UnordItems<V, impl Iterator<Item = V>> {
UnordItems(self.inner.into_iter())
}
#[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
}
#[inline]
pub fn to_sorted_stable_ord(&self) -> Vec<V>
where
V: Ord + StableOrd + Copy,
{
let mut items: Vec<V> = self.inner.iter().copied().collect();
items.sort_unstable();
items
}
#[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
}
// We can safely extend this UnordSet from a set of unordered values because that
// won't expose the internal ordering anywhere.
#[inline]
@ -221,12 +262,14 @@ pub fn clear(&mut self) {
}
impl<V: Hash + Eq> Extend<V> for UnordSet<V> {
#[inline]
fn extend<T: IntoIterator<Item = V>>(&mut self, iter: T) {
self.inner.extend(iter)
}
}
impl<V: Hash + Eq> FromIterator<V> for UnordSet<V> {
#[inline]
fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self {
UnordSet { inner: FxHashSet::from_iter(iter) }
}
@ -254,24 +297,28 @@ pub struct UnordMap<K: Eq + Hash, V> {
}
impl<K: Eq + Hash, V> Default for UnordMap<K, V> {
#[inline]
fn default() -> Self {
Self { inner: FxHashMap::default() }
}
}
impl<K: Hash + Eq, V> Extend<(K, V)> for UnordMap<K, V> {
#[inline]
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
self.inner.extend(iter)
}
}
impl<K: Hash + Eq, V> FromIterator<(K, V)> for UnordMap<K, V> {
#[inline]
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
UnordMap { inner: FxHashMap::from_iter(iter) }
}
}
impl<K: Hash + Eq, V, I: Iterator<Item = (K, V)>> From<UnordItems<(K, V), I>> for UnordMap<K, V> {
#[inline]
fn from(items: UnordItems<(K, V), I>) -> Self {
UnordMap { inner: FxHashMap::from_iter(items.0) }
}
@ -351,30 +398,56 @@ pub fn extend<I: Iterator<Item = (K, V)>>(&mut self, items: UnordItems<(K, V), I
self.inner.extend(items.0)
}
pub fn to_sorted<HCX>(&self, hcx: &HCX) -> Vec<(&K, &V)>
#[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();
items.sort_by_cached_key(|(k, _)| k.to_stable_hash_key(hcx));
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
}
pub fn into_sorted<HCX>(self, hcx: &HCX) -> Vec<(K, V)>
#[inline]
pub fn to_sorted_stable_ord(&self) -> Vec<(K, &V)>
where
K: Ord + StableOrd + Copy,
{
let mut items: Vec<(K, &V)> = self.inner.iter().map(|(&k, v)| (k, v)).collect();
items.sort_unstable_by_key(|&(k, _)| k);
items
}
#[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();
items.sort_by_cached_key(|(k, _)| k.to_stable_hash_key(hcx));
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
}
pub fn values_sorted<HCX>(&self, hcx: &HCX) -> impl Iterator<Item = &V>
#[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();
items.sort_by_cached_key(|(k, _)| k.to_stable_hash_key(hcx));
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)
}
}

View File

@ -19,7 +19,6 @@
use rustc_middle::ty::{self, ClosureSizeProfileData, Ty, TyCtxt};
use rustc_span::symbol::sym;
use rustc_span::Span;
use smallvec::SmallVec;
use std::mem;
use std::ops::ControlFlow;
@ -450,9 +449,9 @@ fn visit_closures(&mut self) {
let common_hir_owner = fcx_typeck_results.hir_owner;
let fcx_closure_kind_origins =
fcx_typeck_results.closure_kind_origins().items_in_stable_order(self.tcx());
fcx_typeck_results.closure_kind_origins().items_in_stable_order();
for (&local_id, origin) in fcx_closure_kind_origins {
for (local_id, origin) in fcx_closure_kind_origins {
let hir_id = hir::HirId { owner: common_hir_owner, local_id };
let place_span = origin.0;
let place = self.resolve(origin.1.clone(), &place_span);
@ -465,14 +464,10 @@ fn visit_coercion_casts(&mut self) {
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
self.tcx().with_stable_hashing_context(|hcx| {
let fcx_coercion_casts: SmallVec<[_; 32]> =
fcx_typeck_results.coercion_casts().items().cloned().into_sorted_small_vec(&hcx);
for local_id in fcx_coercion_casts {
self.typeck_results.set_coercion_cast(local_id);
}
});
let fcx_coercion_casts = fcx_typeck_results.coercion_casts().to_sorted_stable_ord();
for local_id in fcx_coercion_casts {
self.typeck_results.set_coercion_cast(local_id);
}
}
fn visit_user_provided_tys(&mut self) {
@ -482,10 +477,10 @@ fn visit_user_provided_tys(&mut self) {
if self.rustc_dump_user_substs {
let sorted_user_provided_types =
fcx_typeck_results.user_provided_types().items_in_stable_order(self.tcx());
fcx_typeck_results.user_provided_types().items_in_stable_order();
let mut errors_buffer = Vec::new();
for (&local_id, c_ty) in sorted_user_provided_types {
for (local_id, c_ty) in sorted_user_provided_types {
let hir_id = hir::HirId { owner: common_hir_owner, local_id };
if let ty::UserType::TypeOf(_, user_substs) = c_ty.value {
@ -661,10 +656,9 @@ fn visit_liberated_fn_sigs(&mut self) {
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
let common_hir_owner = fcx_typeck_results.hir_owner;
let fcx_liberated_fn_sigs =
fcx_typeck_results.liberated_fn_sigs().items_in_stable_order(self.tcx());
let fcx_liberated_fn_sigs = fcx_typeck_results.liberated_fn_sigs().items_in_stable_order();
for (&local_id, &fn_sig) in fcx_liberated_fn_sigs {
for (local_id, &fn_sig) in fcx_liberated_fn_sigs {
let hir_id = hir::HirId { owner: common_hir_owner, local_id };
let fn_sig = self.resolve(fn_sig, &hir_id);
self.typeck_results.liberated_fn_sigs_mut().insert(hir_id, fn_sig);
@ -676,10 +670,9 @@ fn visit_fru_field_types(&mut self) {
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
let common_hir_owner = fcx_typeck_results.hir_owner;
let fcx_fru_field_types =
fcx_typeck_results.fru_field_types().items_in_stable_order(self.tcx());
let fcx_fru_field_types = fcx_typeck_results.fru_field_types().items_in_stable_order();
for (&local_id, ftys) in fcx_fru_field_types {
for (local_id, ftys) in fcx_fru_field_types {
let hir_id = hir::HirId { owner: common_hir_owner, local_id };
let ftys = self.resolve(ftys.clone(), &hir_id);
self.typeck_results.fru_field_types_mut().insert(hir_id, ftys);

View File

@ -1188,7 +1188,7 @@ fn encode_def_ids(&mut self) {
}
}
let inherent_impls = tcx.with_stable_hashing_context(|hcx| {
tcx.crate_inherent_impls(()).inherent_impls.to_sorted(&hcx)
tcx.crate_inherent_impls(()).inherent_impls.to_sorted(&hcx, true)
});
for (def_id, implementations) in inherent_impls {

View File

@ -27,7 +27,7 @@
use rustc_span::Span;
use std::{collections::hash_map::Entry, hash::Hash, iter};
use super::{RvalueScopes, TyCtxt};
use super::RvalueScopes;
#[derive(TyEncodable, TyDecodable, Debug, HashStable)]
pub struct TypeckResults<'tcx> {
@ -575,9 +575,8 @@ pub fn items(
self.data.items().map(|(id, value)| (*id, value))
}
#[allow(rustc::pass_by_value)]
pub fn items_in_stable_order(&self, tcx: TyCtxt<'_>) -> Vec<(&'a ItemLocalId, &'a V)> {
tcx.with_stable_hashing_context(|hcx| self.data.to_sorted(&hcx))
pub fn items_in_stable_order(&self) -> Vec<(ItemLocalId, &'a V)> {
self.data.to_sorted_stable_ord()
}
}

View File

@ -54,7 +54,7 @@ fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
let inherent_impls = cx
.tcx
.with_stable_hashing_context(|hcx| cx.tcx.crate_inherent_impls(()).inherent_impls.to_sorted(&hcx));
.with_stable_hashing_context(|hcx| cx.tcx.crate_inherent_impls(()).inherent_impls.to_sorted(&hcx, true));
for (_, impl_ids) in inherent_impls.into_iter().filter(|(&id, impls)| {
impls.len() > 1

View File

@ -81,7 +81,7 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
}
cx.tcx.with_stable_hashing_context(|hcx| {
for assoc in provided.values_sorted(&hcx) {
for assoc in provided.values_sorted(&hcx, true) {
let source_map = cx.tcx.sess.source_map();
let definition_span = source_map.guess_head_span(cx.tcx.def_span(assoc.def_id));