Sign in to follow this  
jamesleighe

Optimizing Return Values

Recommended Posts

Lets say I had:
[code]
class Extents
{
Vector3D mins;
Vector3D maxs;
};

class IPhysicsObject
{
// return the mins and maxs of an aabb that completely surrounds the object
virtual const Extents& GetExtents( ) = 0;
};
[/code]

Lets say that worked fine for 90% of physics objects, and returning a const reference allows the compiler to do some really nice optimization.

However, lets say I had an object, like an AABB, that inherited from IPhysicsObject.
It is so fast to compute it's extents (mins and maxs) from it's origin and half sizes that it dosen't make sense to store it in memory, but rather recompute it when it's asked for.

So I'll have something like:
[code]

FORCE_INLINE const Extents3D&
AABox::GetExtents( )
{
Extents3D extents;

extents.mins.x = GetTranslationX() - halfSizes_.x;
extents.mins.y = GetTranslationY() - halfSizes_.y;
extents.mins.z = GetTranslationZ() - halfSizes_.z;

extents.maxs.x = GetTranslationX() + halfSizes_.x;
extents.maxs.y = GetTranslationY() + halfSizes_.y;
extents.maxs.z = GetTranslationZ() + halfSizes_.z;

return extents;
}
[/code]

Which is of course bad since I'm now returning the address of a temporary!


My question is, am I better off:
A) Saving off everything into memory even when it's actually faster to recompute it (+ it uses less storage space, allot less in some cases).
OR
B) Making the return value pass by value instead of reference, losing some performance in a hotspot (physics).

I'm hoping there is a 'C' option or something, or maybe it's not that much slower to pass that by reference after all the optimization is done by the compiler. But it wont be as fast for sure.


Thanks as usual guys!

Share this post


Link to post
Share on other sites
First of all, don't return the address of a local variable, no matter what.

Write the code as cleanly as you can: probably returning by value in that function would be a good start. If later on it turns out your code is too slow and a profiler shows that you are spending significant time copying the return value of that function, then you can consider how to make it faster (e.g., the caller passes a reference to an object that gets filled out by the function). It is very possible that the compiler will not copy the value at all, because it is allowed to optimize that away.

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1333808226' post='4929036']
First of all, don't return the address of a local variable, no matter what.

Write the code as cleanly as you can: probably returning by value in that function would be a good start. If later on it turns out your code is too slow and a profiler shows that you are spending significant time copying the return value of that function, then you can consider how to make it faster (e.g., the caller passes a reference to an object that gets filled out by the function). It is very possible that the compiler will not copy the value at all, because it is allowed to optimize that away.
[/quote]

Originally I had the code written so that I passed in a reference that was written to by the object like you said, but I found most of the time the compiler treated that the same as return by value after optimization so I dropped doing that because using a return value was almost always cleaner.

Why shoulden't I return a const reference ever? Isn't that the ideal performance wise and const allows some control in that it's not supposed to be modified (but I guess you could cast it away)? In this example I'll just return by value, but for future reference.

Share this post


Link to post
Share on other sites
[quote name='James Leighe' timestamp='1333808897' post='4929039']
Why shoulden't I return a const reference ever?
[/quote]

That's not what alvaro said. You shouldn't return the address of (or a reference to) a local variable.

Share this post


Link to post
Share on other sites
Right, that's what I meant.

Why not assuming it's a const reference?

Edit:
Wait do you mean local as in private or local as in temporary?

I know about the temporary part.

Share this post


Link to post
Share on other sites
I mean "local" as in [url="http://en.wikipedia.org/wiki/Local_variable"]local[/url]. "Temporary" means [url="http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr382.htm"]something else[/url]. Returning a const reference to private member data is OK as long as you document that behavior, so the caller knows that the answer is only valid as long as the object is not modified or destroyed.

Share this post


Link to post
Share on other sites
Return by value here, and use the automatic Named Return Value Optimization support in your compiler, or use move semantics.

NRVO will catch this in most compilers, and in C++11 with move semantics, you can eliminate the copy entirely.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this