Skip to content

Commit b40788e

Browse files
committed
Fix generator size regressions due to optimization
I tested the generator optimizations in rust-lang#60187 and rust-lang#61922 on the Fuchsia build, and noticed that some small generators (about 8% of the async fns in our build) increased in size slightly. This is because in rust-lang#60187 we split the fields into two groups, a "prefix" non-overlap region and an overlap region, and lay them out separately. This can introduce unnecessary padding bytes between the two groups. In every single case in the Fuchsia build, it was due to there being only a single variant being used in the overlap region. This means that we aren't doing any overlapping, period. So it's better to combine the two regions into one and lay out all the fields at once, which is what this change does.
1 parent 9d4ca87 commit b40788e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/librustc/ty/layout.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,27 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
13681368
}
13691369
}
13701370

1371+
// Count the number of variants in use. If only one of them, then it is
1372+
// impossible to overlap any locals in our layout. In this case it's
1373+
// always better to make the remaining locals ineligible, so we can
1374+
// lay them out with the other locals in the prefix and eliminate
1375+
// unnecessary padding bytes.
1376+
{
1377+
let mut used_variants = BitSet::new_empty(info.variant_fields.len());
1378+
for assignment in &assignments {
1379+
match assignment {
1380+
Assigned(idx) => { used_variants.insert(*idx); }
1381+
_ => {}
1382+
}
1383+
}
1384+
if used_variants.count() < 2 {
1385+
for assignment in assignments.iter_mut() {
1386+
*assignment = Ineligible(None);
1387+
}
1388+
ineligible_locals.insert_all();
1389+
}
1390+
}
1391+
13711392
// Write down the order of our locals that will be promoted to the prefix.
13721393
{
13731394
let mut idx = 0u32;

0 commit comments

Comments
 (0)