Skip to content

Commit 6e984fb

Browse files
Avoid unnecessary sorting of traits
1 parent c29082f commit 6e984fb

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;
@@ -1478,10 +1478,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14781478
}
14791479
}
14801480

1481-
let inherent_impls = tcx.with_stable_hashing_context(|hcx| {
1482-
tcx.crate_inherent_impls(()).unwrap().inherent_impls.to_sorted(&hcx, true)
1483-
});
1484-
for (def_id, impls) in inherent_impls {
1481+
for (def_id, impls) in &tcx.crate_inherent_impls(()).unwrap().inherent_impls {
14851482
record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| {
14861483
assert!(def_id.is_local());
14871484
def_id.index
@@ -1952,8 +1949,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19521949
fn encode_impls(&mut self) -> LazyArray<TraitImpls> {
19531950
empty_proc_macro!(self);
19541951
let tcx = self.tcx;
1955-
let mut fx_hash_map: FxHashMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
1956-
FxHashMap::default();
1952+
let mut trait_impls: FxIndexMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
1953+
FxIndexMap::default();
19571954

19581955
for id in tcx.hir().items() {
19591956
let DefKind::Impl { of_trait } = tcx.def_kind(id.owner_id) else {
@@ -1973,7 +1970,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19731970
trait_ref.self_ty(),
19741971
TreatParams::AsCandidateKey,
19751972
);
1976-
fx_hash_map
1973+
trait_impls
19771974
.entry(trait_ref.def_id)
19781975
.or_default()
19791976
.push((id.owner_id.def_id.local_def_index, simplified_self_ty));
@@ -1994,47 +1991,30 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19941991
}
19951992
}
19961993

1997-
let mut all_impls: Vec<_> = fx_hash_map.into_iter().collect();
1998-
1999-
// Bring everything into deterministic order for hashing
2000-
all_impls.sort_by_cached_key(|&(trait_def_id, _)| tcx.def_path_hash(trait_def_id));
2001-
2002-
let all_impls: Vec<_> = all_impls
1994+
let trait_impls: Vec<_> = trait_impls
20031995
.into_iter()
2004-
.map(|(trait_def_id, mut impls)| {
2005-
// Bring everything into deterministic order for hashing
2006-
impls.sort_by_cached_key(|&(index, _)| {
2007-
tcx.hir().def_path_hash(LocalDefId { local_def_index: index })
2008-
});
2009-
2010-
TraitImpls {
2011-
trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
2012-
impls: self.lazy_array(&impls),
2013-
}
1996+
.map(|(trait_def_id, impls)| TraitImpls {
1997+
trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
1998+
impls: self.lazy_array(&impls),
20141999
})
20152000
.collect();
20162001

2017-
self.lazy_array(&all_impls)
2002+
self.lazy_array(&trait_impls)
20182003
}
20192004

20202005
#[instrument(level = "debug", skip(self))]
20212006
fn encode_incoherent_impls(&mut self) -> LazyArray<IncoherentImpls> {
20222007
empty_proc_macro!(self);
20232008
let tcx = self.tcx;
2024-
let all_impls = tcx.with_stable_hashing_context(|hcx| {
2025-
tcx.crate_inherent_impls(()).unwrap().incoherent_impls.to_sorted(&hcx, true)
2026-
});
20272009

2028-
let all_impls: Vec<_> = all_impls
2029-
.into_iter()
2030-
.map(|(&simp, impls)| {
2031-
let mut impls: Vec<_> =
2032-
impls.into_iter().map(|def_id| def_id.local_def_index).collect();
2033-
impls.sort_by_cached_key(|&local_def_index| {
2034-
tcx.hir().def_path_hash(LocalDefId { local_def_index })
2035-
});
2036-
2037-
IncoherentImpls { self_ty: simp, impls: self.lazy_array(impls) }
2010+
let all_impls: Vec<_> = tcx
2011+
.crate_inherent_impls(())
2012+
.unwrap()
2013+
.incoherent_impls
2014+
.iter()
2015+
.map(|(&simp, impls)| IncoherentImpls {
2016+
self_ty: simp,
2017+
impls: self.lazy_array(impls.iter().map(|def_id| def_id.local_def_index)),
20382018
})
20392019
.collect();
20402020

@@ -2278,6 +2258,8 @@ pub fn provide(providers: &mut Providers) {
22782258
span_bug!(tcx.def_span(def_id), "no traits in scope for a doc link")
22792259
})
22802260
},
2261+
2262+
// TODO: Uplift these into
22812263
traits: |tcx, LocalCrate| {
22822264
let mut traits = Vec::new();
22832265
for id in tcx.hir().items() {
@@ -2286,8 +2268,6 @@ pub fn provide(providers: &mut Providers) {
22862268
}
22872269
}
22882270

2289-
// Bring everything into deterministic order.
2290-
traits.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
22912271
tcx.arena.alloc_slice(&traits)
22922272
},
22932273
trait_impls_in_crate: |tcx, LocalCrate| {
@@ -2300,8 +2280,6 @@ pub fn provide(providers: &mut Providers) {
23002280
}
23012281
}
23022282

2303-
// Bring everything into deterministic order.
2304-
trait_impls.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
23052283
tcx.arena.alloc_slice(&trait_impls)
23062284
},
23072285

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use rustc_data_structures::intern::Interned;
3838
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3939
use rustc_data_structures::steal::Steal;
4040
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
41-
use rustc_data_structures::unord::UnordMap;
4241
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, StashKey};
4342
use rustc_hir as hir;
4443
use rustc_hir::def::{CtorKind, CtorOf, DefKind, DocLinkResMap, LifetimeRes, Res};
@@ -2006,8 +2005,8 @@ pub fn provide(providers: &mut Providers) {
20062005
/// (constructing this map requires touching the entire crate).
20072006
#[derive(Clone, Debug, Default, HashStable)]
20082007
pub struct CrateInherentImpls {
2009-
pub inherent_impls: LocalDefIdMap<Vec<DefId>>,
2010-
pub incoherent_impls: UnordMap<SimplifiedType, Vec<LocalDefId>>,
2008+
pub inherent_impls: FxIndexMap<LocalDefId, Vec<DefId>>,
2009+
pub incoherent_impls: FxIndexMap<SimplifiedType, Vec<LocalDefId>>,
20112010
}
20122011

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

0 commit comments

Comments
 (0)