Skip to content

Commit b5d0608

Browse files
Avoid unnecessary sorting of traits
1 parent 5affbb1 commit b5d0608

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;
@@ -83,12 +84,12 @@ pub(crate) struct CrateMetadata {
8384
/// Trait impl data.
8485
/// FIXME: Used only from queries and can use query cache,
8586
/// so pre-decoding can probably be avoided.
86-
trait_impls: FxHashMap<(u32, DefIndex), LazyArray<(DefIndex, Option<SimplifiedType>)>>,
87+
trait_impls: FxIndexMap<(u32, DefIndex), LazyArray<(DefIndex, Option<SimplifiedType>)>>,
8788
/// Inherent impls which do not follow the normal coherence rules.
8889
///
8990
/// These can be introduced using either `#![rustc_coherence_is_core]`
9091
/// or `#[rustc_allow_incoherent_impl]`.
91-
incoherent_impls: FxHashMap<SimplifiedType, LazyArray<DefIndex>>,
92+
incoherent_impls: FxIndexMap<SimplifiedType, LazyArray<DefIndex>>,
9293
/// Proc macro descriptions for this crate, if it's a proc macro crate.
9394
raw_proc_macros: Option<&'static [ProcMacro]>,
9495
/// 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;
@@ -1508,10 +1508,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15081508
}
15091509
}
15101510

1511-
let inherent_impls = tcx.with_stable_hashing_context(|hcx| {
1512-
tcx.crate_inherent_impls(()).unwrap().inherent_impls.to_sorted(&hcx, true)
1513-
});
1514-
for (def_id, impls) in inherent_impls {
1511+
for (def_id, impls) in &tcx.crate_inherent_impls(()).unwrap().inherent_impls {
15151512
record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| {
15161513
assert!(def_id.is_local());
15171514
def_id.index
@@ -1992,8 +1989,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19921989
fn encode_impls(&mut self) -> LazyArray<TraitImpls> {
19931990
empty_proc_macro!(self);
19941991
let tcx = self.tcx;
1995-
let mut fx_hash_map: FxHashMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
1996-
FxHashMap::default();
1992+
let mut trait_impls: FxIndexMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
1993+
FxIndexMap::default();
19971994

19981995
for id in tcx.hir().items() {
19991996
let DefKind::Impl { of_trait } = tcx.def_kind(id.owner_id) else {
@@ -2012,7 +2009,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20122009
trait_ref.self_ty(),
20132010
TreatParams::AsCandidateKey,
20142011
);
2015-
fx_hash_map
2012+
trait_impls
20162013
.entry(trait_ref.def_id)
20172014
.or_default()
20182015
.push((id.owner_id.def_id.local_def_index, simplified_self_ty));
@@ -2033,47 +2030,30 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20332030
}
20342031
}
20352032

2036-
let mut all_impls: Vec<_> = fx_hash_map.into_iter().collect();
2037-
2038-
// Bring everything into deterministic order for hashing
2039-
all_impls.sort_by_cached_key(|&(trait_def_id, _)| tcx.def_path_hash(trait_def_id));
2040-
2041-
let all_impls: Vec<_> = all_impls
2033+
let trait_impls: Vec<_> = trait_impls
20422034
.into_iter()
2043-
.map(|(trait_def_id, mut impls)| {
2044-
// Bring everything into deterministic order for hashing
2045-
impls.sort_by_cached_key(|&(index, _)| {
2046-
tcx.hir().def_path_hash(LocalDefId { local_def_index: index })
2047-
});
2048-
2049-
TraitImpls {
2050-
trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
2051-
impls: self.lazy_array(&impls),
2052-
}
2035+
.map(|(trait_def_id, impls)| TraitImpls {
2036+
trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
2037+
impls: self.lazy_array(&impls),
20532038
})
20542039
.collect();
20552040

2056-
self.lazy_array(&all_impls)
2041+
self.lazy_array(&trait_impls)
20572042
}
20582043

20592044
#[instrument(level = "debug", skip(self))]
20602045
fn encode_incoherent_impls(&mut self) -> LazyArray<IncoherentImpls> {
20612046
empty_proc_macro!(self);
20622047
let tcx = self.tcx;
2063-
let all_impls = tcx.with_stable_hashing_context(|hcx| {
2064-
tcx.crate_inherent_impls(()).unwrap().incoherent_impls.to_sorted(&hcx, true)
2065-
});
20662048

2067-
let all_impls: Vec<_> = all_impls
2068-
.into_iter()
2069-
.map(|(&simp, impls)| {
2070-
let mut impls: Vec<_> =
2071-
impls.into_iter().map(|def_id| def_id.local_def_index).collect();
2072-
impls.sort_by_cached_key(|&local_def_index| {
2073-
tcx.hir().def_path_hash(LocalDefId { local_def_index })
2074-
});
2075-
2076-
IncoherentImpls { self_ty: simp, impls: self.lazy_array(impls) }
2049+
let all_impls: Vec<_> = tcx
2050+
.crate_inherent_impls(())
2051+
.unwrap()
2052+
.incoherent_impls
2053+
.iter()
2054+
.map(|(&simp, impls)| IncoherentImpls {
2055+
self_ty: simp,
2056+
impls: self.lazy_array(impls.iter().map(|def_id| def_id.local_def_index)),
20772057
})
20782058
.collect();
20792059

@@ -2317,6 +2297,8 @@ pub fn provide(providers: &mut Providers) {
23172297
span_bug!(tcx.def_span(def_id), "no traits in scope for a doc link")
23182298
})
23192299
},
2300+
2301+
// TODO: Uplift these into
23202302
traits: |tcx, LocalCrate| {
23212303
let mut traits = Vec::new();
23222304
for id in tcx.hir().items() {
@@ -2325,8 +2307,6 @@ pub fn provide(providers: &mut Providers) {
23252307
}
23262308
}
23272309

2328-
// Bring everything into deterministic order.
2329-
traits.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
23302310
tcx.arena.alloc_slice(&traits)
23312311
},
23322312
trait_impls_in_crate: |tcx, LocalCrate| {
@@ -2339,8 +2319,6 @@ pub fn provide(providers: &mut Providers) {
23392319
}
23402320
}
23412321

2342-
// Bring everything into deterministic order.
2343-
trait_impls.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
23442322
tcx.arena.alloc_slice(&trait_impls)
23452323
},
23462324

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use rustc_data_structures::intern::Interned;
4040
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4141
use rustc_data_structures::steal::Steal;
4242
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
43-
use rustc_data_structures::unord::UnordMap;
4443
use rustc_errors::{Diag, ErrorGuaranteed, StashKey};
4544
use rustc_hir as hir;
4645
use rustc_hir::def::{CtorKind, CtorOf, DefKind, DocLinkResMap, LifetimeRes, Res};
@@ -2096,8 +2095,8 @@ pub fn provide(providers: &mut Providers) {
20962095
/// (constructing this map requires touching the entire crate).
20972096
#[derive(Clone, Debug, Default, HashStable)]
20982097
pub struct CrateInherentImpls {
2099-
pub inherent_impls: LocalDefIdMap<Vec<DefId>>,
2100-
pub incoherent_impls: UnordMap<SimplifiedType, Vec<LocalDefId>>,
2098+
pub inherent_impls: FxIndexMap<LocalDefId, Vec<DefId>>,
2099+
pub incoherent_impls: FxIndexMap<SimplifiedType, Vec<LocalDefId>>,
21012100
}
21022101

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

0 commit comments

Comments
 (0)