Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 10, 2014 Tail pad optimization, cache friendlyness and C++ interrop | ||||
---|---|---|---|---|
| ||||
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 ? |
June 10, 2014 Re: Tail pad optimization, cache friendlyness and C++ interrop | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | deadalnix:
> thought ?
I think for D there is a lower handing fruit: the D specs allow to reorder class instance fields, but I think the D front-end is not yet doing that.
Bye,
bearophile
|
June 10, 2014 Re: Tail pad optimization, cache friendlyness and C++ interrop | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Tuesday, 10 June 2014 at 08:57:26 UTC, bearophile wrote:
> deadalnix:
>
>> thought ?
>
> I think for D there is a lower handing fruit: the D specs allow to reorder class instance fields, but I think the D front-end is not yet doing that.
But only for classes, not for structs.
|
June 10, 2014 Re: Tail pad optimization, cache friendlyness and C++ interrop | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | 10-Jun-2014 11:30, deadalnix пишет: [snip] > 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 ? Would be useful, but as proposed it might break some of current C bindings silently (unless it's extern(C): at the top of file). -- Dmitry Olshansky |
June 10, 2014 Re: Tail pad optimization, cache friendlyness and C++ interrop | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On 6/10/2014 12:30 AM, deadalnix wrote:
> thought ?
This does not apply to D structs because D structs do not inherit.
C doesn't have classes, so no issues there.
extern(C++) class should match the C++ ABI for this.
extern(D) class we are free to innovate with the layout.
I suggest turning this into a bugzilla enhancement request.
|
June 11, 2014 Re: Tail pad optimization, cache friendlyness and C++ interrop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Tuesday, 10 June 2014 at 20:50:46 UTC, Walter Bright wrote:
> On 6/10/2014 12:30 AM, deadalnix wrote:
>> thought ?
>
> This does not apply to D structs because D structs do not inherit.
>
> C doesn't have classes, so no issues there.
>
> extern(C++) class should match the C++ ABI for this.
>
> extern(D) class we are free to innovate with the layout.
>
> I suggest turning this into a bugzilla enhancement request.
I'm talking about structs, not classes.
|
June 11, 2014 Re: Tail pad optimization, cache friendlyness and C++ interrop | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On 6/10/2014 5:27 PM, deadalnix wrote:
> I'm talking about structs, not classes.
Ok, but since D structs do not inherit, how does tail pad optimization apply?
|
June 11, 2014 Re: Tail pad optimization, cache friendlyness and C++ interrop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Wednesday, 11 June 2014 at 02:10:18 UTC, Walter Bright wrote:
> On 6/10/2014 5:27 PM, deadalnix wrote:
>> I'm talking about structs, not classes.
>
> Ok, but since D structs do not inherit, how does tail pad optimization apply?
struct S1 {
int a;
byte b;
}
struct S2 {
S1 s1;
char c;
}
Sé could be 8 byte long (some variation of this are in C++ for
instance if I make a public and b private).
|
June 11, 2014 Re: Tail pad optimization, cache friendlyness and C++ interrop | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On 6/10/2014 7:44 PM, deadalnix wrote:
> On Wednesday, 11 June 2014 at 02:10:18 UTC, Walter Bright wrote:
>> On 6/10/2014 5:27 PM, deadalnix wrote:
>>> I'm talking about structs, not classes.
>>
>> Ok, but since D structs do not inherit, how does tail pad optimization apply?
>
> struct S1 {
> int a;
> byte b;
> }
>
> struct S2 {
> S1 s1;
> char c;
> }
>
> Sé could be 8 byte long (some variation of this are in C++ for
> instance if I make a public and b private).
Do any C++ compilers do this for this case, or just for the inheritance one?
|
June 11, 2014 Re: Tail pad optimization, cache friendlyness and C++ interrop | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On 6/10/2014 7:44 PM, deadalnix wrote:
> On Wednesday, 11 June 2014 at 02:10:18 UTC, Walter Bright wrote:
>> On 6/10/2014 5:27 PM, deadalnix wrote:
>>> I'm talking about structs, not classes.
>>
>> Ok, but since D structs do not inherit, how does tail pad optimization apply?
>
> struct S1 {
> int a;
> byte b;
> }
>
> struct S2 {
> S1 s1;
> char c;
> }
Hmm, this could have serious problems with the following:
S1 s1;
S2 s2;
s2.c = 3;
memcpy(&s2.s1, &s1, sizeof(S1)); // Oops! stomped on s2.c
|
Copyright © 1999-2021 by the D Language Foundation