Home > Software engineering >  How would C hardware destructive & constructive interference size work with the mandated declarati
How would C hardware destructive & constructive interference size work with the mandated declarati

Time:10-14

For reference, the interference size is part of C 17, P0154R1, and mandated declaration order is proposed for C 23, P1847R4.

As far as I understand...

  • The first proposal requires the compiler to move alignas-ed member variables closer together / farther away.

  • The second proposal would require the compiler to lay out member variables in order of declaration in the class.

Seems to me the 2nd proposal takes the punch out of the first. hardware_destructive_interference_size would necessarily require leaving unused memory between the two member variables, with no option to fill it with other members. hardware_constructive_interference_size would be reduced to a warning saying "can't do it, try reordering the member variables yourself".

CodePudding user response:

P0154 makes no changes to how member variables are laid out. It merely exposes some constexpr variables, which you can use to adjust the alignment of member variables through alignas. But alignas doesn't gain any special properties.

That is, these two structs have the same layout, if hardware_destructive_interference_size is 64:

struct one
{
  alignas(hardware_destructive_interference_size) int member1;
  alignas(hardware_destructive_interference_size) int member2;
};

struct two
{
  alignas(64) int member1;
  alignas(64) int member2;
};

P1847 doesn't actually change any behavior. It removes a degree of freedom that was previously available to compilers when laying out the order of members in a class. This removal doesn't change any compiler's behavior because... no compilers took advantage of this degree of freedom. That's why the committee is removing it; nobody did anything with it.

Also, said degree of freedom is the ability to layout members with different access classes (public/private). The layout of members within an access class has been declaration order since (at least) C 11.

So these two changes have nothing to do with one another.

  • Related