Optimizing Return Values

This topic is 2442 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

 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; }; 

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:
 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; } 

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 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 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.

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 on other sites

Why shoulden't I return a const reference ever?

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

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 on other sites
I mean "local" as in local. "Temporary" means something else. 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.

Tanks

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.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633710
• Total Posts
3013482
×