Skip to content

Commit d28a9c3

Browse files
committed
Auto merge of #65240 - michaelwoerister:sp-review-3, r=Mark-Simulacrum
self-profiling: Add events for metadata loading (plus a small dep-tracking optimization) This PR - adds self-profiling events related to loading things from crate metadata - makes the compiler cache the `DepNodeIndex` of upstream crates, so that they don't have to be looked up over and over. The commits are best reviewed in isolation. Self-profiling tracking issue: #58967 r? @Mark-Simulacrum cc @wesleywiser
2 parents 36d4506 + a9853fc commit d28a9c3

File tree

6 files changed

+55
-24
lines changed

6 files changed

+55
-24
lines changed

src/librustc/dep_graph/graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ rustc_index::newtype_index! {
3030
}
3131

3232
impl DepNodeIndex {
33-
const INVALID: DepNodeIndex = DepNodeIndex::MAX;
33+
pub const INVALID: DepNodeIndex = DepNodeIndex::MAX;
3434
}
3535

3636
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]

src/librustc_metadata/creader.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
use crate::cstore::{self, CStore, CrateSource, MetadataBlob};
44
use crate::locator::{self, CratePaths};
55
use crate::schema::{CrateRoot, CrateDep};
6-
use rustc_data_structures::sync::{Lrc, RwLock, Lock};
6+
use rustc_data_structures::sync::{Lrc, RwLock, Lock, AtomicCell};
77

88
use rustc::hir::def_id::CrateNum;
99
use rustc_data_structures::svh::Svh;
10+
use rustc::dep_graph::DepNodeIndex;
1011
use rustc::middle::cstore::DepKind;
1112
use rustc::mir::interpret::AllocDecodingState;
1213
use rustc::session::{Session, CrateDisambiguator};
@@ -196,6 +197,9 @@ impl<'a> CrateLoader<'a> {
196197
dep_kind: DepKind,
197198
name: Symbol
198199
) -> (CrateNum, Lrc<cstore::CrateMetadata>) {
200+
let _prof_timer =
201+
self.sess.prof.generic_activity("metadata_register_crate");
202+
199203
let crate_root = lib.metadata.get_root();
200204
self.verify_no_symbol_conflicts(span, &crate_root);
201205

@@ -271,7 +275,8 @@ impl<'a> CrateLoader<'a> {
271275
},
272276
private_dep,
273277
span,
274-
raw_proc_macros
278+
raw_proc_macros,
279+
dep_node_index: AtomicCell::new(DepNodeIndex::INVALID),
275280
};
276281

277282
let cmeta = Lrc::new(cmeta);

src/librustc_metadata/cstore.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// crates and libraries
33

44
use crate::schema;
5+
use rustc::dep_graph::DepNodeIndex;
56
use rustc::hir::def_id::{CrateNum, DefIndex};
67
use rustc::hir::map::definitions::DefPathTable;
78
use rustc::middle::cstore::{DepKind, ExternCrate, MetadataLoader};
89
use rustc::mir::interpret::AllocDecodingState;
910
use rustc_index::vec::IndexVec;
1011
use rustc::util::nodemap::{FxHashMap, NodeMap};
1112

12-
use rustc_data_structures::sync::{Lrc, RwLock, Lock};
13+
use rustc_data_structures::sync::{Lrc, RwLock, Lock, AtomicCell};
1314
use syntax::ast;
1415
use syntax::ext::base::SyntaxExtension;
1516
use syntax_pos;
@@ -83,6 +84,13 @@ pub struct CrateMetadata {
8384
pub span: Span,
8485

8586
pub raw_proc_macros: Option<&'static [ProcMacro]>,
87+
88+
/// The `DepNodeIndex` of the `DepNode` representing this upstream crate.
89+
/// It is initialized on the first access in `get_crate_dep_node_index()`.
90+
/// Do not access the value directly, as it might not have been initialized
91+
/// yet.
92+
/// The field must always be initialized to `DepNodeIndex::INVALID`.
93+
pub(super) dep_node_index: AtomicCell<DepNodeIndex>,
8694
}
8795

8896
pub struct CStore {

src/librustc_metadata/cstore_impl.rs

+11-20
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,22 @@ macro_rules! provide {
4848
$tcx: TyCtxt<$lt>,
4949
def_id_arg: T,
5050
) -> <ty::queries::$name<$lt> as QueryConfig<$lt>>::Value {
51+
let _prof_timer =
52+
$tcx.prof.generic_activity("metadata_decode_entry");
53+
5154
#[allow(unused_variables)]
5255
let ($def_id, $other) = def_id_arg.into_args();
5356
assert!(!$def_id.is_local());
5457

55-
let def_path_hash = $tcx.def_path_hash(DefId {
56-
krate: $def_id.krate,
57-
index: CRATE_DEF_INDEX
58-
});
59-
let dep_node = def_path_hash
60-
.to_dep_node(rustc::dep_graph::DepKind::CrateMetadata);
61-
// The DepNodeIndex of the DepNode::CrateMetadata should be
62-
// cached somewhere, so that we can use read_index().
63-
$tcx.dep_graph.read(dep_node);
64-
6558
let $cdata = $tcx.crate_data_as_rc_any($def_id.krate);
6659
let $cdata = $cdata.downcast_ref::<cstore::CrateMetadata>()
6760
.expect("CrateStore created data is not a CrateMetadata");
61+
62+
if $tcx.dep_graph.is_fully_enabled() {
63+
let crate_dep_node_index = $cdata.get_crate_dep_node_index($tcx);
64+
$tcx.dep_graph.read_index(crate_dep_node_index);
65+
}
66+
6867
$compute
6968
})*
7069

@@ -449,6 +448,8 @@ impl cstore::CStore {
449448
}
450449

451450
pub fn load_macro_untracked(&self, id: DefId, sess: &Session) -> LoadedMacro {
451+
let _prof_timer = sess.prof.generic_activity("metadata_load_macro");
452+
452453
let data = self.get_crate_data(id.krate);
453454
if data.is_proc_macro_crate() {
454455
return LoadedMacro::ProcMacro(data.load_proc_macro(id.index, sess));
@@ -526,20 +527,10 @@ impl CrateStore for cstore::CStore {
526527
/// parent `DefId` as well as some idea of what kind of data the
527528
/// `DefId` refers to.
528529
fn def_key(&self, def: DefId) -> DefKey {
529-
// Note: loading the def-key (or def-path) for a def-id is not
530-
// a *read* of its metadata. This is because the def-id is
531-
// really just an interned shorthand for a def-path, which is the
532-
// canonical name for an item.
533-
//
534-
// self.dep_graph.read(DepNode::MetaData(def));
535530
self.get_crate_data(def.krate).def_key(def.index)
536531
}
537532

538533
fn def_path(&self, def: DefId) -> DefPath {
539-
// See `Note` above in `def_key()` for why this read is
540-
// commented out:
541-
//
542-
// self.dep_graph.read(DepNode::MetaData(def));
543534
self.get_crate_data(def.krate).def_path(def.index)
544535
}
545536

src/librustc_metadata/decoder.rs

+25
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc::hir::def::{self, Res, DefKind, CtorOf, CtorKind};
1313
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1414
use rustc_data_structures::fingerprint::Fingerprint;
1515
use rustc_data_structures::fx::FxHashMap;
16+
use rustc::dep_graph::{DepNodeIndex, DepKind};
1617
use rustc::middle::lang_items;
1718
use rustc::mir::{self, interpret};
1819
use rustc::mir::interpret::AllocDecodingSession;
@@ -1365,6 +1366,30 @@ impl<'a, 'tcx> CrateMetadata {
13651366
// This shouldn't borrow twice, but there is no way to downgrade RefMut to Ref.
13661367
self.source_map_import_info.borrow()
13671368
}
1369+
1370+
/// Get the `DepNodeIndex` corresponding this crate. The result of this
1371+
/// method is cached in the `dep_node_index` field.
1372+
pub(super) fn get_crate_dep_node_index(&self, tcx: TyCtxt<'tcx>) -> DepNodeIndex {
1373+
let mut dep_node_index = self.dep_node_index.load();
1374+
1375+
if unlikely!(dep_node_index == DepNodeIndex::INVALID) {
1376+
// We have not cached the DepNodeIndex for this upstream crate yet,
1377+
// so use the dep-graph to find it out and cache it.
1378+
// Note that multiple threads can enter this block concurrently.
1379+
// That is fine because the DepNodeIndex remains constant
1380+
// throughout the whole compilation session, and multiple stores
1381+
// would always write the same value.
1382+
1383+
let def_path_hash = self.def_path_hash(CRATE_DEF_INDEX);
1384+
let dep_node = def_path_hash.to_dep_node(DepKind::CrateMetadata);
1385+
1386+
dep_node_index = tcx.dep_graph.dep_node_index_of(&dep_node);
1387+
assert!(dep_node_index != DepNodeIndex::INVALID);
1388+
self.dep_node_index.store(dep_node_index);
1389+
}
1390+
1391+
dep_node_index
1392+
}
13681393
}
13691394

13701395
// Cannot be implemented on 'ProcMacro', as libproc_macro

src/librustc_metadata/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
22

33
#![feature(box_patterns)]
4+
#![feature(core_intrinsics)]
45
#![feature(crate_visibility_modifier)]
56
#![feature(drain_filter)]
67
#![feature(in_band_lifetimes)]
@@ -11,6 +12,7 @@
1112
#![feature(rustc_private)]
1213
#![feature(slice_patterns)]
1314
#![feature(specialization)]
15+
#![feature(stmt_expr_attributes)]
1416

1517
#![recursion_limit="256"]
1618

0 commit comments

Comments
 (0)