[Insight-developers] Empty FixedArray destructor: Performancehit using gcc (times 2)

Tom Vercauteren tom.vercauteren at m4x.org
Thu Jun 5 17:30:27 EDT 2008


> I am really surprised that C++ does not generate a proper alignment
> for double. What flags are you using for your build?

Nothing special. I tried
-O0
-O2 -g
-O2
and
-O3

The behavior is rather consistent across these flags. It is also
consistent across computers. We tested on 2 ubuntu boxes with gcc 4.1
and a mac running Mac OS 10.5 and mac gcc 4.0

Could you run the test I submitted? Was it inconclusive?

Tom

>
> Bill
>
> On Thu, Jun 5, 2008 at 3:49 PM, Tom Vercauteren <tom.vercauteren at m4x.org> wrote:
>> Hi,
>>
>> As shown in the code I sent, the real performance hit does NOT come
>> from the destructor itself but from the memory alignment.
>>
>> The example shows that by using the same vanilla FixedArray class
>> (where the destructor is explicitly implemented as empty), the well
>> aligned case is faster than the badly aligned case.
>>
>> What is weird is why the empty destructor leads to bad memory alignment...
>>
>> What is striking is that if you replace the c array of itk::FixedArray
>> by a std::vector<itk::FixedArray>, you don't get any performance hit.
>> I guess that STL takes care of aligning things in an optimized manner.
>>
>> By the way, this also implies that if ITK was using STL vector instead
>> of c array in its image class, we wouldn't have stumbled on the issue.
>>
>> Anyhow, I agree with Karthik and Bill. In this specific case,
>> explicitly implementing an empty destructor seems useless.
>>
>> Tom
>>
>> On Thu, Jun 5, 2008 at 9:02 PM, Niels Dekker <niels-xtk at xs4all.nl> wrote:
>>> Bill Lorensen wrote:
>>>>
>>>> Here is an FAQ on destructors:
>>>> http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=370
>>>> They say that an empty destructor is pretty much useless.
>>>
>>> The destructor of FixedArray<TValueType, VLength> invokes the destructor of
>>> TValueType for all of its elements.  So it may not be as empty as it
>>> seems...  When the destructor is removed from FixedArray, it will be
>>> implicitly defined as an inline function, as the FAQ you're referring to
>>> says.  This might give a performance gain, but it might also produce a
>>> larger binary.  Things might get worse (or better?) when TValueType itself
>>> has also has an inline destructor.
>>>
>>> In some special cases, the destructor of a class /must/ be implemented in a
>>> CXX file, even if it is empty. As is the case discussed at GameDev.Net,
>>> "auto_ptr within Pimpl Class",
>>> http://www.gamedev.net/community/forums/topic.asp?topic_id=437587
>>>
>>> Anyway, just my 2 cents.  It might still be okay to remove FixedArray's
>>> destructor...
>>>
>>> Kind regards,
>>> --
>>> Niels Dekker
>>> http://www.xs4all.nl/~nd/dekkerware
>>> Scientific programmer at LKEB, Leiden University Medical Center
>>>
>>
>


More information about the Insight-developers mailing list