about summary refs log tree commit diff
path: root/compiler/rustc_session
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-03-12 06:48:30 +0000
committerbors <bors@rust-lang.org>2023-03-12 06:48:30 +0000
commit150cb381471533050751111e5faf1d9f05c02f77 (patch)
tree3180ee8fcea1444fdcfe2038f48bf3e06c661799 /compiler/rustc_session
parent501ad021b9a4fb2cd6a39e0302d22f169f6166b0 (diff)
parent9570023ce13d0b2fb3d5b79ce9db57e6d28c31d1 (diff)
downloadrust-150cb381471533050751111e5faf1d9f05c02f77.tar.gz
rust-150cb381471533050751111e5faf1d9f05c02f77.zip
Auto merge of #108794 - nnethercote:avoid-unnecessary-hashing, r=cjgillot
Avoid unnecessary hashing

I noticed some stable hashing being done in a non-incremental build. It turns out that some of this is necessary to compute the crate hash, but some of it is not. Removing the unnecessary hashing is a perf win.

r? `@cjgillot`
Diffstat (limited to 'compiler/rustc_session')
-rw-r--r--compiler/rustc_session/src/session.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs
index 2cc25e977a9..fdacf814dd6 100644
--- a/compiler/rustc_session/src/session.rs
+++ b/compiler/rustc_session/src/session.rs
@@ -224,6 +224,13 @@ pub struct PerfStats {
     pub normalize_projection_ty: AtomicUsize,
 }
 
+#[derive(PartialEq, Eq, PartialOrd, Ord)]
+pub enum MetadataKind {
+    None,
+    Uncompressed,
+    Compressed,
+}
+
 impl Session {
     pub fn miri_unleashed_feature(&self, span: Span, feature_gate: Option<Symbol>) {
         self.miri_unleashed_features.lock().push((span, feature_gate));
@@ -287,6 +294,38 @@ impl Session {
         self.crate_types.get().unwrap().as_slice()
     }
 
+    pub fn needs_crate_hash(&self) -> bool {
+        // Why is the crate hash needed for these configurations?
+        // - debug_assertions: for the "fingerprint the result" check in
+        //   `rustc_query_system::query::plumbing::execute_job`.
+        // - incremental: for query lookups.
+        // - needs_metadata: for putting into crate metadata.
+        // - instrument_coverage: for putting into coverage data (see
+        //   `hash_mir_source`).
+        cfg!(debug_assertions)
+            || self.opts.incremental.is_some()
+            || self.needs_metadata()
+            || self.instrument_coverage()
+    }
+
+    pub fn metadata_kind(&self) -> MetadataKind {
+        self.crate_types()
+            .iter()
+            .map(|ty| match *ty {
+                CrateType::Executable | CrateType::Staticlib | CrateType::Cdylib => {
+                    MetadataKind::None
+                }
+                CrateType::Rlib => MetadataKind::Uncompressed,
+                CrateType::Dylib | CrateType::ProcMacro => MetadataKind::Compressed,
+            })
+            .max()
+            .unwrap_or(MetadataKind::None)
+    }
+
+    pub fn needs_metadata(&self) -> bool {
+        self.metadata_kind() != MetadataKind::None
+    }
+
     pub fn init_crate_types(&self, crate_types: Vec<CrateType>) {
         self.crate_types.set(crate_types).expect("`crate_types` was initialized twice")
     }