Boost logo

Boost :

Subject: Re: [boost] [optional] memory use for optional refs and ptrs
From: Larry Evans (cppljevans_at_[hidden])
Date: 2010-10-14 11:29:45


On 10/06/10 02:56, Rutger ter Borg wrote:
> On 2010-10-06 00:19, Fernando Cacciola wrote:
>> Ok, yes, in that case there is no possible doubt about the semantics, so
>> the boolean can be spared.
>>
>> OTOH, is such a corner case that I don't imagine myself doing it in the
>> sort term.
>>
>
> Although it isn't the most important case for me, it has its own section
> in the documentation, and I will probably be using lots of these. What
> also doesn't seem right to me is that, e.g.,
>
> struct A {
> int a;
> double b;
> double c;
> std::string d;
> };
>
> struct B {
> boost::optional< int > a;
> boost::optional< double > b;
> boost::optional< double > c;
> boost::optional< std::string > d;
> };
>
> the size of A is 32 bytes in this case, and the sizeof B is 56 bytes:
> almost doubled. So, while it may be argued that the case above is a
> corner case, too, I don't it's an acceptable penalty to anyone to pay 24
> bytes for something that can be stored in 4 bits and/or in the type itself.
>
> What would make optional useful to me, is a kind of optional that allows
> the boolean set/get stuff to be done (and possibly stored) outside of
> the optional itself. E.g., by means free functions that can be
> overloaded (and throw in the ability of defining tags to the optional
> template-parameters to be able to define a bit# in a bitset).

I think the most efficient storage would be in descending order of
alignment values followed by the bits for the tags. The reason for
this is that a type with a smaller aligned value can be used to
fill the padding for the previous type. Since the bool tags
have align==1, they could go last.

This is shown by the A_sort_bools_type output from:

https://svn.boost.org/trac/boost/browser/sandbox/variadic_templates/libs/composite_storage/sandbox/layout_all.1.cpp

which shows 0 padding using this rule. Of course, as shown by
the output for bools_A_type, that's not the only way to get 0
padding for a set of types.

The layout_all.1.cpp output is reproduced in the attachment.

HTH.

-Larry




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