Inlining of vararg functions

前端 未结 4 1961
不知归路
不知归路 2021-01-17 08:38

While playing about with optimisation settings, I noticed an interesting phenomenon: functions taking a variable number of arguments (...) never seemed to get i

4条回答
  •  爱一瞬间的悲伤
    2021-01-17 08:51

    The point of inlining is that it reduces function call overhead.

    But for varargs, there is very little to be gained in general.
    Consider this code in the body of that function:

    if (blah)
    {
        printf("%d", va_arg(vl, int));
    }
    else
    {
        printf("%s", va_arg(vl, char *));
    }
    

    How is the compiler supposed to inline it? Doing that requires the compiler to push everything on the stack in the correct order anyway, even though there isn't any function being called. The only thing that's optimized away is a call/ret instruction pair (and maybe pushing/popping ebp and whatnot). The memory manipulations cannot be optimized away, and the parameters cannot be passed in registers. So it's unlikely that you'll gain anything notable by inlining varargs.

提交回复
热议问题