Skip to content

Commit d20b9ad

Browse files
committed
Auto merge of #85905 - cjgillot:one-trait-map, r=Aaron1011
Only compute the trait map once Part of #85153 r? `@Aaron1011`
2 parents c4f186f + 93b25bd commit d20b9ad

File tree

8 files changed

+28
-69
lines changed

8 files changed

+28
-69
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use rustc_ast::walk_list;
4343
use rustc_ast::{self as ast, *};
4444
use rustc_ast_pretty::pprust;
4545
use rustc_data_structures::captures::Captures;
46-
use rustc_data_structures::fx::FxHashSet;
46+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4747
use rustc_data_structures::sync::Lrc;
4848
use rustc_errors::{struct_span_err, Applicability};
4949
use rustc_hir as hir;
@@ -198,7 +198,7 @@ pub trait ResolverAstLowering {
198198

199199
fn next_node_id(&mut self) -> NodeId;
200200

201-
fn trait_map(&self) -> &NodeMap<Vec<hir::TraitCandidate>>;
201+
fn take_trait_map(&mut self) -> NodeMap<Vec<hir::TraitCandidate>>;
202202

203203
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId>;
204204

@@ -501,14 +501,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
501501
let proc_macros =
502502
c.proc_macros.iter().map(|id| self.node_id_to_hir_id[*id].unwrap()).collect();
503503

504-
let trait_map = self
505-
.resolver
506-
.trait_map()
507-
.iter()
508-
.filter_map(|(&k, v)| {
509-
self.node_id_to_hir_id.get(k).and_then(|id| id.as_ref()).map(|id| (*id, v.clone()))
510-
})
511-
.collect();
504+
let mut trait_map: FxHashMap<_, FxHashMap<_, _>> = FxHashMap::default();
505+
for (k, v) in self.resolver.take_trait_map().into_iter() {
506+
if let Some(Some(hir_id)) = self.node_id_to_hir_id.get(k) {
507+
let map = trait_map.entry(hir_id.owner).or_default();
508+
map.insert(hir_id.local_id, v.into_boxed_slice());
509+
}
510+
}
512511

513512
let mut def_id_to_hir_id = IndexVec::default();
514513

compiler/rustc_data_structures/src/stable_hasher.rs

-32
Original file line numberDiff line numberDiff line change
@@ -550,35 +550,3 @@ pub fn hash_stable_hashmap<HCX, K, V, R, SK, F>(
550550
entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
551551
entries.hash_stable(hcx, hasher);
552552
}
553-
554-
/// A vector container that makes sure that its items are hashed in a stable
555-
/// order.
556-
#[derive(Debug)]
557-
pub struct StableVec<T>(Vec<T>);
558-
559-
impl<T> StableVec<T> {
560-
pub fn new(v: Vec<T>) -> Self {
561-
StableVec(v)
562-
}
563-
}
564-
565-
impl<T> ::std::ops::Deref for StableVec<T> {
566-
type Target = Vec<T>;
567-
568-
fn deref(&self) -> &Vec<T> {
569-
&self.0
570-
}
571-
}
572-
573-
impl<T, HCX> HashStable<HCX> for StableVec<T>
574-
where
575-
T: HashStable<HCX> + ToStableHashKey<HCX>,
576-
{
577-
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
578-
let StableVec(ref v) = *self;
579-
580-
let mut sorted: Vec<_> = v.iter().map(|x| x.to_stable_hash_key(hcx)).collect();
581-
sorted.sort_unstable();
582-
sorted.hash_stable(hcx, hasher);
583-
}
584-
}

compiler/rustc_hir/src/hir.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ignore-tidy-filelength
22
use crate::def::{CtorKind, DefKind, Res};
33
use crate::def_id::DefId;
4-
crate use crate::hir_id::HirId;
4+
crate use crate::hir_id::{HirId, ItemLocalId};
55
use crate::{itemlikevisit, LangItem};
66

77
use rustc_ast::util::parser::ExprPrecedence;
@@ -10,6 +10,7 @@ use rustc_ast::{Attribute, FloatTy, IntTy, Label, LitKind, StrStyle, TraitObject
1010
pub use rustc_ast::{BorrowKind, ImplPolarity, IsAuto};
1111
pub use rustc_ast::{CaptureBy, Movability, Mutability};
1212
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
13+
use rustc_data_structures::fx::FxHashMap;
1314
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
1415
use rustc_macros::HashStable_Generic;
1516
use rustc_span::source_map::Spanned;
@@ -658,7 +659,9 @@ pub struct Crate<'hir> {
658659
/// they are declared in the static array generated by proc_macro_harness.
659660
pub proc_macros: Vec<HirId>,
660661

661-
pub trait_map: BTreeMap<HirId, Vec<TraitCandidate>>,
662+
/// Map indicating what traits are in scope for places where this
663+
/// is relevant; generated by resolve.
664+
pub trait_map: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Box<[TraitCandidate]>>>,
662665

663666
/// Collected attributes from HIR nodes.
664667
pub attrs: BTreeMap<HirId, &'hir [Attribute]>,

compiler/rustc_middle/src/query/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1127,8 +1127,7 @@ rustc_queries! {
11271127
desc { "computing whether impls specialize one another" }
11281128
}
11291129
query in_scope_traits_map(_: LocalDefId)
1130-
-> Option<&'tcx FxHashMap<ItemLocalId, StableVec<TraitCandidate>>> {
1131-
eval_always
1130+
-> Option<&'tcx FxHashMap<ItemLocalId, Box<[TraitCandidate]>>> {
11321131
desc { "traits in scope at a block" }
11331132
}
11341133

compiler/rustc_middle/src/ty/context.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_attr as attr;
3131
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3232
use rustc_data_structures::profiling::SelfProfilerRef;
3333
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
34-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableVec};
34+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3535
use rustc_data_structures::steal::Steal;
3636
use rustc_data_structures::sync::{self, Lock, Lrc, WorkerLocal};
3737
use rustc_errors::ErrorReported;
@@ -966,10 +966,6 @@ pub struct GlobalCtxt<'tcx> {
966966
/// Resolutions of `extern crate` items produced by resolver.
967967
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
968968

969-
/// Map indicating what traits are in scope for places where this
970-
/// is relevant; generated by resolve.
971-
trait_map: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, StableVec<TraitCandidate>>>,
972-
973969
/// Export map produced by name resolution.
974970
export_map: ExportMap<LocalDefId>,
975971

@@ -1150,12 +1146,6 @@ impl<'tcx> TyCtxt<'tcx> {
11501146
let common_consts = CommonConsts::new(&interners, &common_types);
11511147
let cstore = resolutions.cstore;
11521148

1153-
let mut trait_map: FxHashMap<_, FxHashMap<_, _>> = FxHashMap::default();
1154-
for (hir_id, v) in krate.trait_map.iter() {
1155-
let map = trait_map.entry(hir_id.owner).or_default();
1156-
map.insert(hir_id.local_id, StableVec::new(v.to_vec()));
1157-
}
1158-
11591149
GlobalCtxt {
11601150
sess: s,
11611151
lint_store,
@@ -1169,7 +1159,6 @@ impl<'tcx> TyCtxt<'tcx> {
11691159
consts: common_consts,
11701160
visibilities: resolutions.visibilities,
11711161
extern_crate_map: resolutions.extern_crate_map,
1172-
trait_map,
11731162
export_map: resolutions.export_map,
11741163
maybe_unused_trait_imports: resolutions.maybe_unused_trait_imports,
11751164
maybe_unused_extern_crates: resolutions.maybe_unused_extern_crates,
@@ -2662,8 +2651,10 @@ impl<'tcx> TyCtxt<'tcx> {
26622651
struct_lint_level(self.sess, lint, level, src, None, decorate);
26632652
}
26642653

2665-
pub fn in_scope_traits(self, id: HirId) -> Option<&'tcx StableVec<TraitCandidate>> {
2666-
self.in_scope_traits_map(id.owner).and_then(|map| map.get(&id.local_id))
2654+
pub fn in_scope_traits(self, id: HirId) -> Option<&'tcx [TraitCandidate]> {
2655+
let map = self.in_scope_traits_map(id.owner)?;
2656+
let candidates = map.get(&id.local_id)?;
2657+
Some(&*candidates)
26672658
}
26682659

26692660
pub fn named_region(self, id: HirId) -> Option<resolve_lifetime::Region> {
@@ -2793,7 +2784,7 @@ fn ptr_eq<T, U>(t: *const T, u: *const U) -> bool {
27932784
}
27942785

27952786
pub fn provide(providers: &mut ty::query::Providers) {
2796-
providers.in_scope_traits_map = |tcx, id| tcx.gcx.trait_map.get(&id);
2787+
providers.in_scope_traits_map = |tcx, id| tcx.hir_crate(()).trait_map.get(&id);
27972788
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).map(|v| &v[..]);
27982789
providers.crate_name = |tcx, id| {
27992790
assert_eq!(id, LOCAL_CRATE);

compiler/rustc_middle/src/ty/query/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use crate::ty::subst::{GenericArg, SubstsRef};
3434
use crate::ty::util::AlwaysRequiresDrop;
3535
use crate::ty::{self, AdtSizedConstraint, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
3636
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
37-
use rustc_data_structures::stable_hasher::StableVec;
3837
use rustc_data_structures::steal::Steal;
3938
use rustc_data_structures::svh::Svh;
4039
use rustc_data_structures::sync::Lrc;

compiler/rustc_resolve/src/late.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1959,7 +1959,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19591959
if ns == ValueNS {
19601960
let item_name = path.last().unwrap().ident;
19611961
let traits = self.traits_in_scope(item_name, ns);
1962-
self.r.trait_map.insert(id, traits);
1962+
self.r.trait_map.as_mut().unwrap().insert(id, traits);
19631963
}
19641964

19651965
if PrimTy::from_name(path[0].ident.name).is_some() {
@@ -2435,12 +2435,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
24352435
// the field name so that we can do some nice error reporting
24362436
// later on in typeck.
24372437
let traits = self.traits_in_scope(ident, ValueNS);
2438-
self.r.trait_map.insert(expr.id, traits);
2438+
self.r.trait_map.as_mut().unwrap().insert(expr.id, traits);
24392439
}
24402440
ExprKind::MethodCall(ref segment, ..) => {
24412441
debug!("(recording candidate traits for expr) recording traits for {}", expr.id);
24422442
let traits = self.traits_in_scope(segment.ident, ValueNS);
2443-
self.r.trait_map.insert(expr.id, traits);
2443+
self.r.trait_map.as_mut().unwrap().insert(expr.id, traits);
24442444
}
24452445
_ => {
24462446
// Nothing to do.

compiler/rustc_resolve/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ pub struct Resolver<'a> {
909909
/// `CrateNum` resolutions of `extern crate` items.
910910
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
911911
export_map: ExportMap<LocalDefId>,
912-
trait_map: NodeMap<Vec<TraitCandidate>>,
912+
trait_map: Option<NodeMap<Vec<TraitCandidate>>>,
913913

914914
/// A map from nodes to anonymous modules.
915915
/// Anonymous modules are pseudo-modules that are implicitly created around items
@@ -1138,8 +1138,8 @@ impl ResolverAstLowering for Resolver<'_> {
11381138
self.next_node_id()
11391139
}
11401140

1141-
fn trait_map(&self) -> &NodeMap<Vec<TraitCandidate>> {
1142-
&self.trait_map
1141+
fn take_trait_map(&mut self) -> NodeMap<Vec<TraitCandidate>> {
1142+
std::mem::replace(&mut self.trait_map, None).unwrap()
11431143
}
11441144

11451145
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
@@ -1286,7 +1286,7 @@ impl<'a> Resolver<'a> {
12861286
label_res_map: Default::default(),
12871287
extern_crate_map: Default::default(),
12881288
export_map: FxHashMap::default(),
1289-
trait_map: Default::default(),
1289+
trait_map: Some(NodeMap::default()),
12901290
underscore_disambiguator: 0,
12911291
empty_module,
12921292
module_map,

0 commit comments

Comments
 (0)