Skip to content

Commit e292500

Browse files
committed
Improve the CGU merging algorithm.
1 parent b7951c8 commit e292500

File tree

1 file changed

+16
-19
lines changed
  • compiler/rustc_monomorphize/src/partitioning

1 file changed

+16
-19
lines changed

compiler/rustc_monomorphize/src/partitioning/merging.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub fn merge_codegen_units<'tcx>(
2020
// We want this merging to produce a deterministic ordering of codegen units
2121
// from the input.
2222
//
23-
// Due to basically how we've implemented the merging below (merge the two
24-
// smallest into each other) we're sure to start off with a deterministic
23+
// Due to basically how we've implemented the merging below (repeatedly
24+
// merging adjacent pairs of CGUs) we're sure to start off with a deterministic
2525
// order (sorted by name). This'll mean that if two cgus have the same size
2626
// the stable sort below will keep everything nice and deterministic.
2727
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
@@ -30,29 +30,26 @@ pub fn merge_codegen_units<'tcx>(
3030
let mut cgu_contents: FxHashMap<Symbol, Vec<Symbol>> =
3131
codegen_units.iter().map(|cgu| (cgu.name(), vec![cgu.name()])).collect();
3232

33-
// Merge the two smallest codegen units until the target size is reached.
33+
// Repeatedly merge cgu[n] into cgu[n-1].
3434
while codegen_units.len() > cx.target_cgu_count {
35-
// Sort small cgus to the back
35+
// njn: more comments about this.
36+
// Sort small cgus to the back. At this point
3637
codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
37-
let mut smallest = codegen_units.pop().unwrap();
38-
let second_smallest = codegen_units.last_mut().unwrap();
38+
let mut cgu_n = codegen_units.swap_remove(cx.target_cgu_count);
39+
let cgu_n_minus_1 = &mut codegen_units[cx.target_cgu_count - 1];
3940

40-
// Move the mono-items from `smallest` to `second_smallest`
41-
second_smallest.modify_size_estimate(smallest.size_estimate());
42-
for (k, v) in smallest.items_mut().drain() {
43-
second_smallest.items_mut().insert(k, v);
41+
// Move the mono-items from `cgu_n` to `cgu_n_minus_1`
42+
cgu_n_minus_1.modify_size_estimate(cgu_n.size_estimate());
43+
for (k, v) in cgu_n.items_mut().drain() {
44+
cgu_n_minus_1.items_mut().insert(k, v);
4445
}
4546

46-
// Record that `second_smallest` now contains all the stuff that was in
47-
// `smallest` before.
48-
let mut consumed_cgu_names = cgu_contents.remove(&smallest.name()).unwrap();
49-
cgu_contents.get_mut(&second_smallest.name()).unwrap().append(&mut consumed_cgu_names);
47+
// Record that `cgu_n_minus_1` now contains all the stuff that was in
48+
// `cgu_n` before.
49+
let mut consumed_cgu_names = cgu_contents.remove(&cgu_n.name()).unwrap();
50+
cgu_contents.get_mut(&cgu_n_minus_1.name()).unwrap().append(&mut consumed_cgu_names);
5051

51-
debug!(
52-
"CodegenUnit {} merged into CodegenUnit {}",
53-
smallest.name(),
54-
second_smallest.name()
55-
);
52+
debug!("CodegenUnit {} merged into CodegenUnit {}", cgu_n.name(), cgu_n_minus_1.name());
5653
}
5754

5855
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);

0 commit comments

Comments
 (0)