The cost of a function call -- Daniel Lemire
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; }

Finding out how to implement features from the standard library can be a useful learning exercise. Quasar Chunawala explores implementing your own version of std::vector.
What do you do when the code for a variable initialization is complicated? Do you move it to another method or write inside the current scope? Bartlomiej Filipek presents a trick that allows computing a value for a variable, even a const variable, with a compact notation.