Skip to content

Commit efbe288

Browse files
committed
XXX: better merging, plus splitting fixes
1 parent dc8ab7e commit efbe288

File tree

1 file changed

+24
-25
lines changed
  • compiler/rustc_monomorphize/src/partitioning

1 file changed

+24
-25
lines changed

compiler/rustc_monomorphize/src/partitioning/default.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,16 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
104104
// same set). We want this merging to produce a deterministic ordering
105105
// of codegen units from the input.
106106
//
107-
// Due to basically how we've implemented the merging below (merge the
108-
// two smallest into each other) we're sure to start off with a
109-
// deterministic order (sorted by name). This'll mean that if two cgus
110-
// have the same size the stable sort below will keep everything nice
111-
// and deterministic.
107+
// Due to basically how we've implemented the merging below (repeatedly
108+
// merging adjacent pairs of CGUs) we're sure to start off with a
109+
// deterministic deterministic order (sorted by name). This'll mean that
110+
// if two cgus have the same size the stable sort below will keep
111+
// everything nice and deterministic.
112112
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
113113

114114
//---------------------------------------------------------------------------
115115
// njn: split big CGUs if necessary
116-
if false && codegen_units.len() > cx.target_cgu_count {
116+
if codegen_units.len() > cx.target_cgu_count {
117117
// njn: type ann?
118118
let total_size: usize = codegen_units.iter().map(|cgu| cgu.size_estimate()).sum();
119119
let target_size = total_size / cx.target_cgu_count;
@@ -123,6 +123,7 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
123123
// njn: make it a for loop?
124124
// njn: explain all this
125125
let mut i = 0;
126+
let mut j = 0; // njn: explain
126127
let n = codegen_units.len();
127128
while i < n {
128129
let old_cgu = &mut codegen_units[i];
@@ -134,7 +135,7 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
134135
// times
135136

136137
let mut new_name = old_cgu.name().to_string();
137-
new_name += "-split";
138+
new_name += &format!("-split{}", j);
138139
let mut new_cgu = CodegenUnit::new(Symbol::intern(&new_name));
139140
new_cgu.create_size_estimate(cx.tcx); // initially zero
140141

@@ -164,9 +165,11 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
164165

165166
codegen_units.push(new_cgu);
166167
// njn: explain lack of `i += 1`;
168+
j += 1;
167169
} else {
168170
// njn: explain this
169171
i += 1;
172+
j = 0;
170173
}
171174
}
172175
}
@@ -176,30 +179,26 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
176179
let mut cgu_contents: FxHashMap<Symbol, Vec<Symbol>> =
177180
codegen_units.iter().map(|cgu| (cgu.name(), vec![cgu.name()])).collect();
178181

179-
// Merge the two smallest codegen units until the target size is
180-
// reached.
182+
// Repeatedly merge cgu[n] into cgu[n-1].
181183
while codegen_units.len() > cx.target_cgu_count {
182-
// Sort small cgus to the back
184+
// njn: more comments about this.
185+
// Sort small cgus to the back. At this point... njn: more
183186
codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
184-
let mut smallest = codegen_units.pop().unwrap();
185-
let second_smallest = codegen_units.last_mut().unwrap();
187+
let mut cgu_n = codegen_units.swap_remove(cx.target_cgu_count);
188+
let cgu_n_minus_1 = &mut codegen_units[cx.target_cgu_count - 1];
186189

187-
// Move the mono-items from `smallest` to `second_smallest`
188-
second_smallest.increase_size_estimate(smallest.size_estimate());
189-
for (k, v) in smallest.items_mut().drain() {
190-
second_smallest.items_mut().insert(k, v);
190+
// Move the mono-items from `cgu_n` to `cgu_n_minus_1`
191+
cgu_n_minus_1.increase_size_estimate(cgu_n.size_estimate());
192+
for (k, v) in cgu_n.items_mut().drain() {
193+
cgu_n_minus_1.items_mut().insert(k, v);
191194
}
192195

193-
// Record that `second_smallest` now contains all the stuff that was
194-
// in `smallest` before.
195-
let mut consumed_cgu_names = cgu_contents.remove(&smallest.name()).unwrap();
196-
cgu_contents.get_mut(&second_smallest.name()).unwrap().append(&mut consumed_cgu_names);
196+
// Record that `cgu_n_minus_1` now contains all the stuff that was in
197+
// `cgu_n` before.
198+
let mut consumed_cgu_names = cgu_contents.remove(&cgu_n.name()).unwrap();
199+
cgu_contents.get_mut(&cgu_n_minus_1.name()).unwrap().append(&mut consumed_cgu_names);
197200

198-
debug!(
199-
"CodegenUnit {} merged into CodegenUnit {}",
200-
smallest.name(),
201-
second_smallest.name()
202-
);
201+
debug!("CodegenUnit {} merged into CodegenUnit {}", cgu_n.name(), cgu_n_minus_1.name());
203202
}
204203

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

0 commit comments

Comments
 (0)