Skip to content

Commit 931a32b

Browse files
committed
WIP: Perf test using a RwLock unconditionally
1 parent 8cfaf70 commit 931a32b

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

compiler/rustc_interface/src/queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl<'tcx> Queries<'tcx> {
197197
self.codegen_backend().metadata_loader(),
198198
stable_crate_id,
199199
)) as _);
200-
let definitions = RwLock::new(Definitions::new(stable_crate_id));
200+
let definitions = std::sync::RwLock::new(Definitions::new(stable_crate_id));
201201
let source_span = AppendOnlyIndexVec::new();
202202
let _id = source_span.push(krate.spans.inner_span);
203203
debug_assert_eq!(_id, CRATE_DEF_ID);

compiler/rustc_middle/src/ty/context.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,12 @@ impl<'tcx> TyCtxt<'tcx> {
878878
// If this is a DefPathHash from the local crate, we can look up the
879879
// DefId in the tcx's `Definitions`.
880880
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
881-
self.untracked.definitions.read().local_def_path_hash_to_def_id(hash, err).to_def_id()
881+
self.untracked
882+
.definitions
883+
.read()
884+
.unwrap()
885+
.local_def_path_hash_to_def_id(hash, err)
886+
.to_def_id()
882887
} else {
883888
// If this is a DefPathHash from an upstream crate, let the CrateStore map
884889
// it to a DefId.
@@ -938,7 +943,7 @@ impl<'tcx> TyCtxtAt<'tcx> {
938943
// This is fine because:
939944
// - those queries are `eval_always` so we won't miss their result changing;
940945
// - this write will have happened before these queries are called.
941-
let key = self.untracked.definitions.write().create_def(parent, data);
946+
let key = self.untracked.definitions.write().unwrap().create_def(parent, data);
942947

943948
let feed = TyCtxtFeed { tcx: self.tcx, key };
944949
feed.def_span(self.span);
@@ -958,14 +963,14 @@ impl<'tcx> TyCtxt<'tcx> {
958963

959964
// Recompute the number of definitions each time, because our caller may be creating
960965
// new ones.
961-
while i < { definitions.read().num_definitions() } {
966+
while i < { definitions.read().unwrap().num_definitions() } {
962967
let local_def_index = rustc_span::def_id::DefIndex::from_usize(i);
963968
yield LocalDefId { local_def_index };
964969
i += 1;
965970
}
966971

967972
// Leak a read lock once we finish iterating on definitions, to prevent adding new ones.
968-
definitions.leak();
973+
std::mem::forget(definitions.read());
969974
})
970975
}
971976

@@ -976,8 +981,9 @@ impl<'tcx> TyCtxt<'tcx> {
976981

977982
// Leak a read lock once we start iterating on definitions, to prevent adding new ones
978983
// while iterating. If some query needs to add definitions, it should be `ensure`d above.
979-
let definitions = self.untracked.definitions.leak();
980-
definitions.def_path_table()
984+
std::mem::forget(self.untracked.definitions.read());
985+
let definitions = self.untracked.definitions.read().unwrap();
986+
unsafe { &*(definitions.def_path_table() as *const rustc_hir::definitions::DefPathTable) }
981987
}
982988

983989
pub fn def_path_hash_to_def_index_map(
@@ -988,8 +994,12 @@ impl<'tcx> TyCtxt<'tcx> {
988994
self.ensure().hir_crate(());
989995
// Leak a read lock once we start iterating on definitions, to prevent adding new ones
990996
// while iterating. If some query needs to add definitions, it should be `ensure`d above.
991-
let definitions = self.untracked.definitions.leak();
992-
definitions.def_path_hash_to_def_index_map()
997+
std::mem::forget(self.untracked.definitions.read());
998+
let definitions = self.untracked.definitions.read().unwrap();
999+
unsafe {
1000+
&*(definitions.def_path_hash_to_def_index_map()
1001+
as *const rustc_hir::def_path_hash_map::DefPathHashMap)
1002+
}
9931003
}
9941004

9951005
/// Note that this is *untracked* and should only be used within the query
@@ -1006,8 +1016,8 @@ impl<'tcx> TyCtxt<'tcx> {
10061016
/// Note that this is *untracked* and should only be used within the query
10071017
/// system if the result is otherwise tracked through queries
10081018
#[inline]
1009-
pub fn definitions_untracked(self) -> ReadGuard<'tcx, Definitions> {
1010-
self.untracked.definitions.read()
1019+
pub fn definitions_untracked(self) -> std::sync::RwLockReadGuard<'tcx, Definitions> {
1020+
self.untracked.definitions.read().unwrap()
10111021
}
10121022

10131023
/// Note that this is *untracked* and should only be used within the query

compiler/rustc_query_system/src/ich/hcx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'a> StableHashingContext<'a> {
9696

9797
#[inline]
9898
pub fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash {
99-
self.untracked.definitions.read().def_path_hash(def_id)
99+
self.untracked.definitions.read().unwrap().def_path_hash(def_id)
100100
}
101101

102102
#[inline]

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ impl<'tcx> Resolver<'_, 'tcx> {
12071207
);
12081208

12091209
// FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()`
1210-
let def_id = self.tcx.untracked().definitions.write().create_def(parent, data);
1210+
let def_id = self.tcx.untracked().definitions.write().unwrap().create_def(parent, data);
12111211

12121212
// Create the definition.
12131213
if expn_id != ExpnId::root() {

compiler/rustc_session/src/cstore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,5 +261,5 @@ pub struct Untracked {
261261
pub cstore: RwLock<Box<CrateStoreDyn>>,
262262
/// Reference span for definitions.
263263
pub source_span: AppendOnlyIndexVec<LocalDefId, Span>,
264-
pub definitions: RwLock<Definitions>,
264+
pub definitions: std::sync::RwLock<Definitions>,
265265
}

0 commit comments

Comments
 (0)