The cost of a function call -- Daniel Lemire

Capture-decran-le-2026-02-08-a-15.07.17-825x510.pngFunction 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 add3 calls the function add.

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.

Comments (0)

There are currently no comments on this entry.