Does stack space required by a function affect inlining decisions in C/C++?

  • A+
Category:Languages

Would a large amount of stack space required by a function prevent it from being inlined? Such as if I had a 10k automatic buffer on the stack, would that make the function less likely to be inlined?

int inlineme(int args) {   char svar[10000];    return stringyfunc(args, svar); } 

I'm more concerned about gcc, but icc and llvm would also be nice to know.

I know this isn't ideal, but I'm very curious. The code is probable also pretty bad on cache too.

 


Yes, the decision to inline or not depends on the complexity of the function, its stack and registers usage and the context in which the call is made. The rules are compiler- and target platform-dependent. Always check the generated assembly when performance matters.

Compare this version with a 10000-char array not being inlined (GCC 8.2, x64, -O2):

inline int inlineme(int args) {   char svar[10000];    return stringyfunc(args, svar); }  int test(int x) {     return inlineme(x); } 

Generated assembly:

inlineme(int):         sub     rsp, 10008         mov     rsi, rsp         call    stringyfunc(int, char*)         add     rsp, 10008         ret test(int):         jmp     inlineme(int) 

with this one with a much smaller 10-char array, which is inlined:

inline int inlineme(int args) {   char svar[10];    return stringyfunc(args, svar); }  int test(int x) {     return inlineme(x); } 

Generated assembly:

test(int):         sub     rsp, 24         lea     rsi, [rsp+6]         call    stringyfunc(int, char*)         add     rsp, 24         ret 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: