Tail pad optimization, cache friendlyness and C++ interrop

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 10 00:30:43 PDT 2014


I was messing around with clang codegen and noticed that sometime 
it optimize structs using the tail pad, and sometime it doesn't. 
It ended up in the following stack overflow question :

http://stackoverflow.com/questions/24110347/when-extending-a-padded-struct-why-cant-extra-fields-be-placed-in-the-tail-pad

It seems that C++ will use the padding left in a struct to pack 
more stuff in there. I was wonering what position we want to have 
with D.

Packing elements efficiently is a major importance for 
performance. For now, D uses C style packing (ie: nothing in the 
tail pad) as far as I know. This is missed opportunities to pack 
more (I have many places in SDC where it could be beneficial from 
using tail pad).

If we are gonna interoperable with C++, then we need to 
understand the tail pad optimization anyway. So here is what I 
propose:

extern(D) do tail pad optimize.
extern(C) struct do not tail pad optimize.
extern(C++) do tail pad with C++ rules:
do not tail pad if (otherwise tail pad):
1. has no non-static data members that aren't standard-layout
2. has no virtual functions and no virtual base classes
3. has the same access control for all non-static data members
4. has no base classes that aren't standard-layout
5. either has no base class with non-static data members or has 
no non-static data members in the most derived class and only one 
base with them
6. has no base classes of the same type as the first non-static 
data member

thought ?


More information about the Digitalmars-d mailing list