4 ms·
Actually, the C (and I believe C++) language standards both allow reorganizing struct layouts, except for the first member which has to be in first place (for t
by nhaehnle 9y ago
Actually, the C (and I believe C++) language standards both allow reorganizing struct layouts, except for the first member which has to be in first place (for tagged unions).
The struct layout is fixed by platform-specific ABI documents, which must specify the layout precisely so that structs can be passed between separately compiled code, e.g. between applications and dynamically linked libraries.
Changing ABIs is virtually impossible, so...
It would be nice to see opt-in optimizations of struct layouts, e.g. by annotating structs with a packing-style attribute. Though the best gains would probably be obtained from profile-guided optimizations for cache line optimizations, and those can only really be done with link-time optimizations for structs that are not passed outside the linker's scope.
- gpderetta 9y agoThe compiler is free to change the layout (even from AOS to SOA) as long as it can prove that the program can't tell the difference. Of course this is hard to prove in practice even with whole program optimization.
- Sean1708 9y ago> It would be nice to see opt-in optimizations of struct layouts, e.g. by annotating structs with a packing-style attribute. Like __attribute__((packed))[0]? [0]: https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Type-Attributes.html https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Type-Attributes.h...
- fanf2 9y agoC99 TC3 §6.7.2.1p13 "structure and union specifiers": "Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared."
- zurn 9y agoThe as-if rule allows compilers to perform this optimization, provided they elide the optimization for those structs that have addresses of members taken/used, though. This requires whole program analysis of course (as in clang / gcc "LTO" compilation mode). And of course there could be language extensions to loosen this rule further on a per-struct basis.