Skip to content

Commit 39e9dae

Browse files
committed
Simplify the detection of the largest variant
Since we do not care much about the case where we can use the niche, but that the fields cannot be re-ordered
1 parent 3ff98d9 commit 39e9dae

File tree

1 file changed

+31
-51
lines changed

1 file changed

+31
-51
lines changed

src/librustc_middle/ty/layout.rs

+31-51
Original file line numberDiff line numberDiff line change
@@ -905,58 +905,38 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
905905
// The size computations below assume that the padding is minimum.
906906
// This is the case when fields are re-ordered.
907907
let struct_reordering_opt = !def.repr.inhibit_struct_field_reordering_opt();
908-
if struct_reordering_opt {
909-
let mut extend_niche_range = |d| {
910-
niche_variants =
911-
*niche_variants.start().min(&d)..=*niche_variants.end().max(&d);
912-
};
913908

914-
// Find the largest and second largest variant.
915-
for (v, fields) in variants.iter_enumerated() {
916-
if absent(fields) {
917-
continue;
918-
}
919-
let mut size = Size::ZERO;
920-
for &f in fields {
921-
align = align.max(f.align);
922-
size += f.size;
923-
}
924-
if size > max_size {
925-
second_max_size = max_size;
926-
max_size = size;
927-
if let Some(d) = dataful_variant {
928-
extend_niche_range(d);
929-
}
930-
dataful_variant = Some(v);
931-
} else if size == max_size {
932-
if let Some(d) = dataful_variant {
933-
extend_niche_range(d);
934-
}
935-
dataful_variant = None;
936-
extend_niche_range(v);
937-
} else {
938-
second_max_size = second_max_size.max(size);
939-
extend_niche_range(v);
940-
}
909+
let mut extend_niche_range = |d| {
910+
niche_variants =
911+
*niche_variants.start().min(&d)..=*niche_variants.end().max(&d);
912+
};
913+
914+
// Find the largest and second largest variant.
915+
for (v, fields) in variants.iter_enumerated() {
916+
if absent(fields) {
917+
continue;
941918
}
942-
} else {
943-
// Find one non-ZST variant.
944-
'variants: for (v, fields) in variants.iter_enumerated() {
945-
if absent(fields) {
946-
continue 'variants;
919+
let mut size = Size::ZERO;
920+
for &f in fields {
921+
align = align.max(f.align);
922+
size += f.size;
923+
}
924+
if size > max_size {
925+
second_max_size = max_size;
926+
max_size = size;
927+
if let Some(d) = dataful_variant {
928+
extend_niche_range(d);
947929
}
948-
for f in fields {
949-
if !f.is_zst() {
950-
if dataful_variant.is_none() {
951-
dataful_variant = Some(v);
952-
continue 'variants;
953-
} else {
954-
dataful_variant = None;
955-
break 'variants;
956-
}
957-
}
958-
niche_variants = *niche_variants.start().min(&v)..=v
930+
dataful_variant = Some(v);
931+
} else if size == max_size {
932+
if let Some(d) = dataful_variant {
933+
extend_niche_range(d);
959934
}
935+
dataful_variant = None;
936+
extend_niche_range(v);
937+
} else {
938+
second_max_size = second_max_size.max(size);
939+
extend_niche_range(v);
960940
}
961941
}
962942

@@ -992,14 +972,15 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
992972
{
993973
return None;
994974
}
975+
} else if second_max_size > Size::ZERO {
976+
return None;
995977
}
996978
Some((field_index, niche, niche.reserve(self, count)?))
997979
});
998980

999981
if let Some((field_index, niche, (niche_start, niche_scalar))) =
1000982
niche_candidate
1001983
{
1002-
let mut align = dl.aggregate_align;
1003984
let prefix = niche.offset + niche.scalar.value.size(dl);
1004985
let st = variants
1005986
.iter_enumerated()
@@ -1019,8 +1000,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
10191000
)?;
10201001
st.variants = Variants::Single { index: j };
10211002

1022-
align = align.max(st.align);
1023-
1003+
debug_assert_eq!(align, align.max(st.align));
10241004
Ok(st)
10251005
})
10261006
.collect::<Result<IndexVec<VariantIdx, _>, _>>()?;

0 commit comments

Comments
 (0)