Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a long function inline

Suppose I have a 10 line function. If I add inline keyword, let's say there is a chance of 50% that compiler will make it inline.

If I have a 2 line function, there might be 90% chance it will be inlined.

Can I split the code in 10 line function into 5 functions to make it inlined with better chances?

like image 353
paseena Avatar asked Feb 24 '23 14:02

paseena


1 Answers

There may be a reason why the compiler isn't inlining it, possibly something to look at. In addition, the function call overhead becomes less of an issue with longer functions, so inlining them may not be as important (if that's your only reason).

Splitting the function into 5 small functions will just make a mess of your code, and possibly confuse the compiler and end up with it not inlining anything. I would not recommend that.

Depending on your C++ compiler, you may be able to force it to inline the function. Visual C++ has the __forceinline attribute, as well as a setting for how inlining should be handled and how often it should be used in the project settings. As Tony mentions, the GCC equivalent is __attribute__((always_inline)).

You may also be able to use some preprocessor trickery to inline the code itself, but I would not typically recommend that.

like image 127
ssube Avatar answered Mar 07 '23 12:03

ssube