Skip to content

Commit 81006f6

Browse files
committed
Optimize slightly by avoiding to call Niche::reserve when not needed
1 parent 4d77d01 commit 81006f6

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

Diff for: src/librustc/ty/layout.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -907,14 +907,25 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
907907
let count = (niche_variants.end().as_u32()
908908
- niche_variants.start().as_u32()
909909
+ 1) as u128;
910-
if let Some((field_index, niche, (niche_start, niche_scalar))) = variants[i]
911-
.iter()
912-
.enumerate()
913-
.filter_map(|(i, &field)| {
914-
let niche = field.largest_niche.as_ref()?;
915-
Some((i, niche, niche.reserve(self, count)?))
910+
let mut niche_size = 0;
911+
if let Some((field_index, niche, niche_start, niche_scalar)) =
912+
variants[i].iter().enumerate().fold(None, |acc, (j, &field)| {
913+
let niche = match &field.largest_niche {
914+
Some(niche) => niche,
915+
_ => return acc,
916+
};
917+
let ns = niche.available(dl);
918+
if ns <= niche_size {
919+
return acc;
920+
}
921+
match niche.reserve(self, count) {
922+
Some(pair) => {
923+
niche_size = ns;
924+
Some((j, niche, pair.0, pair.1))
925+
}
926+
None => acc,
927+
}
916928
})
917-
.max_by_key(|(_, niche, _)| niche.available(dl))
918929
{
919930
let mut align = dl.aggregate_align;
920931
let st = variants

0 commit comments

Comments
 (0)