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

Luis Ibanez luis.ibanez at kitware.com
Mon Jun 9 10:39:31 EDT 2008


Just for the record here is a minimal program that illustrates
what gcc is storing in the bytes previous to the array pointer.

It has to #defines to be passed at compilation time:

-DALIGN  will enable the __attribute__ ((aligned(8)))
-DWITHDESTRUCTOR will enable the destructor of the class

The program allocates an array of 17 elements of the class.
Then in prints out the values of the bytes preceding the
pointer returned by "new", by interpreting them as integers


   [-12,-11,-10,-9][-8,-7,-6,-5][-4,-3,-2,-1][MyArray[0]][MyArray[1]...
     ^               ^            ^           ^
     |               |            |           |
    ps-2           ps-1          ps           P = new MyArray[k];



Here are the results of running this program in Gcc 4.1.2:

zion [204] g++ -DALIGN -DWITHDESTRUCTOR fixedArrayAlignment.cxx; ./a.out
Alignment 0
MyArray sizeof = 56
Integer value at  -4 Bytes = 17
Integer value at  -8 Bytes = 0
Integer value at -12 Bytes = 969

zion [205] g++ -DALIGN  fixedArrayAlignment.cxx; ./a.out
Alignment 0
MyArray sizeof = 56
Integer value at  -4 Bytes = 961
Integer value at  -8 Bytes = 0
Integer value at -12 Bytes = 0

zion [206] g++  -DWITHDESTRUCTOR fixedArrayAlignment.cxx; ./a.out
Alignment 4
MyArray sizeof = 56
Integer value at  -4 Bytes = 17
Integer value at  -8 Bytes = 961
Integer value at -12 Bytes = 0

zion [207] g++  fixedArrayAlignment.cxx; ./a.out
Alignment 0
MyArray sizeof = 56
Integer value at  -4 Bytes = 961
Integer value at  -8 Bytes = 0
Integer value at -12 Bytes = 0


Note that 56 is the sizeof(MyArray), because it contains a group
of 7 doubles:  7 * sizeof(double) = 56.

The number 961 seems to result from the space required for storing
17 elements of 56 bytes each, plust the information about the array
that preceeds the array:

                 961 - 17 * 56 = 9  bytes



My suggestion will be to provide a set of convinience macros to
faciliate the request for the alignment attribute of classes
when users need it.  E.g. following the line of the ALIGN8 macro.


Users defining images with nontrivial type, a use will do:


     class MyPixelType : public itk::Vector<double,3>
     {
     } ITK_ALIGN8;

     typedef itk::Image< MyPixelType, 3 >  MyImageType;


The rationale is the the alignment may not matter in many situations,
e.g. in an image itk::Image<char,3>, and only the user, when
instantiating the type of the image will be in a good position to
know where it is worth to insert the alignment.


It still remains to be seen how this behaves in compilers
different from gcc...



     Luis


--------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fixedArrayAlignment.cxx
Type: text/x-c++src
Size: 684 bytes
Desc: not available
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20080609/39f27065/attachment.cxx>


More information about the Insight-developers mailing list