Consider these two C functions with different headers but the same body differing only in how they return their results (via stack or pointer): T f1(int x) and void f2(int x, T *ret) where sizeof(T) >= 16.
Is there any performance penalty when calling f1 over calling f2 or the compiler like gcc -O2 optimizes both calls into the similar results.
Consider these two C functions with different headers but the same body differing only
Share
There would be a performance penalty from what I see. For f1, the function is going to have to create a object of type T, and then it’s returning a copy of that since you didn’t specify that it should be returned from reference or pointer (but then the function would have to allocate memory for it).
But as for f2, you are sending an object of type T that you have already allocated memory for. This saves time because the function doesn’t have to create a new object or allocate memory for it.
Though I’m not sure if using gcc level 2 optimization uses RVO to avoid copying the object as a return type.