From ff296f4d7e5e875a214395b1a8b6bbb12dd5df6e Mon Sep 17 00:00:00 2001 From: Martin Zacho Date: Mon, 13 Jan 2025 22:24:15 +0100 Subject: [PATCH] Add cfg(debug_assertions) to CurrentDepGraph debug fields --- .../rustc_query_system/src/dep_graph/graph.rs | 22 ++++++++++++++----- .../src/dep_graph/serialized.rs | 12 +++++----- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_query_system/src/dep_graph/graph.rs b/compiler/rustc_query_system/src/dep_graph/graph.rs index 4b47ce8389c3d..438ad84d3b95c 100644 --- a/compiler/rustc_query_system/src/dep_graph/graph.rs +++ b/compiler/rustc_query_system/src/dep_graph/graph.rs @@ -11,7 +11,9 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::profiling::{QueryInvocationId, SelfProfilerRef}; use rustc_data_structures::sharded::{self, Sharded}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; -use rustc_data_structures::sync::{AtomicU32, AtomicU64, Lock, Lrc}; +#[cfg(debug_assertions)] +use rustc_data_structures::sync::AtomicU64; +use rustc_data_structures::sync::{AtomicU32, Lock, Lrc}; use rustc_data_structures::unord::UnordMap; use rustc_index::IndexVec; use rustc_macros::{Decodable, Encodable}; @@ -484,9 +486,8 @@ impl DepGraph { }; let task_deps = &mut *task_deps; - if cfg!(debug_assertions) { - data.current.total_read_count.fetch_add(1, Ordering::Relaxed); - } + #[cfg(debug_assertions)] + data.current.total_read_count.fetch_add(1, Ordering::Relaxed); // As long as we only have a low number of reads we can avoid doing a hash // insert and potentially allocating/reallocating the hashmap @@ -514,7 +515,8 @@ impl DepGraph { } } } - } else if cfg!(debug_assertions) { + } else { + #[cfg(debug_assertions)] data.current.total_duplicate_read_count.fetch_add(1, Ordering::Relaxed); } }) @@ -962,10 +964,13 @@ impl DepGraph { pub fn print_incremental_info(&self) { if let Some(data) = &self.data { + #[cfg(debug_assertions)] data.current.encoder.print_incremental_info( data.current.total_read_count.load(Ordering::Relaxed), data.current.total_duplicate_read_count.load(Ordering::Relaxed), - ) + ); + #[cfg(not(debug_assertions))] + data.current.encoder.print_incremental_info(0, 0) } } @@ -1082,7 +1087,10 @@ pub(super) struct CurrentDepGraph { /// These are simple counters that are for profiling and /// debugging and only active with `debug_assertions`. + #[cfg(debug_assertions)] total_read_count: AtomicU64, + + #[cfg(debug_assertions)] total_duplicate_read_count: AtomicU64, } @@ -1135,7 +1143,9 @@ impl CurrentDepGraph { forbidden_edge, #[cfg(debug_assertions)] fingerprints: Lock::new(IndexVec::from_elem_n(None, new_node_count_estimate)), + #[cfg(debug_assertions)] total_read_count: AtomicU64::new(0), + #[cfg(debug_assertions)] total_duplicate_read_count: AtomicU64::new(0), } } diff --git a/compiler/rustc_query_system/src/dep_graph/serialized.rs b/compiler/rustc_query_system/src/dep_graph/serialized.rs index a4fb0a5b07220..66183012db998 100644 --- a/compiler/rustc_query_system/src/dep_graph/serialized.rs +++ b/compiler/rustc_query_system/src/dep_graph/serialized.rs @@ -462,7 +462,6 @@ impl NodeInfo { } struct Stat { - kind: DepKind, node_counter: u64, edge_counter: u64, } @@ -524,8 +523,7 @@ impl EncoderState { // Outline the stats code as it's typically disabled and cold. outline(move || { - let stat = - stats.entry(kind).or_insert(Stat { kind, node_counter: 0, edge_counter: 0 }); + let stat = stats.entry(kind).or_insert(Stat { node_counter: 0, edge_counter: 0 }); stat.node_counter += 1; stat.edge_counter += edge_count as u64; }); @@ -651,8 +649,8 @@ impl GraphEncoder { let mut status = self.status.lock(); let status = status.as_mut().unwrap(); if let Some(record_stats) = &status.stats { - let mut stats: Vec<_> = record_stats.values().collect(); - stats.sort_by_key(|s| -(s.node_counter as i64)); + let mut stats: Vec<_> = record_stats.iter().collect(); + stats.sort_by_key(|(_, s)| -(s.node_counter as i64)); const SEPARATOR: &str = "[incremental] --------------------------------\ ----------------------------------------------\ @@ -677,14 +675,14 @@ impl GraphEncoder { ); eprintln!("{SEPARATOR}"); - for stat in stats { + for (kind, stat) in stats { let node_kind_ratio = (100.0 * (stat.node_counter as f64)) / (status.total_node_count as f64); let node_kind_avg_edges = (stat.edge_counter as f64) / (stat.node_counter as f64); eprintln!( "[incremental] {:<36}|{:>16.1}% |{:>12} |{:>17.1} |", - format!("{:?}", stat.kind), + format!("{:?}", kind), node_kind_ratio, stat.node_counter, node_kind_avg_edges,