I'm curious why Swift doesn't use optimal (from a size perspective) ordering of stored properties, for non-frozen structs (and similar)?
As far as I can tell there's nothing in the ABI that prevents it. You can already do this through manual re-ordering of the declarations, so why not have the compiler do it automatically?
For example, currently the following struct has an actual size (stride) of 24 bytes, but by simply ordering the stored properties better that can be reduced to the optimal 16.
struct SadPanda {
let a: Int32
let b: Int64
let c: Int32
}
Is there a way to explicitly opt into this behaviour, at least? Some kind of @minimisePadding
decorator or somesuch?
Note that I'm not talking about misaligning any of the fields (away from their natural alignment), as e.g. the packed
attribute does in C. Merely reordering things. Though if anyone knows of an equivalent to __attribute__((packed))
for Swift, I'd like to know about that too.