Why do function calls incur overhead?

Often people talk about calling functions that produce a certain amount of overhead or an inevitable set of additional problems and circumstances in the program. Can this be better explained and compared with a similar program without calling a function?

+1
source share
4 answers

It depends on your compiler settings and how you optimize the code. Some features are built-in. No others. This usually depends on whether you are optimizing size or speed.

Typically, the calling function causes a delay for two reasons:

  • The program must connect to a random location in the memory where the function code is run. To do this, he needs to save the current cursor position on the stack so that he knows where to return. This process takes more than one CPU cycle.

  • Depending on the architecture of your processor, there may be a pipeline that retrieves the next few instructions from memory to the processor cache in parallel with the execution of the current command. This speeds up execution speed. When you call the function, the cursor intercepts a completely different address, and all cached instructions are cleared from the pipeline. This leads to further delays.

+3
source

Also see here and here for a discussion of when an attachment will make sense.

  • Embedding

    In general, you can only offer the inline function to the compiler, but the compiler may decide otherwise. Visual Studio offers its own forceinline . Some functions cannot be built in, for example. when they are recursive or when the objective function cannot be determined at compile time (calls through function tables, calls to virtual functions in C ++).

    I suggest you trust the compiler if the function should be inline. If you really want to embed your code, consider using a macro.

  • Overhead

    The overhead of the memory is minimal when using functions because you are not duplicating the code; Embed code is duplicated on the call site. The overhead of productivity these days is negligible, because modern architectures are really good, predicting and causing only about 1-2 cycles.

+3
source

Functions can be inlined , but the norm (basically) is functions at a specific address, and the values ​​passed to the functions are inlined stack, and the result is then inlined stack and returned.

+2
source

Functions can certainly be built-in if certain conditions are met, but they are definitely not always integral. Most often, calling a function calls a genuine call without a function. A function call has some additional costs associated with it, such as

  • Preparing parameters for a function in accordance with the function call convention
  • Getting the return value of a function
  • Functional prologue and epilogue code responsible for managing local memory, managing parameter memory and storing register values.
  • The function can compress some processor registers, thereby violating their use in the calling code and thereby hinder optimization
  • Less processor-friendly and caching non-linear code behavior

All this will lead to overhead, probably would not have happened if the body of the function were embedded inline in the calling code.

+1
source

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


All Articles