Boost logo

Boost :

Subject: Re: [boost] [optional] memory use for optional refs and ptrs
From: Rutger ter Borg (rutger_at_[hidden])
Date: 2010-10-06 08:09:17


On 2010-10-06 13:11, Stewart, Robert wrote:

>> 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.
>
> That's an interesting scenario, but is it likely that so many pieces would be optional in a single context like that?

This is likely indeed, if you're going to model message structures, the
most direct representation commonly contains a great deal of optionals.

> (I haven't looked, but I do hope that the bool follows the optional value to reduce padding overhead.)

It doesn't, the boolean is followed by the optional value (see
boost/optional/optional.hpp lines 447-448).

>
> I wouldn't want that to replace the current design, but it would be useful as an alternative to the current design or an extension of it.
>

I agree.

Cheers,

Rutger


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