[Insight-developers] note on data accessors

Luis Ibanez ibanez@cs.unc.edu
Mon, 05 Feb 2001 10:37:03 -0500


"Miller, James V (CRD)" wrote:

> (Brad can correct me on the standard, but here is my interpretation of "inline".)
>
> "inline" is only a suggestion to the compiler.  The compiler can choose not inline methods marked
> inline.  So even if a method is marked inline and the code appears in the .h file, it is not
> guarenteed to be inlined.

You are right,
'inline' is just a request (or hint) to the compiler.
Some of the things that can prevent the function from being inlined are:

- Having a loop, switch or goto in the function body.
- Being a recursive function.
- Having static variables on the function body.
- Being virtual.

In any case, even if  'inline' is only  a hint to the compiler, 
it seems to be better to use that not to use it at all. On the 
other hand is important to avoid use 'inline' in functions for
which is clear that they will not be inlined, because they can be
created as static functions and being replicated in every .cpp 
file that includes their header. 

>
> The only way I know of to force a routine to be inlined is to put the implementation in with the
> declaration of the function, i.e. within the class body.  This makes for ugly header files but if the
> inlined methods are short (which they should be) then it usually is not too bad.
>

That's a good point.
ITK's code is not uniform on this now. Some classes have methods
implemented on the header file, while others are strictly leaving 
implementation on the .txx or .cpp files, even for one-line methods. 
Maybe we need a rule to decide what is small enough to be put on the 
header...


BTW it was interesting to note that the increment of iterators is not
inlined. so each ++it cost a function call. Unfortunately the operator++ 
is too complex to be fully inlined, but the first part of the method
could be separated.  Given that it is just an 'if'  and a normal
increment when the iterator test to see if it is on the middle of a 
line (a row).  If the operator++ is separated in two parts, one that 
can be inlined, and a function call to the part that cannot be inlined 
(the one that has loops), that will allow the iterator to be fast when 
it is in the middle of a line, and only pay for the function call when 
it needs to go to the next line, slice,...and so on.



Luis