Function calls are cheap — but they are not free — and in tight loops their cost can dominate your runtime. Modern compilers rely on inlining to remove that overhead and unlock deeper optimizations, sometimes turning an ordinary loop into dramatically faster SIMD code.
The cost of a function call
by Daniel Lemire
From the article:
When programming, we chain functions together. Function A calls function B. And so forth.
You do not have to program this way, you could write an entire program using a single function. It would be a fun exercise to write a non-trivial program using a single function… as long as you delegate the code writing to AI because human beings quickly struggle with long functions.
A key compiler optimization is ‘inlining’: the compiler takes your function definition and it tries to substitute it at the call location. It is conceptually quite simple. Consider the following example where the function
add3calls the functionadd.int add(int x, int y) { return x + y; } int add3(int x, int y, int z) { return add(add(x, y), z); }You can manually inline the call as follows.
int add3(int x, int y, int z) { return x + y + z; }

Add a Comment
Comments are closed.