From 931a32b028a6f516b8d0415c27610f848cb763f8 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 5 Sep 2023 08:41:33 +0000 Subject: [PATCH] WIP: Perf test using a `RwLock` unconditionally --- compiler/rustc_interface/src/queries.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 30 ++++++++++++++-------- compiler/rustc_query_system/src/ich/hcx.rs | 2 +- compiler/rustc_resolve/src/lib.rs | 2 +- compiler/rustc_session/src/cstore.rs | 2 +- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 2db7aa0e36710..c49380e16958b 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -197,7 +197,7 @@ impl<'tcx> Queries<'tcx> { self.codegen_backend().metadata_loader(), stable_crate_id, )) as _); - let definitions = RwLock::new(Definitions::new(stable_crate_id)); + let definitions = std::sync::RwLock::new(Definitions::new(stable_crate_id)); let source_span = AppendOnlyIndexVec::new(); let _id = source_span.push(krate.spans.inner_span); debug_assert_eq!(_id, CRATE_DEF_ID); diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index e2f9e299566ae..8903c4d1c4d37 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -878,7 +878,12 @@ impl<'tcx> TyCtxt<'tcx> { // If this is a DefPathHash from the local crate, we can look up the // DefId in the tcx's `Definitions`. if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) { - self.untracked.definitions.read().local_def_path_hash_to_def_id(hash, err).to_def_id() + self.untracked + .definitions + .read() + .unwrap() + .local_def_path_hash_to_def_id(hash, err) + .to_def_id() } else { // If this is a DefPathHash from an upstream crate, let the CrateStore map // it to a DefId. @@ -938,7 +943,7 @@ impl<'tcx> TyCtxtAt<'tcx> { // This is fine because: // - those queries are `eval_always` so we won't miss their result changing; // - this write will have happened before these queries are called. - let key = self.untracked.definitions.write().create_def(parent, data); + let key = self.untracked.definitions.write().unwrap().create_def(parent, data); let feed = TyCtxtFeed { tcx: self.tcx, key }; feed.def_span(self.span); @@ -958,14 +963,14 @@ impl<'tcx> TyCtxt<'tcx> { // Recompute the number of definitions each time, because our caller may be creating // new ones. - while i < { definitions.read().num_definitions() } { + while i < { definitions.read().unwrap().num_definitions() } { let local_def_index = rustc_span::def_id::DefIndex::from_usize(i); yield LocalDefId { local_def_index }; i += 1; } // Leak a read lock once we finish iterating on definitions, to prevent adding new ones. - definitions.leak(); + std::mem::forget(definitions.read()); }) } @@ -976,8 +981,9 @@ impl<'tcx> TyCtxt<'tcx> { // Leak a read lock once we start iterating on definitions, to prevent adding new ones // while iterating. If some query needs to add definitions, it should be `ensure`d above. - let definitions = self.untracked.definitions.leak(); - definitions.def_path_table() + std::mem::forget(self.untracked.definitions.read()); + let definitions = self.untracked.definitions.read().unwrap(); + unsafe { &*(definitions.def_path_table() as *const rustc_hir::definitions::DefPathTable) } } pub fn def_path_hash_to_def_index_map( @@ -988,8 +994,12 @@ impl<'tcx> TyCtxt<'tcx> { self.ensure().hir_crate(()); // Leak a read lock once we start iterating on definitions, to prevent adding new ones // while iterating. If some query needs to add definitions, it should be `ensure`d above. - let definitions = self.untracked.definitions.leak(); - definitions.def_path_hash_to_def_index_map() + std::mem::forget(self.untracked.definitions.read()); + let definitions = self.untracked.definitions.read().unwrap(); + unsafe { + &*(definitions.def_path_hash_to_def_index_map() + as *const rustc_hir::def_path_hash_map::DefPathHashMap) + } } /// Note that this is *untracked* and should only be used within the query @@ -1006,8 +1016,8 @@ impl<'tcx> TyCtxt<'tcx> { /// Note that this is *untracked* and should only be used within the query /// system if the result is otherwise tracked through queries #[inline] - pub fn definitions_untracked(self) -> ReadGuard<'tcx, Definitions> { - self.untracked.definitions.read() + pub fn definitions_untracked(self) -> std::sync::RwLockReadGuard<'tcx, Definitions> { + self.untracked.definitions.read().unwrap() } /// Note that this is *untracked* and should only be used within the query diff --git a/compiler/rustc_query_system/src/ich/hcx.rs b/compiler/rustc_query_system/src/ich/hcx.rs index 5593a15412fb7..bce5df6043477 100644 --- a/compiler/rustc_query_system/src/ich/hcx.rs +++ b/compiler/rustc_query_system/src/ich/hcx.rs @@ -96,7 +96,7 @@ impl<'a> StableHashingContext<'a> { #[inline] pub fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash { - self.untracked.definitions.read().def_path_hash(def_id) + self.untracked.definitions.read().unwrap().def_path_hash(def_id) } #[inline] diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index b757f42eaa026..689b33f412f57 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1207,7 +1207,7 @@ impl<'tcx> Resolver<'_, 'tcx> { ); // FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()` - let def_id = self.tcx.untracked().definitions.write().create_def(parent, data); + let def_id = self.tcx.untracked().definitions.write().unwrap().create_def(parent, data); // Create the definition. if expn_id != ExpnId::root() { diff --git a/compiler/rustc_session/src/cstore.rs b/compiler/rustc_session/src/cstore.rs index c53a355b533ea..4b8f998643d7b 100644 --- a/compiler/rustc_session/src/cstore.rs +++ b/compiler/rustc_session/src/cstore.rs @@ -261,5 +261,5 @@ pub struct Untracked { pub cstore: RwLock>, /// Reference span for definitions. pub source_span: AppendOnlyIndexVec, - pub definitions: RwLock, + pub definitions: std::sync::RwLock, }