The question is inspired by the following statement:

7.14 Functions

It is recommended that you use the following functions: The current declaration of the current module (ie the current declaration. This enables the compiler to optimize across function calls. You can’t use it.

A source

How does the optimization for static functions work?

    1 answer 1

    The idea of ​​the cited advice is based on the fact that the definition of this function is always available to the compiler in the place where the function with internal binding is called.

    Therefore, first, the compiler always has the ability to embed a call to such a function, if it deems it necessary.

    Secondly, regardless of whether a function is embedded or not, having access to the definition of a function, the compiler has a full understanding of its behavior (for example, which elements of the transmitted aggregate it modifies and which does not), which gives it the opportunity to better optimize the calling code.

    static void foo(int a[]) { a[5] = 5; a[rand() + 1] = rand(); } void bar() { int a[100] = { 42 }; foo(a); // Здесь компилятор знает, что `a[0]` по-прежнему `42` } 

    Thirdly, the compiler has a complete understanding of all the contexts for calling such a function and can optimize the function code taking into account the peculiarities of these contexts. (Provided that such a function is not accessible from outside by indirect means, i.e. via pointers)

     static void foo(int a, int b) { // Здесь компилятор знает, что `a` всегда четно, // а `b` не превосходит `3` } ... foo(i * 2, 2); ... foo(j * 4, 3); ... // Других вызовов `foo` нет