Wednesday, October 19, 2005

Inline functions optimization?

Inline functions are definitely way better choice over macros! C++ has introduced this feature which is yet another indicator of the richness of this language.
But though this is a great feature for optimization, it's not in complete control of the programmer. The decision of actual inlined function code inclusion rests with the compiler.
What do you think is the reason that the compiler has been given the total control of the decision that whether an inlined function should be actually inlined or not? And then if some functions are not inlined by compiler, why aren't they?

1 Comments:

Blogger Eklavya said...

The reason the compiler has been given the control is because programmers can be sloppy and can thus take uninformed decisions w.r.t to making functions inline. The idea behind inline functions is to avoid the overhead associated with the creation of new frames for functions that are small say less than 10 lines or so. It would be best to execute them inline. Now say if a programmer is given such control and he/she marks a very big function as inline, imagine the amount of code bloat that would ensue as a result esp. if that function was invoked at several places in the code. This would increase the size of our executable image with the same copy of the function lying at several places. The basic idea behind using functions would be lost. Such functions would never be inlined by the compiler. Although, it should be noted that if the same function is invoked only once during the entire execution of the code, it would make sense to make it inline. Hence although the programmer is allowed to request for inline privileges, its only the compiler that should take the decision. Comments/suggestions/rants welcome!

9:52 PM  

Post a Comment

<< Home