Boost logo

Boost Users :

Subject: Re: [Boost-users] sizeof(boost::fusion:map)
From: Joel de Guzman (joel_at_[hidden])
Date: 2010-11-06 00:49:51


On 11/6/2010 7:56 AM, Christopher Schmidt wrote:
> Christopher Schmidt schrieb:
>> Robert Lupton the Good schrieb:
>>> I was very excited to find the boost::fusion library, expecting that I could use it to implement structs with introspection. I still think I can, but I was disappointed to see that I'm paying a size penalty. I'd have expected all the book-keeping to be handled at compile time.
>>>
>>> The following code prints
>>> sizeof(Foo) = 12 sizeof(FooS) = 4
>>> with g++ 4.2 or 4.4 on an os/x 10.6 or Centos box.
>>>
>>> Here Foo is
>>> boost::fusion::map<boost::fusion::pair<Name, int> >
>>>
>> [snip]
>>
>> I have no idea why there is that space overhead. This problem is not
>> fusion specific, though.
>>
>> #include<iostream>
>> struct X{};
>> struct A:X{int i;};
>> struct B:X{A a;};
>> int main()
>> {std::cout<< sizeof(A)<< " "<< sizeof(B)<< std::endl;}
>>
>> When compiled with gcc 4.5.1 i686-pc-mingw32 and -O2, the output is "4
>> 8". When compiled with MSVC 2010 (x86) and /O2, "4 4" is printed.
>>
>> -Christopher
>
> I think gcc is correct. There are two distinct instances of X in B, and
> those may not have the same address. That's why there is that 4 byte
> displacement.
> It is pretty much the same in Fusion. All inbuilt sequences and the
> vector data storage share a common base class - fusion::sequence_root -
> that's where the 4 resp. 8 bytes come from.

Good analysis, Christopher. We should do something about this.
sequence_root is only there for easy tag detection. There should
be better way.

Regards,

-- 
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net