Skip to content

Commit

Permalink
Auto merge of rust-lang#85905 - cjgillot:one-trait-map, r=Aaron1011
Browse files Browse the repository at this point in the history
Only compute the trait map once

Part of rust-lang#85153

r? `@Aaron1011`
  • Loading branch information
bors committed Jun 2, 2021
2 parents c4f186f + 93b25bd commit d20b9ad
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 69 deletions.
19 changes: 9 additions & 10 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use rustc_ast::walk_list;
use rustc_ast::{self as ast, *};
use rustc_ast_pretty::pprust;
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::Lrc;
use rustc_errors::{struct_span_err, Applicability};
use rustc_hir as hir;
Expand Down Expand Up @@ -198,7 +198,7 @@ pub trait ResolverAstLowering {

fn next_node_id(&mut self) -> NodeId;

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

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

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

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

let mut def_id_to_hir_id = IndexVec::default();

Expand Down
32 changes: 0 additions & 32 deletions compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,35 +550,3 @@ pub fn hash_stable_hashmap<HCX, K, V, R, SK, F>(
entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
entries.hash_stable(hcx, hasher);
}

/// A vector container that makes sure that its items are hashed in a stable
/// order.
#[derive(Debug)]
pub struct StableVec<T>(Vec<T>);

impl<T> StableVec<T> {
pub fn new(v: Vec<T>) -> Self {
StableVec(v)
}
}

impl<T> ::std::ops::Deref for StableVec<T> {
type Target = Vec<T>;

fn deref(&self) -> &Vec<T> {
&self.0
}
}

impl<T, HCX> HashStable<HCX> for StableVec<T>
where
T: HashStable<HCX> + ToStableHashKey<HCX>,
{
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
let StableVec(ref v) = *self;

let mut sorted: Vec<_> = v.iter().map(|x| x.to_stable_hash_key(hcx)).collect();
sorted.sort_unstable();
sorted.hash_stable(hcx, hasher);
}
}
7 changes: 5 additions & 2 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ignore-tidy-filelength
use crate::def::{CtorKind, DefKind, Res};
use crate::def_id::DefId;
crate use crate::hir_id::HirId;
crate use crate::hir_id::{HirId, ItemLocalId};
use crate::{itemlikevisit, LangItem};

use rustc_ast::util::parser::ExprPrecedence;
Expand All @@ -10,6 +10,7 @@ use rustc_ast::{Attribute, FloatTy, IntTy, Label, LitKind, StrStyle, TraitObject
pub use rustc_ast::{BorrowKind, ImplPolarity, IsAuto};
pub use rustc_ast::{CaptureBy, Movability, Mutability};
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
use rustc_macros::HashStable_Generic;
use rustc_span::source_map::Spanned;
Expand Down Expand Up @@ -658,7 +659,9 @@ pub struct Crate<'hir> {
/// they are declared in the static array generated by proc_macro_harness.
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, Box<[TraitCandidate]>>>,

/// Collected attributes from HIR nodes.
pub attrs: BTreeMap<HirId, &'hir [Attribute]>,
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,8 +1127,7 @@ rustc_queries! {
desc { "computing whether impls specialize one another" }
}
query in_scope_traits_map(_: LocalDefId)
-> Option<&'tcx FxHashMap<ItemLocalId, StableVec<TraitCandidate>>> {
eval_always
-> Option<&'tcx FxHashMap<ItemLocalId, Box<[TraitCandidate]>>> {
desc { "traits in scope at a block" }
}

Expand Down
21 changes: 6 additions & 15 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use rustc_attr as attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableVec};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::steal::Steal;
use rustc_data_structures::sync::{self, Lock, Lrc, WorkerLocal};
use rustc_errors::ErrorReported;
Expand Down Expand Up @@ -966,10 +966,6 @@ pub struct GlobalCtxt<'tcx> {
/// Resolutions of `extern crate` items produced by resolver.
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: ExportMap<LocalDefId>,

Expand Down Expand Up @@ -1150,12 +1146,6 @@ impl<'tcx> TyCtxt<'tcx> {
let common_consts = CommonConsts::new(&interners, &common_types);
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 {
sess: s,
lint_store,
Expand All @@ -1169,7 +1159,6 @@ impl<'tcx> TyCtxt<'tcx> {
consts: common_consts,
visibilities: resolutions.visibilities,
extern_crate_map: resolutions.extern_crate_map,
trait_map,
export_map: resolutions.export_map,
maybe_unused_trait_imports: resolutions.maybe_unused_trait_imports,
maybe_unused_extern_crates: resolutions.maybe_unused_extern_crates,
Expand Down Expand Up @@ -2662,8 +2651,10 @@ impl<'tcx> TyCtxt<'tcx> {
struct_lint_level(self.sess, lint, level, src, None, decorate);
}

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

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

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.crate_name = |tcx, id| {
assert_eq!(id, LOCAL_CRATE);
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/ty/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use crate::ty::subst::{GenericArg, SubstsRef};
use crate::ty::util::AlwaysRequiresDrop;
use crate::ty::{self, AdtSizedConstraint, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_data_structures::stable_hasher::StableVec;
use rustc_data_structures::steal::Steal;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::Lrc;
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1959,7 +1959,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
if ns == ValueNS {
let item_name = path.last().unwrap().ident;
let traits = self.traits_in_scope(item_name, ns);
self.r.trait_map.insert(id, traits);
self.r.trait_map.as_mut().unwrap().insert(id, traits);
}

if PrimTy::from_name(path[0].ident.name).is_some() {
Expand Down Expand Up @@ -2435,12 +2435,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// the field name so that we can do some nice error reporting
// later on in typeck.
let traits = self.traits_in_scope(ident, ValueNS);
self.r.trait_map.insert(expr.id, traits);
self.r.trait_map.as_mut().unwrap().insert(expr.id, traits);
}
ExprKind::MethodCall(ref segment, ..) => {
debug!("(recording candidate traits for expr) recording traits for {}", expr.id);
let traits = self.traits_in_scope(segment.ident, ValueNS);
self.r.trait_map.insert(expr.id, traits);
self.r.trait_map.as_mut().unwrap().insert(expr.id, traits);
}
_ => {
// Nothing to do.
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ pub struct Resolver<'a> {
/// `CrateNum` resolutions of `extern crate` items.
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
export_map: ExportMap<LocalDefId>,
trait_map: NodeMap<Vec<TraitCandidate>>,
trait_map: Option<NodeMap<Vec<TraitCandidate>>>,

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

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

fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
Expand Down Expand Up @@ -1286,7 +1286,7 @@ impl<'a> Resolver<'a> {
label_res_map: Default::default(),
extern_crate_map: Default::default(),
export_map: FxHashMap::default(),
trait_map: Default::default(),
trait_map: Some(NodeMap::default()),
underscore_disambiguator: 0,
empty_module,
module_map,
Expand Down

0 comments on commit d20b9ad

Please sign in to comment.