Boost logo

Boost Users :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2006-06-14 11:32:31


On Jun 14, 2006, at 10:22 AM, David Abrahams wrote:

> Howard Hinnant <hinnant_at_[hidden]> writes:
>
>> There are applications such as boost::function where aligned storage
>> for an unknown type is useful. In boost::function's case, aligned
>> storage could be used to implement the "short string optimization".
>
> And so it is, AFAIK.
>
>> The general pattern is:
>>
>> class Container
>> {
>> private:
>> const unsigned buf_size = ...;
>> aligned_storage<buf_size>::type buffer;
>> public:
>> template <class T>
>> Container(const T& t) {/* might stick t into buffer instead
>> of on heap, based on sizeof(T) */}
>> };
>>
>> On Jun 14, 2006, at 6:55 AM, David Abrahams wrote:
>>
>>> I think what the OP is looking for is called boost::shared_ptr ;-)
>>
>> shared_ptr can't compete with this application performance-wise.
>
> Well, it depends how expensive it is to copy your small object. If it
> turns out that T is a vector, and it fits in your local buffer, when
> Container is copied it might be a lot cheaper to use shared_ptr.

Sure. I'm responding to the assertion that you need a known complete
type to effectively use aligned_storage. My point is that there
exist valuable use cases where this assertion is false. The
existence of these cases is what makes the defaulted alignment
template parameter on aligned_storage valuable.

Tony's observation is that this default value is even more valuable
if it is dependent upon the buffer size, which again I fully agree
with. If you want a 5 byte buffer that is aligned to the "most
stringent alignment requirements" (definition found in 5.3.4p10), an
alignment of 16 (for example) is likely to be correct but overkill.
I had personally settled on 8 for this particular example but Tony
makes a convincing argument for 4 being the optimum (all numbers
assuming current popular desktop processors and quoted just for
example).

-Howard


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