Skip to content

Commit

Permalink
Auto merge of rust-lang#78448 - rylev:cache-foreign_modules, r=wesley…
Browse files Browse the repository at this point in the history
…wiser

foreign_modules query hash table lookups

When compiling a large monolithic crate we're seeing huge times in the `foreign_modules` query due to repeated iteration over foreign modules (in order to find a module by its id). This implements hash table lookups so that which massively reduces time spent in that query in this particular case. We'll need to see if the overhead of creating the hash table has a negative impact on performance in more normal compilation scenarios.

I'm working with `@wesleywiser` on this.
  • Loading branch information
bors committed Nov 3, 2020
2 parents 4c0c5e0 + 81444b2 commit 7b5a9e9
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ pub fn provide_both(providers: &mut Providers) {
.collect::<FxHashMap<_, _>>();

let mut ret = FxHashMap::default();
for lib in tcx.foreign_modules(cnum).iter() {
let module = def_id_to_native_lib.get(&lib.def_id).and_then(|s| s.wasm_import_module);
for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
let module = def_id_to_native_lib.get(&def_id).and_then(|s| s.wasm_import_module);
let module = match module {
Some(s) => s,
None => continue,
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,6 @@ pub fn provide_both(providers: &mut Providers) {

providers.dllimport_foreign_items = |tcx, krate| {
let module_map = tcx.foreign_modules(krate);
let module_map =
module_map.iter().map(|lib| (lib.def_id, lib)).collect::<FxHashMap<_, _>>();

let dllimports = tcx
.native_libraries(krate)
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1414,12 +1414,14 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}
}

fn get_foreign_modules(&self, tcx: TyCtxt<'tcx>) -> &'tcx [ForeignModule] {
fn get_foreign_modules(&self, tcx: TyCtxt<'tcx>) -> Lrc<FxHashMap<DefId, ForeignModule>> {
if self.root.is_proc_macro_crate() {
// Proc macro crates do not have any *target* foreign modules.
&[]
Lrc::new(FxHashMap::default())
} else {
tcx.arena.alloc_from_iter(self.root.foreign_modules.decode((self, tcx.sess)))
let modules: FxHashMap<DefId, ForeignModule> =
self.root.foreign_modules.decode((self, tcx.sess)).map(|m| (m.def_id, m)).collect();
Lrc::new(modules)
}
}

Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use crate::rmeta::{self, encoder};

use rustc_ast as ast;
use rustc_ast::expand::allocator::AllocatorKind;
use rustc_data_structures::stable_map::FxHashMap;
use rustc_data_structures::svh::Svh;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
use rustc_middle::hir::exports::Export;
use rustc_middle::middle::cstore::ForeignModule;
use rustc_middle::middle::cstore::{CrateSource, CrateStore, EncodedMetadata};
use rustc_middle::middle::exported_symbols::ExportedSymbol;
use rustc_middle::middle::stability::DeprecationEntry;
Expand Down Expand Up @@ -266,9 +268,8 @@ pub fn provide(providers: &mut Providers) {
Some(id) => id,
None => return false,
};
tcx.foreign_modules(id.krate)
.iter()
.find(|m| m.def_id == fm_id)
let map = tcx.foreign_modules(id.krate);
map.get(&fm_id)
.expect("failed to find foreign module")
.foreign_items
.contains(&id)
Expand All @@ -281,7 +282,9 @@ pub fn provide(providers: &mut Providers) {
},
foreign_modules: |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
&tcx.arena.alloc(foreign_modules::collect(tcx))[..]
let modules: FxHashMap<DefId, ForeignModule> =
foreign_modules::collect(tcx).into_iter().map(|m| (m.def_id, m)).collect();
Lrc::new(modules)
},
link_args: |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,7 @@ impl EncodeContext<'a, 'tcx> {
fn encode_foreign_modules(&mut self) -> Lazy<[ForeignModule]> {
empty_proc_macro!(self);
let foreign_modules = self.tcx.foreign_modules(LOCAL_CRATE);
self.lazy(foreign_modules.iter().cloned())
self.lazy(foreign_modules.iter().map(|(_, m)| m).cloned())
}

fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ rustc_queries! {
}

Other {
query foreign_modules(_: CrateNum) -> &'tcx [ForeignModule] {
query foreign_modules(_: CrateNum) -> Lrc<FxHashMap<DefId, ForeignModule>> {
desc { "looking up the foreign modules of a linked crate" }
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/diagnostic_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> FxHashMap<Symbol, DefId> {
tcx.hir().krate().visit_all_item_likes(&mut collector);
// FIXME(visit_all_item_likes): Foreign items are not visited
// here, so we have to manually look at them for now.
for foreign_module in tcx.foreign_modules(LOCAL_CRATE) {
for (_, foreign_module) in tcx.foreign_modules(LOCAL_CRATE).iter() {
for &foreign_item in foreign_module.foreign_items.iter() {
match tcx.hir().get(tcx.hir().local_def_id_to_hir_id(foreign_item.expect_local())) {
hir::Node::ForeignItem(item) => {
Expand Down

0 comments on commit 7b5a9e9

Please sign in to comment.