Skip to content

Commit a6bd9fb

Browse files
Avoid unnecessary sorting of traits
1 parent 7606c13 commit a6bd9fb

File tree

3 files changed

+25
-47
lines changed

3 files changed

+25
-47
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::rmeta::*;
77
use rustc_ast as ast;
88
use rustc_data_structures::captures::Captures;
99
use rustc_data_structures::fingerprint::Fingerprint;
10+
use rustc_data_structures::fx::FxIndexMap;
1011
use rustc_data_structures::owned_slice::OwnedSlice;
1112
use rustc_data_structures::sync::{Lock, Lrc, OnceLock};
1213
use rustc_data_structures::unhash::UnhashMap;
@@ -69,12 +70,12 @@ pub(crate) struct CrateMetadata {
6970
/// Trait impl data.
7071
/// FIXME: Used only from queries and can use query cache,
7172
/// so pre-decoding can probably be avoided.
72-
trait_impls: FxHashMap<(u32, DefIndex), LazyArray<(DefIndex, Option<SimplifiedType>)>>,
73+
trait_impls: FxIndexMap<(u32, DefIndex), LazyArray<(DefIndex, Option<SimplifiedType>)>>,
7374
/// Inherent impls which do not follow the normal coherence rules.
7475
///
7576
/// These can be introduced using either `#![rustc_coherence_is_core]`
7677
/// or `#[rustc_allow_incoherent_impl]`.
77-
incoherent_impls: FxHashMap<SimplifiedType, LazyArray<DefIndex>>,
78+
incoherent_impls: FxIndexMap<SimplifiedType, LazyArray<DefIndex>>,
7879
/// Proc macro descriptions for this crate, if it's a proc macro crate.
7980
raw_proc_macros: Option<&'static [ProcMacro]>,
8081
/// Source maps for code from the crate.

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::errors::{FailCreateFileEncoder, FailWriteFile};
22
use crate::rmeta::*;
33

44
use rustc_ast::Attribute;
5-
use rustc_data_structures::fx::FxIndexSet;
5+
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
66
use rustc_data_structures::memmap::{Mmap, MmapMut};
77
use rustc_data_structures::sync::{join, par_for_each_in, Lrc};
88
use rustc_data_structures::temp_dir::MaybeTempDir;
@@ -1491,10 +1491,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14911491
}
14921492
}
14931493

1494-
let inherent_impls = tcx.with_stable_hashing_context(|hcx| {
1495-
tcx.crate_inherent_impls(()).unwrap().inherent_impls.to_sorted(&hcx, true)
1496-
});
1497-
for (def_id, impls) in inherent_impls {
1494+
for (def_id, impls) in &tcx.crate_inherent_impls(()).unwrap().inherent_impls {
14981495
record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| {
14991496
assert!(def_id.is_local());
15001497
def_id.index
@@ -1965,8 +1962,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19651962
fn encode_impls(&mut self) -> LazyArray<TraitImpls> {
19661963
empty_proc_macro!(self);
19671964
let tcx = self.tcx;
1968-
let mut fx_hash_map: FxHashMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
1969-
FxHashMap::default();
1965+
let mut trait_impls: FxIndexMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
1966+
FxIndexMap::default();
19701967

19711968
for id in tcx.hir().items() {
19721969
let DefKind::Impl { of_trait } = tcx.def_kind(id.owner_id) else {
@@ -1986,7 +1983,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19861983
trait_ref.self_ty(),
19871984
TreatParams::AsCandidateKey,
19881985
);
1989-
fx_hash_map
1986+
trait_impls
19901987
.entry(trait_ref.def_id)
19911988
.or_default()
19921989
.push((id.owner_id.def_id.local_def_index, simplified_self_ty));
@@ -2007,47 +2004,30 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20072004
}
20082005
}
20092006

2010-
let mut all_impls: Vec<_> = fx_hash_map.into_iter().collect();
2011-
2012-
// Bring everything into deterministic order for hashing
2013-
all_impls.sort_by_cached_key(|&(trait_def_id, _)| tcx.def_path_hash(trait_def_id));
2014-
2015-
let all_impls: Vec<_> = all_impls
2007+
let trait_impls: Vec<_> = trait_impls
20162008
.into_iter()
2017-
.map(|(trait_def_id, mut impls)| {
2018-
// Bring everything into deterministic order for hashing
2019-
impls.sort_by_cached_key(|&(index, _)| {
2020-
tcx.hir().def_path_hash(LocalDefId { local_def_index: index })
2021-
});
2022-
2023-
TraitImpls {
2024-
trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
2025-
impls: self.lazy_array(&impls),
2026-
}
2009+
.map(|(trait_def_id, impls)| TraitImpls {
2010+
trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
2011+
impls: self.lazy_array(&impls),
20272012
})
20282013
.collect();
20292014

2030-
self.lazy_array(&all_impls)
2015+
self.lazy_array(&trait_impls)
20312016
}
20322017

20332018
#[instrument(level = "debug", skip(self))]
20342019
fn encode_incoherent_impls(&mut self) -> LazyArray<IncoherentImpls> {
20352020
empty_proc_macro!(self);
20362021
let tcx = self.tcx;
2037-
let all_impls = tcx.with_stable_hashing_context(|hcx| {
2038-
tcx.crate_inherent_impls(()).unwrap().incoherent_impls.to_sorted(&hcx, true)
2039-
});
20402022

2041-
let all_impls: Vec<_> = all_impls
2042-
.into_iter()
2043-
.map(|(&simp, impls)| {
2044-
let mut impls: Vec<_> =
2045-
impls.into_iter().map(|def_id| def_id.local_def_index).collect();
2046-
impls.sort_by_cached_key(|&local_def_index| {
2047-
tcx.hir().def_path_hash(LocalDefId { local_def_index })
2048-
});
2049-
2050-
IncoherentImpls { self_ty: simp, impls: self.lazy_array(impls) }
2023+
let all_impls: Vec<_> = tcx
2024+
.crate_inherent_impls(())
2025+
.unwrap()
2026+
.incoherent_impls
2027+
.iter()
2028+
.map(|(&simp, impls)| IncoherentImpls {
2029+
self_ty: simp,
2030+
impls: self.lazy_array(impls.iter().map(|def_id| def_id.local_def_index)),
20512031
})
20522032
.collect();
20532033

@@ -2291,6 +2271,8 @@ pub fn provide(providers: &mut Providers) {
22912271
span_bug!(tcx.def_span(def_id), "no traits in scope for a doc link")
22922272
})
22932273
},
2274+
2275+
// TODO: Uplift these into
22942276
traits: |tcx, LocalCrate| {
22952277
let mut traits = Vec::new();
22962278
for id in tcx.hir().items() {
@@ -2299,8 +2281,6 @@ pub fn provide(providers: &mut Providers) {
22992281
}
23002282
}
23012283

2302-
// Bring everything into deterministic order.
2303-
traits.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
23042284
tcx.arena.alloc_slice(&traits)
23052285
},
23062286
trait_impls_in_crate: |tcx, LocalCrate| {
@@ -2313,8 +2293,6 @@ pub fn provide(providers: &mut Providers) {
23132293
}
23142294
}
23152295

2316-
// Bring everything into deterministic order.
2317-
trait_impls.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
23182296
tcx.arena.alloc_slice(&trait_impls)
23192297
},
23202298

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use rustc_data_structures::intern::Interned;
3939
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4040
use rustc_data_structures::steal::Steal;
4141
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
42-
use rustc_data_structures::unord::UnordMap;
4342
use rustc_errors::{Diag, ErrorGuaranteed, StashKey};
4443
use rustc_hir as hir;
4544
use rustc_hir::def::{CtorKind, CtorOf, DefKind, DocLinkResMap, LifetimeRes, Res};
@@ -2045,8 +2044,8 @@ pub fn provide(providers: &mut Providers) {
20452044
/// (constructing this map requires touching the entire crate).
20462045
#[derive(Clone, Debug, Default, HashStable)]
20472046
pub struct CrateInherentImpls {
2048-
pub inherent_impls: LocalDefIdMap<Vec<DefId>>,
2049-
pub incoherent_impls: UnordMap<SimplifiedType, Vec<LocalDefId>>,
2047+
pub inherent_impls: FxIndexMap<LocalDefId, Vec<DefId>>,
2048+
pub incoherent_impls: FxIndexMap<SimplifiedType, Vec<LocalDefId>>,
20502049
}
20512050

20522051
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, HashStable)]

0 commit comments

Comments
 (0)