Skip to content

Commit 8982c67

Browse files
authored
Rollup merge of #50532 - michaelwoerister:lockless-cnum-map, r=Zoxc
Don't use Lock for heavily accessed CrateMetadata::cnum_map. The `cnum_map` in `CrateMetadata` is used for two things: 1. to map `CrateNums` between crates (used a lot during decoding) 2. to construct the (reverse) post order of the crate graph For the second case, we need to modify the map after the fact, which is why the map is wrapped in a `Lock`. This is bad for the first case, which does not need the modification and does lots of small reads from the map. This PR splits case (2) out into a separate `dependencies` field. This allows to make the `cnum_map` immutable (and shifts the interior mutability to a less busy data structure). Fixes #50502 r? @Zoxc
2 parents 072bfa6 + ea49428 commit 8982c67

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/librustc_metadata/creader.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ impl<'a> CrateLoader<'a> {
219219

220220
let cnum_map = self.resolve_crate_deps(root, &crate_root, &metadata, cnum, span, dep_kind);
221221

222+
let dependencies: Vec<CrateNum> = cnum_map.iter().cloned().collect();
223+
222224
let def_path_table = record_time(&self.sess.perf_stats.decode_def_path_tables_time, || {
223225
crate_root.def_path_table.decode((&metadata, self.sess))
224226
});
@@ -239,8 +241,9 @@ impl<'a> CrateLoader<'a> {
239241
}),
240242
root: crate_root,
241243
blob: metadata,
242-
cnum_map: Lock::new(cnum_map),
244+
cnum_map,
243245
cnum,
246+
dependencies: Lock::new(dependencies),
244247
codemap_import_info: RwLock::new(vec![]),
245248
attribute_cache: Lock::new([Vec::new(), Vec::new()]),
246249
dep_kind: Lock::new(dep_kind),
@@ -392,7 +395,7 @@ impl<'a> CrateLoader<'a> {
392395

393396
// Propagate the extern crate info to dependencies.
394397
extern_crate.direct = false;
395-
for &dep_cnum in cmeta.cnum_map.borrow().iter() {
398+
for &dep_cnum in cmeta.dependencies.borrow().iter() {
396399
self.update_extern_crate(dep_cnum, extern_crate, visited);
397400
}
398401
}
@@ -1040,7 +1043,7 @@ impl<'a> CrateLoader<'a> {
10401043
}
10411044

10421045
info!("injecting a dep from {} to {}", cnum, krate);
1043-
data.cnum_map.borrow_mut().push(krate);
1046+
data.dependencies.borrow_mut().push(krate);
10441047
});
10451048
}
10461049
}

src/librustc_metadata/cstore.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ pub struct CrateMetadata {
6464
pub extern_crate: Lock<Option<ExternCrate>>,
6565

6666
pub blob: MetadataBlob,
67-
pub cnum_map: Lock<CrateNumMap>,
67+
pub cnum_map: CrateNumMap,
6868
pub cnum: CrateNum,
69+
pub dependencies: Lock<Vec<CrateNum>>,
6970
pub codemap_import_info: RwLock<Vec<ImportedFileMap>>,
7071
pub attribute_cache: Lock<[Vec<Option<Lrc<[ast::Attribute]>>>; 2]>,
7172

@@ -144,7 +145,7 @@ impl CStore {
144145
}
145146

146147
let data = self.get_crate_data(krate);
147-
for &dep in data.cnum_map.borrow().iter() {
148+
for &dep in data.dependencies.borrow().iter() {
148149
if dep != krate {
149150
self.push_dependencies_in_postorder(ordering, dep);
150151
}

src/librustc_metadata/decoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {
246246
if cnum == LOCAL_CRATE {
247247
self.cdata().cnum
248248
} else {
249-
self.cdata().cnum_map.borrow()[cnum]
249+
self.cdata().cnum_map[cnum]
250250
}
251251
}
252252
}
@@ -932,7 +932,7 @@ impl<'a, 'tcx> CrateMetadata {
932932
// Translate a DefId from the current compilation environment to a DefId
933933
// for an external crate.
934934
fn reverse_translate_def_id(&self, did: DefId) -> Option<DefId> {
935-
for (local, &global) in self.cnum_map.borrow().iter_enumerated() {
935+
for (local, &global) in self.cnum_map.iter_enumerated() {
936936
if global == did.krate {
937937
return Some(DefId {
938938
krate: local,
@@ -1007,7 +1007,7 @@ impl<'a, 'tcx> CrateMetadata {
10071007
.enumerate()
10081008
.flat_map(|(i, link)| {
10091009
let cnum = CrateNum::new(i + 1);
1010-
link.map(|link| (self.cnum_map.borrow()[cnum], link))
1010+
link.map(|link| (self.cnum_map[cnum], link))
10111011
})
10121012
.collect()
10131013
}

0 commit comments

Comments
 (0)