Skip to content

Commit e291be3

Browse files
committed
Only compute the trait_map once.
1 parent 7f9ab03 commit e291be3

File tree

5 files changed

+19
-27
lines changed

5 files changed

+19
-27
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ 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};
47+
use rustc_data_structures::stable_hasher::StableVec;
4748
use rustc_data_structures::sync::Lrc;
4849
use rustc_errors::{struct_span_err, Applicability};
4950
use rustc_hir as hir;
@@ -198,7 +199,7 @@ pub trait ResolverAstLowering {
198199

199200
fn next_node_id(&mut self) -> NodeId;
200201

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

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

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

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();
505+
let mut trait_map: FxHashMap<_, FxHashMap<_, _>> = FxHashMap::default();
506+
for (k, v) in self.resolver.trait_map().into_iter() {
507+
if let Some(Some(hir_id)) = self.node_id_to_hir_id.get(k) {
508+
let map = trait_map.entry(hir_id.owner).or_default();
509+
map.insert(hir_id.local_id, StableVec::new(v.to_vec()));
510+
}
511+
}
512512

513513
let mut def_id_to_hir_id = IndexVec::default();
514514

compiler/rustc_hir/src/hir.rs

+6-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,8 @@ 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;
14+
use rustc_data_structures::stable_hasher::StableVec;
1315
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
1416
use rustc_macros::HashStable_Generic;
1517
use rustc_span::source_map::Spanned;
@@ -658,7 +660,9 @@ pub struct Crate<'hir> {
658660
/// they are declared in the static array generated by proc_macro_harness.
659661
pub proc_macros: Vec<HirId>,
660662

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

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

compiler/rustc_middle/src/query/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,6 @@ rustc_queries! {
11281128
}
11291129
query in_scope_traits_map(_: LocalDefId)
11301130
-> Option<&'tcx FxHashMap<ItemLocalId, StableVec<TraitCandidate>>> {
1131-
eval_always
11321131
desc { "traits in scope at a block" }
11331132
}
11341133

compiler/rustc_middle/src/ty/context.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -2793,7 +2782,7 @@ fn ptr_eq<T, U>(t: *const T, u: *const U) -> bool {
27932782
}
27942783

27952784
pub fn provide(providers: &mut ty::query::Providers) {
2796-
providers.in_scope_traits_map = |tcx, id| tcx.gcx.trait_map.get(&id);
2785+
providers.in_scope_traits_map = |tcx, id| tcx.hir_crate(()).trait_map.get(&id);
27972786
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).map(|v| &v[..]);
27982787
providers.crate_name = |tcx, id| {
27992788
assert_eq!(id, LOCAL_CRATE);

compiler/rustc_resolve/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -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 trait_map(&mut self) -> NodeMap<Vec<TraitCandidate>> {
1142+
std::mem::take(&mut self.trait_map)
11431143
}
11441144

11451145
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {

0 commit comments

Comments
 (0)