[Insight-developers] note on data accessors

Luis Ibanez ibanez@cs.unc.edu
Sat, 3 Feb 2001 21:39:06 -0500 (EST)


Josh Cates wrote:
> A note on itkDataAccessor:
> I have been playing around with the itkDataAccessor object in some of my
> code and found that gcc -O2 doesn't seem to optimize away the inline
> Get/Set functions.  It would appear you have to go to -O3.
> 
> Josh.

Thanks for pointing this out.
This is quite important because *every* 
pixel access uses one of those calls.


Here is a test to verify whether the calls to Get()/Set()
methods of DataAccessor are being inlined or not.

Unfortunately the only way to know that for sure is
to check the assembly code generated by the compiler.

This can be done, for example by:

1- Compiling on gcc with the option -S.
   that will generate a *.s file, instead of a *.o.
   the .s file contains the code in assembler corresponding
   to the .cpp source file.

2- Using ddd (the data display debugger) and selecting in
   the menu option 'source' display machine code.

3- On VC++ it can be done by a selection on the debugging 
   windows menu

------

The test was done with the following lines of code:

  ImageType::Pointer image = ImageType::New();
  ImageIteratorWithIndex< ImageType > 
  it( image, image->GetRequestedRegion() );
  it.Set( 17 );    // here itkDataAccessor::Set() is called
  it.Set( 33 );    // here itkDataAccessor::Set() is called


When the code is compiled on with gcc -S
the last three lines are translated into:

   ...
   call   _ImageIteratorWithIndex(...constructor...)
   ...
   movl   -24(%ebp),%eax
   movl   $17,%eax
   movl   -24(%ebp),%eax
   movl   $33,%eax
 

%eax represents one of the four main registers on 80x86
architecture. %ebp is the frame pointer that allows to
point to a particular position in memory. 
So, Set() seems to be inlined as expected.

And this is the same whether we use:

   -g -O2 -S

or

   -g -O3 -S



However, there are several other calls that were 
supposed to be inlined an that are not.

For example the constructors and operator= on
itkRGB are not being inlined.  It was necessary
to add the 'inline' keyword on the header and
to compile without debugging in order to get them
inlined.

It seems that we are relying too much on templates
assuming that they will inline most of their methods,
and we have not used 'inline' enough.

A grep on Code/Common shows that the keyword
"inline" is used in only 16 out of 180 header 
files.

BTW the use of 'const' can also help the compiler
to optimize expression, because in some cases it
can avoid to put them in memory variables.





Luis