Boost logo

Boost :

Subject: Re: [boost] [optional] memory use for optional refs and ptrs
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2010-10-07 07:22:13


Sebastian Redl wrote:
> On 06.10.2010, at 20:22, Stewart, Robert wrote:
>
> > If any of the types, other than bool, were smaller than
> > int, I think you'll find some differences, but the answer
> > does depend upon compiler options.
>
> No, and no.
>
> First, this snippet:
>
> struct I {
> double d;
> char c;
> // expect 7 bytes padding
> };
>
> static_assert(sizeof(I) == 16, "Bad size for I");
>
> struct O {
> I i;
> short s; // placed in padding?
> };
>
> If your theory is true, then the compiler would place s in
> the padding of i in the layout of O. Trying this out with
> Clang (which is ABI-compatible with GCC, so GCC would give
> the same result) shows that the size of O is 24, meaning that
> s is not placed in the padding.
> I can't try MSVC. Maybe it does things differently.

That's not what I was suggesting. short can have alignment requirements, but bool and char do not. I was suggesting that this:

struct P
{
   short s;
   char c;
   short t;
   char d;
};

could occupy less space than if the chars and shorts were reversed. That is, that
P::c could occupy padding between P::s and P::t and P::d could occupy padding between a P instance and something following it in another composite. Doing so would not violate an ABI, unless the ABI specifically disallowed it, because it can be established as the expected layout in those cases.

Applying this to the optional case:

struct Q
{
   optional<short> s;
   optional<short> t;
};

If the bools in the optionals were to follow the shorts, then Q's size could be smaller than if they precede the shorts. The use of aligned storage doesn't even matter in that case as it would dictate the alignment of the shorts.

I checked the above using 32 and 64 bit GCC 4.1.2 and ICC 11.0.083 (which maintains GCC ABI compatibility), and 32 bit MSVC 9 and found that the size did not differ, so for those compilers, at least, my assertions are incorrect. That doesn't disprove the theory, but does disprove its application in those cases.

Compilers can be instructed to pack structures differently, in which case they might be coerced to satisfy my theory, even if they wouldn't by default. However, that doesn't affect optional, which was the point of this discussion.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk