A function that only calls another function slows down?

I saw code that was something like this

int *func2(int *var) { //Do some actual work return var; } int *func1(int *var) { return func2(var); } int main() { int var; var = func1(&var); return 0; } 

It seems to me that this is an incredible waste, but I thought that the intermediate function could have two functions that it could name, or there are some plans for expansion in the future. I was just wondering if compilers like gcc can detect such things and eliminate a useless function in a real program, or if this thing actually takes CPU cycles at runtime?

+4
source share
4 answers

Super quick answer: Yes, maybe.

Quick answer: Yes, but usually it’s not enough for you to take care, and sometimes not at all.

The complete answer: if all the functions are in the same translation system, and the compiler is not sucked, the additional level of the function call will simply be optimized, and it will be zero . top performance. Otherwise, if you are making external function calls, expect a small but non-zero performance. In most cases, this does not matter, but in functions that are super-short, where each cycle is counted, this can make your program twice as slow or worse. Some of the worst examples are:

  • A function of type getc , which simply pulls the next byte from the buffer, advances the position and returns (in the general case, when the buffer is not empty).
  • A function that promotes a state machine through a trivial operation and returns, for example, processes one byte of the UTF-8 character.
  • Lock / sync items. This is a bit of a special case, because actual access to atomic memory should dominate the execution time, making the overhead seem insignificant. But if your intended use case is only to hold locks for one trivial operation (for example, lock(); a++; unlock(); ), then even a small amount of added time with locking can have serious consequences for competing characteristics if blocking is very contrary.

Finally, the answer is β€œwhat should you do”: write your code in the most natural way until testing / measurement shows you performance problems. Only then should you consider removing your code for performance.

+1
source

Do not use premature optimization. Focus on writing readable code. Even without optimization, an extra function call is likely to have minimal performance impact. The compiler can select it.

If you have performance problems later, you can check the profile to find bottlenecks.

+12
source

In most cases, if you increase compiler optimization high enough, such trivial functions will be inlined . Thus, there is no overhead.

So, the answer to your question: Yes, the compiler is usually smart enough to eliminate the call.
Therefore, do not worry about it if you do not need to.

You can also use the inline to make it more explicit: (although the compiler can still ignore it)

 inline int *func1(int *var) { return func2(var); } 
+4
source

This will depend on the compiler and runtime environments. If the method call is on the stack, there will be small additional overhead from the stack increment. But in this case, everything is on the pointer, so it can be a tail and will probably be inline. Calling inline / tail will cause the function to work like a jump instead of increasing the stack; what it will be like a loop or goto at startup.

0
source

Source: https://habr.com/ru/post/1395121/


All Articles