Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A virtual function returning a small structure - return value vs output parameter?

Tags:

c++

I have a virtual function in a hotspot code that needs to return a structure as a result. I have these two options:

virtual Vec4 generateVec() const = 0; // return value

virtual void generateVec(Vec4& output) const = 0; // output parameter

My question is, is there generally any difference in the performance of these functions? I'd assume the second one is faster, because it does not involve copying data on the stack. However, the first one is often much more convenient to use. If the first one is still slightly slower, would this be measurable at all? Am I too obsessed :)

Let me stress that that this function will be called millions of times every second, but also that the size of the structure Vec4 is small - 16 bytes.

like image 471
Iliyan Georgiev Avatar asked May 30 '11 14:05

Iliyan Georgiev


People also ask

Can virtual function return value?

Yes. The return types are allowed to be different as long as they are covariant.

Does virtual function have return type?

The return type of an overriding virtual function may differ from the return type of the overridden virtual function. This overriding function would then be called a covariant virtual function. Suppose that B::f overrides the virtual function A::f .

What is difference between function and virtual function?

Non- virtual member functions are resolved statically. That is, the member function is selected statically (at compile-time) based on the type of the pointer (or reference) to the object. In contrast, virtual member functions are resolved dynamically (at run-time).


2 Answers

As has been said, try them out - but you will quite possibly find that Vec4 generateVec() is actually faster. Return value optimization will elide the copy operation, whereas void generateVec(Vec4& output) may cause an unnecessary initialisation of the output parameter.

Is there any way you can avoid making the function virtual? If you're calling it millions of times a sec that extra level of indirection is worth looking at.

like image 133
Gavi Lock Avatar answered Sep 28 '22 07:09

Gavi Lock


Code called millions of times per second implies you really do need to optimize for speed.

Depending on how complex the body of the derived generateVec's is, the difference between the two may be unnoticeable or could be massive.

Best bet is to try them both and profile to see if you need to worry about optimizing this particular aspect of the code.

like image 28
Will A Avatar answered Sep 28 '22 08:09

Will A