Home > Net >  Wrapping a normal C function call in another static inline function?
Wrapping a normal C function call in another static inline function?

Time:01-20

I have seen code like the simplified example below:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int myfunc(int a) {
    return a   rand();
}

static inline int myfunc_stat(void) {
    return myfunc(0);
}

int main(void) {
    srand(time(NULL));
    int intern = myfunc(0); //myfunc_stat();
    printf("intern is %d\n", intern);
    return 0;
}

... that is: there is a "normal" function myfunc that accepts a an argument; and then there is a static inline function myfunc_stat which does not receive arguments, and returns the return of the "normal" function called with the constant argument.

I was wondering - would there be any special benefits in "wrapping" a "normal" function in a "static inline" function as shown? At this time, I simply cannot see any difference - in principle, the way I see it, the compiler should just inline the call to myfunc_stat from main, and then there would be a "normal" call to myfunc from there.

But it seems, this example is so simple, I get exactly the same assembly, regardless of if I call myfunc(0); or myfunc_stat(); in main(), from https://godbolt.org/z/M67PTnqeT - the compiler basically decides to inline both functions in either case.

But if the myfunc function was not so obviosly simple (or the build was not optimized), would there be some difference/side-effect/benefit to using a call to myfunc_stat(); as opposed to myfunc(0); (aside from the purely semantic?)

CodePudding user response:

If fifty calls to foo() within a program pass a constant value of zero, having them all call a non-inline function foo_zero which accepts no arguments, and which passes zero to foo will require generating code for foo_zero, but the generated code for each foo_zero(); would likely be an instruction smaller than the code for each foo(0);. On an ARM Cortex-M3, the code for foo_zero() would likely be six bytes, each call foo_zero(0) would take six bytes, and foo_zero() would take four. If foo(0) would get called fifty times, that would likely save 94 bytes of code.

If, however, foo_zero() only got called twice, then the six bytes required for the generated code of foo_zero() would exceed the four bytes saved at other call sites, rendering this attempted optimization counter-productive.

If one is using a compiler that in-line expands functions which are declared static inline, but not others, then one could control whether machine code called foo(0) directly or created and used a foo_zero() wrapper. Since the choice of which is optimal would depend on how much the function is used throughout a program, a compiler that examines source files in isolation would have no way to make that decision optimally.

  •  Tags:  
  • c
  • Related