From f3e6d882fec61ef290b9097f896cfbcf39f5b4bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Wed, 28 Oct 2020 16:42:28 +0100 Subject: [PATCH 1/5] Fix typos and replace static vector with slice --- .../rustc_incremental/src/persist/dirty_clean.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_incremental/src/persist/dirty_clean.rs b/compiler/rustc_incremental/src/persist/dirty_clean.rs index f0a1088555035..d55813f4cc5ad 100644 --- a/compiler/rustc_incremental/src/persist/dirty_clean.rs +++ b/compiler/rustc_incremental/src/persist/dirty_clean.rs @@ -160,7 +160,7 @@ pub fn check_dirty_clean_annotations(tcx: TyCtxt<'_>) { let mut all_attrs = FindAllAttrs { tcx, - attr_names: vec![sym::rustc_dirty, sym::rustc_clean], + attr_names: &[sym::rustc_dirty, sym::rustc_clean], found_attrs: vec![], }; intravisit::walk_crate(&mut all_attrs, krate); @@ -299,7 +299,7 @@ impl DirtyCleanVisitor<'tcx> { // Represents a Trait Declaration // FIXME(michaelwoerister): trait declaration is buggy because sometimes some of - // the depnodes don't exist (because they legitametely didn't need to be + // the depnodes don't exist (because they legitimately didn't need to be // calculated) // // michaelwoerister and vitiral came up with a possible solution, @@ -512,17 +512,17 @@ fn expect_associated_value(tcx: TyCtxt<'_>, item: &NestedMetaItem) -> Symbol { } // A visitor that collects all #[rustc_dirty]/#[rustc_clean] attributes from -// the HIR. It is used to verfiy that we really ran checks for all annotated +// the HIR. It is used to verify that we really ran checks for all annotated // nodes. -pub struct FindAllAttrs<'tcx> { +pub struct FindAllAttrs<'a, 'tcx> { tcx: TyCtxt<'tcx>, - attr_names: Vec, + attr_names: &'a [Symbol], found_attrs: Vec<&'tcx Attribute>, } -impl FindAllAttrs<'tcx> { +impl FindAllAttrs<'_, 'tcx> { fn is_active_attr(&mut self, attr: &Attribute) -> bool { - for attr_name in &self.attr_names { + for attr_name in self.attr_names { if self.tcx.sess.check_name(attr, *attr_name) && check_config(self.tcx, attr) { return true; } @@ -543,7 +543,7 @@ impl FindAllAttrs<'tcx> { } } -impl intravisit::Visitor<'tcx> for FindAllAttrs<'tcx> { +impl intravisit::Visitor<'tcx> for FindAllAttrs<'_, 'tcx> { type Map = Map<'tcx>; fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap { From a21f3a76a9f5c1f7087e9aec6102889cb24a0b77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Wed, 28 Oct 2020 16:51:41 +0100 Subject: [PATCH 2/5] Clean up encode_dep_graph --- compiler/rustc_incremental/src/persist/save.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_incremental/src/persist/save.rs b/compiler/rustc_incremental/src/persist/save.rs index c43d4ad4049c9..45cef479a4f43 100644 --- a/compiler/rustc_incremental/src/persist/save.rs +++ b/compiler/rustc_incremental/src/persist/save.rs @@ -153,7 +153,8 @@ fn encode_dep_graph(tcx: TyCtxt<'_>, encoder: &mut Encoder) { let total_node_count = serialized_graph.nodes.len(); let total_edge_count = serialized_graph.edge_list_data.len(); - let mut counts: FxHashMap<_, Stat> = FxHashMap::default(); + let mut counts: FxHashMap<_, Stat> = + FxHashMap::with_capacity_and_hasher(total_node_count, Default::default()); for (i, &node) in serialized_graph.nodes.iter_enumerated() { let stat = counts.entry(node.kind).or_insert(Stat { @@ -170,14 +171,6 @@ fn encode_dep_graph(tcx: TyCtxt<'_>, encoder: &mut Encoder) { let mut counts: Vec<_> = counts.values().cloned().collect(); counts.sort_by_key(|s| -(s.node_counter as i64)); - let percentage_of_all_nodes: Vec = counts - .iter() - .map(|s| (100.0 * (s.node_counter as f64)) / (total_node_count as f64)) - .collect(); - - let average_edges_per_kind: Vec = - counts.iter().map(|s| (s.edge_counter as f64) / (s.node_counter as f64)).collect(); - println!("[incremental]"); println!("[incremental] DepGraph Statistics"); @@ -207,13 +200,13 @@ fn encode_dep_graph(tcx: TyCtxt<'_>, encoder: &mut Encoder) { |------------------|" ); - for (i, stat) in counts.iter().enumerate() { + for stat in counts.iter() { println!( "[incremental] {:<36}|{:>16.1}% |{:>12} |{:>17.1} |", format!("{:?}", stat.kind), - percentage_of_all_nodes[i], + (100.0 * (stat.node_counter as f64)) / (total_node_count as f64), // percentage of all nodes stat.node_counter, - average_edges_per_kind[i] + (stat.edge_counter as f64) / (stat.node_counter as f64), // average edges per kind ); } From 5248b20d9ae02a39cb20448896fc663636f591fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Wed, 28 Oct 2020 17:04:44 +0100 Subject: [PATCH 3/5] Reuse memory --- compiler/rustc_graphviz/src/lib.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_graphviz/src/lib.rs b/compiler/rustc_graphviz/src/lib.rs index 76e33bed97f27..d332466160ce5 100644 --- a/compiler/rustc_graphviz/src/lib.rs +++ b/compiler/rustc_graphviz/src/lib.rs @@ -653,13 +653,13 @@ where writeln!(w, r#" edge[{}];"#, content_attrs_str)?; } + let mut text = Vec::new(); for n in g.nodes().iter() { write!(w, " ")?; let id = g.node_id(n); let escaped = &g.node_label(n).to_dot_string(); - let mut text = Vec::new(); write!(text, "{}", id.as_slice()).unwrap(); if !options.contains(&RenderOption::NoNodeLabels) { @@ -677,6 +677,8 @@ where writeln!(text, ";").unwrap(); w.write_all(&text[..])?; + + text.clear(); } for e in g.edges().iter() { @@ -687,7 +689,6 @@ where let source_id = g.node_id(&source); let target_id = g.node_id(&target); - let mut text = Vec::new(); write!(text, "{} -> {}", source_id.as_slice(), target_id.as_slice()).unwrap(); if !options.contains(&RenderOption::NoEdgeLabels) { @@ -701,6 +702,8 @@ where writeln!(text, ";").unwrap(); w.write_all(&text[..])?; + + text.clear(); } writeln!(w, "}}") From 2fa359814aab054f4b0c9c764a6d6a36ecf6cde8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Wed, 28 Oct 2020 17:05:49 +0100 Subject: [PATCH 4/5] Avoid reallocating cgu_path_components --- compiler/rustc_incremental/src/assert_module_sources.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_incremental/src/assert_module_sources.rs b/compiler/rustc_incremental/src/assert_module_sources.rs index 80448c01a262d..17d8ac9c88297 100644 --- a/compiler/rustc_incremental/src/assert_module_sources.rs +++ b/compiler/rustc_incremental/src/assert_module_sources.rs @@ -111,10 +111,12 @@ impl AssertModuleSource<'tcx> { (&user_path[..], None) }; - let mut cgu_path_components = user_path.split('-').collect::>(); + let mut iter = user_path.split('-'); // Remove the crate name - assert_eq!(cgu_path_components.remove(0), crate_name); + assert_eq!(iter.next().unwrap(), crate_name); + + let cgu_path_components = iter.collect::>(); let cgu_name_builder = &mut CodegenUnitNameBuilder::new(self.tcx); let cgu_name = From a8803d3c04b25928b5ec42b3147c8002de7ac64f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Wed, 28 Oct 2020 17:21:02 +0100 Subject: [PATCH 5/5] Delete files immediately, instead of collecting into vector --- compiler/rustc_incremental/src/persist/fs.rs | 37 +++++++++----------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_incremental/src/persist/fs.rs b/compiler/rustc_incremental/src/persist/fs.rs index 4926f726f3593..9fdf0a56d9de1 100644 --- a/compiler/rustc_incremental/src/persist/fs.rs +++ b/compiler/rustc_incremental/src/persist/fs.rs @@ -765,7 +765,6 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> { // Now garbage collect the valid session directories. let mut deletion_candidates = vec![]; - let mut definitely_delete = vec![]; for (lock_file_name, directory_name) in &lock_file_to_session_dir { debug!("garbage_collect_session_directories() - inspecting: {}", directory_name); @@ -842,8 +841,11 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> { successfully acquired lock" ); - // Note that we are holding on to the lock - definitely_delete.push((crate_directory.join(directory_name), Some(lock))); + delete_old(sess, &crate_directory.join(directory_name)); + + // Let's make it explicit that the file lock is released at this point, + // or rather, that we held on to it until here + mem::drop(lock); } Err(_) => { debug!( @@ -880,26 +882,21 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> { mem::drop(lock); } - for (path, lock) in definitely_delete { - debug!("garbage_collect_session_directories() - deleting `{}`", path.display()); + Ok(()) +} - if let Err(err) = safe_remove_dir_all(&path) { - sess.warn(&format!( - "Failed to garbage collect incremental \ - compilation session directory `{}`: {}", - path.display(), - err - )); - } else { - delete_session_dir_lock_file(sess, &lock_file_path(&path)); - } +fn delete_old(sess: &Session, path: &Path) { + debug!("garbage_collect_session_directories() - deleting `{}`", path.display()); - // Let's make it explicit that the file lock is released at this point, - // or rather, that we held on to it until here - mem::drop(lock); + if let Err(err) = safe_remove_dir_all(&path) { + sess.warn(&format!( + "Failed to garbage collect incremental compilation session directory `{}`: {}", + path.display(), + err + )); + } else { + delete_session_dir_lock_file(sess, &lock_file_path(&path)); } - - Ok(()) } fn all_except_most_recent(