Skip to content

Commit 8e8939b

Browse files
committed
Auto merge of #78489 - bugadani:array, r=estebank
Minor cleanup around incremental compilation * Remove some short lived vectors * Fix some typos * Avoid some reallocations
2 parents d662f80 + a8803d3 commit 8e8939b

File tree

5 files changed

+39
-44
lines changed

5 files changed

+39
-44
lines changed

compiler/rustc_graphviz/src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -653,13 +653,13 @@ where
653653
writeln!(w, r#" edge[{}];"#, content_attrs_str)?;
654654
}
655655

656+
let mut text = Vec::new();
656657
for n in g.nodes().iter() {
657658
write!(w, " ")?;
658659
let id = g.node_id(n);
659660

660661
let escaped = &g.node_label(n).to_dot_string();
661662

662-
let mut text = Vec::new();
663663
write!(text, "{}", id.as_slice()).unwrap();
664664

665665
if !options.contains(&RenderOption::NoNodeLabels) {
@@ -677,6 +677,8 @@ where
677677

678678
writeln!(text, ";").unwrap();
679679
w.write_all(&text[..])?;
680+
681+
text.clear();
680682
}
681683

682684
for e in g.edges().iter() {
@@ -687,7 +689,6 @@ where
687689
let source_id = g.node_id(&source);
688690
let target_id = g.node_id(&target);
689691

690-
let mut text = Vec::new();
691692
write!(text, "{} -> {}", source_id.as_slice(), target_id.as_slice()).unwrap();
692693

693694
if !options.contains(&RenderOption::NoEdgeLabels) {
@@ -701,6 +702,8 @@ where
701702

702703
writeln!(text, ";").unwrap();
703704
w.write_all(&text[..])?;
705+
706+
text.clear();
704707
}
705708

706709
writeln!(w, "}}")

compiler/rustc_incremental/src/assert_module_sources.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@ impl AssertModuleSource<'tcx> {
111111
(&user_path[..], None)
112112
};
113113

114-
let mut cgu_path_components = user_path.split('-').collect::<Vec<_>>();
114+
let mut iter = user_path.split('-');
115115

116116
// Remove the crate name
117-
assert_eq!(cgu_path_components.remove(0), crate_name);
117+
assert_eq!(iter.next().unwrap(), crate_name);
118+
119+
let cgu_path_components = iter.collect::<Vec<_>>();
118120

119121
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(self.tcx);
120122
let cgu_name =

compiler/rustc_incremental/src/persist/dirty_clean.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub fn check_dirty_clean_annotations(tcx: TyCtxt<'_>) {
160160

161161
let mut all_attrs = FindAllAttrs {
162162
tcx,
163-
attr_names: vec![sym::rustc_dirty, sym::rustc_clean],
163+
attr_names: &[sym::rustc_dirty, sym::rustc_clean],
164164
found_attrs: vec![],
165165
};
166166
intravisit::walk_crate(&mut all_attrs, krate);
@@ -299,7 +299,7 @@ impl DirtyCleanVisitor<'tcx> {
299299

300300
// Represents a Trait Declaration
301301
// FIXME(michaelwoerister): trait declaration is buggy because sometimes some of
302-
// the depnodes don't exist (because they legitametely didn't need to be
302+
// the depnodes don't exist (because they legitimately didn't need to be
303303
// calculated)
304304
//
305305
// michaelwoerister and vitiral came up with a possible solution,
@@ -512,17 +512,17 @@ fn expect_associated_value(tcx: TyCtxt<'_>, item: &NestedMetaItem) -> Symbol {
512512
}
513513

514514
// A visitor that collects all #[rustc_dirty]/#[rustc_clean] attributes from
515-
// the HIR. It is used to verfiy that we really ran checks for all annotated
515+
// the HIR. It is used to verify that we really ran checks for all annotated
516516
// nodes.
517-
pub struct FindAllAttrs<'tcx> {
517+
pub struct FindAllAttrs<'a, 'tcx> {
518518
tcx: TyCtxt<'tcx>,
519-
attr_names: Vec<Symbol>,
519+
attr_names: &'a [Symbol],
520520
found_attrs: Vec<&'tcx Attribute>,
521521
}
522522

523-
impl FindAllAttrs<'tcx> {
523+
impl FindAllAttrs<'_, 'tcx> {
524524
fn is_active_attr(&mut self, attr: &Attribute) -> bool {
525-
for attr_name in &self.attr_names {
525+
for attr_name in self.attr_names {
526526
if self.tcx.sess.check_name(attr, *attr_name) && check_config(self.tcx, attr) {
527527
return true;
528528
}
@@ -543,7 +543,7 @@ impl FindAllAttrs<'tcx> {
543543
}
544544
}
545545

546-
impl intravisit::Visitor<'tcx> for FindAllAttrs<'tcx> {
546+
impl intravisit::Visitor<'tcx> for FindAllAttrs<'_, 'tcx> {
547547
type Map = Map<'tcx>;
548548

549549
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {

compiler/rustc_incremental/src/persist/fs.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,6 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
765765

766766
// Now garbage collect the valid session directories.
767767
let mut deletion_candidates = vec![];
768-
let mut definitely_delete = vec![];
769768

770769
for (lock_file_name, directory_name) in &lock_file_to_session_dir {
771770
debug!("garbage_collect_session_directories() - inspecting: {}", directory_name);
@@ -842,8 +841,11 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
842841
successfully acquired lock"
843842
);
844843

845-
// Note that we are holding on to the lock
846-
definitely_delete.push((crate_directory.join(directory_name), Some(lock)));
844+
delete_old(sess, &crate_directory.join(directory_name));
845+
846+
// Let's make it explicit that the file lock is released at this point,
847+
// or rather, that we held on to it until here
848+
mem::drop(lock);
847849
}
848850
Err(_) => {
849851
debug!(
@@ -880,26 +882,21 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
880882
mem::drop(lock);
881883
}
882884

883-
for (path, lock) in definitely_delete {
884-
debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
885+
Ok(())
886+
}
885887

886-
if let Err(err) = safe_remove_dir_all(&path) {
887-
sess.warn(&format!(
888-
"Failed to garbage collect incremental \
889-
compilation session directory `{}`: {}",
890-
path.display(),
891-
err
892-
));
893-
} else {
894-
delete_session_dir_lock_file(sess, &lock_file_path(&path));
895-
}
888+
fn delete_old(sess: &Session, path: &Path) {
889+
debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
896890

897-
// Let's make it explicit that the file lock is released at this point,
898-
// or rather, that we held on to it until here
899-
mem::drop(lock);
891+
if let Err(err) = safe_remove_dir_all(&path) {
892+
sess.warn(&format!(
893+
"Failed to garbage collect incremental compilation session directory `{}`: {}",
894+
path.display(),
895+
err
896+
));
897+
} else {
898+
delete_session_dir_lock_file(sess, &lock_file_path(&path));
900899
}
901-
902-
Ok(())
903900
}
904901

905902
fn all_except_most_recent(

compiler/rustc_incremental/src/persist/save.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ fn encode_dep_graph(tcx: TyCtxt<'_>, encoder: &mut Encoder) {
153153
let total_node_count = serialized_graph.nodes.len();
154154
let total_edge_count = serialized_graph.edge_list_data.len();
155155

156-
let mut counts: FxHashMap<_, Stat> = FxHashMap::default();
156+
let mut counts: FxHashMap<_, Stat> =
157+
FxHashMap::with_capacity_and_hasher(total_node_count, Default::default());
157158

158159
for (i, &node) in serialized_graph.nodes.iter_enumerated() {
159160
let stat = counts.entry(node.kind).or_insert(Stat {
@@ -170,14 +171,6 @@ fn encode_dep_graph(tcx: TyCtxt<'_>, encoder: &mut Encoder) {
170171
let mut counts: Vec<_> = counts.values().cloned().collect();
171172
counts.sort_by_key(|s| -(s.node_counter as i64));
172173

173-
let percentage_of_all_nodes: Vec<f64> = counts
174-
.iter()
175-
.map(|s| (100.0 * (s.node_counter as f64)) / (total_node_count as f64))
176-
.collect();
177-
178-
let average_edges_per_kind: Vec<f64> =
179-
counts.iter().map(|s| (s.edge_counter as f64) / (s.node_counter as f64)).collect();
180-
181174
println!("[incremental]");
182175
println!("[incremental] DepGraph Statistics");
183176

@@ -207,13 +200,13 @@ fn encode_dep_graph(tcx: TyCtxt<'_>, encoder: &mut Encoder) {
207200
|------------------|"
208201
);
209202

210-
for (i, stat) in counts.iter().enumerate() {
203+
for stat in counts.iter() {
211204
println!(
212205
"[incremental] {:<36}|{:>16.1}% |{:>12} |{:>17.1} |",
213206
format!("{:?}", stat.kind),
214-
percentage_of_all_nodes[i],
207+
(100.0 * (stat.node_counter as f64)) / (total_node_count as f64), // percentage of all nodes
215208
stat.node_counter,
216-
average_edges_per_kind[i]
209+
(stat.edge_counter as f64) / (stat.node_counter as f64), // average edges per kind
217210
);
218211
}
219212

0 commit comments

Comments
 (0)