1
Fork 0

Only compute the trait_map once.

This commit is contained in:
Camille GILLOT 2021-04-04 14:24:27 +02:00
parent 59579907ab
commit 139f7ad637
5 changed files with 19 additions and 27 deletions

View file

@ -43,7 +43,8 @@ use rustc_ast::walk_list;
use rustc_ast::{self as ast, *}; use rustc_ast::{self as ast, *};
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_data_structures::captures::Captures; use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stable_hasher::StableVec;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_errors::{struct_span_err, Applicability}; use rustc_errors::{struct_span_err, Applicability};
use rustc_hir as hir; use rustc_hir as hir;
@ -198,7 +199,7 @@ pub trait ResolverAstLowering {
fn next_node_id(&mut self) -> NodeId; fn next_node_id(&mut self) -> NodeId;
fn trait_map(&self) -> &NodeMap<Vec<hir::TraitCandidate>>; fn trait_map(&mut self) -> NodeMap<Vec<hir::TraitCandidate>>;
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId>; fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId>;
@ -501,14 +502,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let proc_macros = let proc_macros =
c.proc_macros.iter().map(|id| self.node_id_to_hir_id[*id].unwrap()).collect(); c.proc_macros.iter().map(|id| self.node_id_to_hir_id[*id].unwrap()).collect();
let trait_map = self let mut trait_map: FxHashMap<_, FxHashMap<_, _>> = FxHashMap::default();
.resolver for (k, v) in self.resolver.trait_map().into_iter() {
.trait_map() if let Some(Some(hir_id)) = self.node_id_to_hir_id.get(k) {
.iter() let map = trait_map.entry(hir_id.owner).or_default();
.filter_map(|(&k, v)| { map.insert(hir_id.local_id, StableVec::new(v.to_vec()));
self.node_id_to_hir_id.get(k).and_then(|id| id.as_ref()).map(|id| (*id, v.clone())) }
}) }
.collect();
let mut def_id_to_hir_id = IndexVec::default(); let mut def_id_to_hir_id = IndexVec::default();

View file

@ -1,7 +1,7 @@
// ignore-tidy-filelength // ignore-tidy-filelength
use crate::def::{CtorKind, DefKind, Res}; use crate::def::{CtorKind, DefKind, Res};
use crate::def_id::DefId; use crate::def_id::DefId;
crate use crate::hir_id::HirId; crate use crate::hir_id::{HirId, ItemLocalId};
use crate::{itemlikevisit, LangItem}; use crate::{itemlikevisit, LangItem};
use rustc_ast::util::parser::ExprPrecedence; use rustc_ast::util::parser::ExprPrecedence;
@ -10,6 +10,8 @@ use rustc_ast::{Attribute, FloatTy, IntTy, Label, LitKind, StrStyle, TraitObject
pub use rustc_ast::{BorrowKind, ImplPolarity, IsAuto}; pub use rustc_ast::{BorrowKind, ImplPolarity, IsAuto};
pub use rustc_ast::{CaptureBy, Movability, Mutability}; pub use rustc_ast::{CaptureBy, Movability, Mutability};
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableVec;
use rustc_data_structures::sync::{par_for_each_in, Send, Sync}; use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
use rustc_macros::HashStable_Generic; use rustc_macros::HashStable_Generic;
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
@ -658,7 +660,9 @@ pub struct Crate<'hir> {
/// they are declared in the static array generated by proc_macro_harness. /// they are declared in the static array generated by proc_macro_harness.
pub proc_macros: Vec<HirId>, pub proc_macros: Vec<HirId>,
pub trait_map: BTreeMap<HirId, Vec<TraitCandidate>>, /// Map indicating what traits are in scope for places where this
/// is relevant; generated by resolve.
pub trait_map: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, StableVec<TraitCandidate>>>,
/// Collected attributes from HIR nodes. /// Collected attributes from HIR nodes.
pub attrs: BTreeMap<HirId, &'hir [Attribute]>, pub attrs: BTreeMap<HirId, &'hir [Attribute]>,

View file

@ -1128,7 +1128,6 @@ rustc_queries! {
} }
query in_scope_traits_map(_: LocalDefId) query in_scope_traits_map(_: LocalDefId)
-> Option<&'tcx FxHashMap<ItemLocalId, StableVec<TraitCandidate>>> { -> Option<&'tcx FxHashMap<ItemLocalId, StableVec<TraitCandidate>>> {
eval_always
desc { "traits in scope at a block" } desc { "traits in scope at a block" }
} }

View file

@ -966,10 +966,6 @@ pub struct GlobalCtxt<'tcx> {
/// Resolutions of `extern crate` items produced by resolver. /// Resolutions of `extern crate` items produced by resolver.
extern_crate_map: FxHashMap<LocalDefId, CrateNum>, extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
/// Map indicating what traits are in scope for places where this
/// is relevant; generated by resolve.
trait_map: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, StableVec<TraitCandidate>>>,
/// Export map produced by name resolution. /// Export map produced by name resolution.
export_map: ExportMap<LocalDefId>, export_map: ExportMap<LocalDefId>,
@ -1150,12 +1146,6 @@ impl<'tcx> TyCtxt<'tcx> {
let common_consts = CommonConsts::new(&interners, &common_types); let common_consts = CommonConsts::new(&interners, &common_types);
let cstore = resolutions.cstore; let cstore = resolutions.cstore;
let mut trait_map: FxHashMap<_, FxHashMap<_, _>> = FxHashMap::default();
for (hir_id, v) in krate.trait_map.iter() {
let map = trait_map.entry(hir_id.owner).or_default();
map.insert(hir_id.local_id, StableVec::new(v.to_vec()));
}
GlobalCtxt { GlobalCtxt {
sess: s, sess: s,
lint_store, lint_store,
@ -1169,7 +1159,6 @@ impl<'tcx> TyCtxt<'tcx> {
consts: common_consts, consts: common_consts,
visibilities: resolutions.visibilities, visibilities: resolutions.visibilities,
extern_crate_map: resolutions.extern_crate_map, extern_crate_map: resolutions.extern_crate_map,
trait_map,
export_map: resolutions.export_map, export_map: resolutions.export_map,
maybe_unused_trait_imports: resolutions.maybe_unused_trait_imports, maybe_unused_trait_imports: resolutions.maybe_unused_trait_imports,
maybe_unused_extern_crates: resolutions.maybe_unused_extern_crates, 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 {
} }
pub fn provide(providers: &mut ty::query::Providers) { pub fn provide(providers: &mut ty::query::Providers) {
providers.in_scope_traits_map = |tcx, id| tcx.gcx.trait_map.get(&id); providers.in_scope_traits_map = |tcx, id| tcx.hir_crate(()).trait_map.get(&id);
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).map(|v| &v[..]); providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).map(|v| &v[..]);
providers.crate_name = |tcx, id| { providers.crate_name = |tcx, id| {
assert_eq!(id, LOCAL_CRATE); assert_eq!(id, LOCAL_CRATE);

View file

@ -1138,8 +1138,8 @@ impl ResolverAstLowering for Resolver<'_> {
self.next_node_id() self.next_node_id()
} }
fn trait_map(&self) -> &NodeMap<Vec<TraitCandidate>> { fn trait_map(&mut self) -> NodeMap<Vec<TraitCandidate>> {
&self.trait_map std::mem::take(&mut self.trait_map)
} }
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> { fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {