Function overhead

Thread Starter

gogo00

Joined Oct 28, 2023
43
I'm looking help to understand the inline functions compared to regular functions, and I'm particularly puzzled by the term "function overhead." In the context of regular functions in C, I know that when a function is called from the main function, the program allocates stack memory for variables, performs operations, and then releases the allocated memory when the function exits. For example, if we have a function that takes one argument and increments its value, . However, I'm unclear on how this relates to inline functions and what the specific function overhead might be in that scenario?
 

BobTPH

Joined Jun 5, 2013
11,466
A function call needs to execute at least two instructions, one to call the function and one to return. With arguments and local storage it could be quite a few more.

A function that is inline expanded needs none of that. Let’s look at your example.

void incr(int *x)
{
(*x)++;
}

Here is a call to it:

incr(a);

If inline expanded, the compiler would generate the sam code as for:

a++;

The overhead is all the code in addition to the increment in the function.

Additionally, the fact that you are passing the address of a to a function might suppress optimization of code involving a.
 

Thread Starter

gogo00

Joined Oct 28, 2023
43
A function call needs to execute at least two instructions, one to call the function and one to return. With arguments and local storage it could be quite a few more.

A function that is inline expanded needs none of that. Let’s look at your example.

void incr(int *x)
{
(*x)++;
}

Here is a call to it:

incr(a);

If inline expanded, the compiler would generate the sam code as for:

a++;

The overhead is all the code in addition to the increment in the function.

Additionally, the fact that you are passing the address of a to a function might suppress optimization of code involving a.
I'm still trying to understand the distinction between regular functions and inline functions. I explained how a regular function works. Now, regarding inline functions, I'm not exactly clear on what happens in terms of memory allocation and execution. If I call an inline function in the main program, it seems like it would perform the same operation as a regular function call. So, where does the difference come in? How does the inline function achieve the objective. and what exactly happens during its execution compared to a regular function?
 

WBahn

Joined Mar 31, 2012
32,710
I'm still trying to understand the distinction between regular functions and inline functions. I explained how a regular function works. Now, regarding inline functions, I'm not exactly clear on what happens in terms of memory allocation and execution. If I call an inline function in the main program, it seems like it would perform the same operation as a regular function call. So, where does the difference come in? How does the inline function achieve the objective. and what exactly happens during its execution compared to a regular function?
No!

An inline function is NOT a function at all.

The function call, in it's entirety, is replaced with inline code that performs the same task. So there IS no function calling overhead because no function is ever called.

A better and more accurate way to think of it is not that you have a special kind of function (i.e., an "inline function"), but rather that you have a function that you are asking the compiler to "inline" for you. To convert it from being a function call to just being equivalent statements that are placed in line with the surrounding code. And note that you are just asking the compiler to consider doing that -- you can't force it to. You are merely providing a hint to the compiler to help it make its optimization decisions.
 

nsaspook

Joined Aug 27, 2009
16,257
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
always_inline
Generally, functions are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function independent of any restrictions that otherwise apply to inlining. Failure to inline such a function is diagnosed as an error. Note that if such a function is called indirectly the compiler may or may not inline it depending on optimization level and a failure to inline an indirect call may or may not be diagnosed.
There's usually a command to shoot yourself in the foot with C. ;)
 

Papabravo

Joined Feb 24, 2006
22,058
People who have never written assembly language code ask such questions. Those questions are born of willful ignorance which is not the fault of the person asking that question.
 

nsaspook

Joined Aug 27, 2009
16,257
People who have never written assembly language code ask such questions. Those questions are born of willful ignorance which is not the fault of the person asking that question.
I look at the positive here, the fact the question was asked means a willingness to learn.
 

Ajith-N

Joined Sep 14, 2020
31
And don't forget, if you inline a large function, be wary. The compiler will generate those instructions at every place where you "invoke" the inlined function (nay, just refer, mere mention of the inline function would suffice). Your program's executable code (binary) could balloon and might end up biting you. These trade-offs are always there when you optimize.

Another rule of optimization -- do not optimize unless you know that you will need it.

The old jungle saying goes thus: "Premature optimization is the root of all evil":)
 
Top