How does the use of `static` affect the speed of my code?

Clang uses a cost based decision whether a function will be inlined or not. This cost is affected by a lot of things. It is affected by static.

Fortunately, clang has an output, where we can observe this. Check out this godbolt link:

void call();

inline void a() {

static inline void b() {

void foo() {

In this little example, a() and b() are the same, the only exception is that b() is static.

If you move the mouse over the calls a() or b() on godbolt (in OptViewer window), you can read:

a(): cost=0, threshold=487

b(): cost=-15000, threshold=487

(clang will inline a call, if the cost is less than the threshold.)

clang gave b() a much lower cost, because it is static. It seems that clang will only give this -15000 cost reduction for a static function only once. If b() is called several times, the cost of all b()s will be zero, except one.

Here are the numbers for your case, link:

process_value(): cost=400, threshold=325 -> it is just above the threshold, won't be inlined

process_valueS():: cost=-14600, threshold=325 -> OK to inline

So, apparently, static can have a lot of impact, if it is only called once. Which makes sense, because inlining a static function once doesn't increase code size.

Tip: if you want to force clang to inline a function, use __attribute__((always_inline)) on it.