Sign in to follow this  

Reference dilemma

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Today, I came up with an interesting dilemma regarding C++ references which I would like to share with you. OK, everybody knows that references (and pointers for that matter) are great methods to avoid copying big objects around and passing objects by reference, both as function arguments and return values when appropriate, is a great way to save up memory bandwidth and CPU cycles (since there is no DMA style memory access involved here and copying objects requires a CPU loop to first read memory contents into registers and then copying them back to other memory locations which involves two data bus crossings plus some CPU cycles). References are not magical solutions though and like all other solutions in the world of computer science suffer from the same well-known memory vs. processing power trade offs. What's usually ignored herein, is the effect of pointer dereferences that are paid for ever single time those objects are accessed via a reference or pointer. This doesn't seem to be a problem though especially when the desired pattern of access is to manipulate the objects a few couple of times, in which case the cost of dereferencings is negligible compared to the cost of the copying required if the whole object was to be moved around to avoid such dereferences. But what about such situations where the object is accessed a thousand times in a loop? Will this not cause poor cache performance? Seems to me references are not always the dominant option then. What do you think?

Share this post


Link to post
Share on other sites
I can only think of one case where it is preferable to not use references/pointers for performance reasons.

When passing basic types as arguments to functions it is preferable to pass them by value because when you pass a basic type this way it is likely to be placed directly into a CPU register.

However AFAIK passing arguments by reference/pointer vs. value doesn't have any effect on CPU cache coherency.

Share this post


Link to post
Share on other sites
References are just a syntactic flavor. Pointers and references are the same concept with different syntax so references are prone to the same cache performance problems (if any!?) of pointers. Anyone?

Share this post


Link to post
Share on other sites
Quote:
But what about such situations where the object is accessed a thousand times in a loop?


Try tens of millions accesses per second.

Quote:
I can only think of one case where it is preferable to not use references/pointers for performance reasons.


Compiler will commonly do the smart thing, and pass by register/value.

Quote:
Will this not cause poor cache performance?


References are commonly used with stack-based objects. The relative positions in memory will often be close. Compiler will take that into account (look into generated assembly).

If an object is allocated on heap, then it doesn't matter anyway - that is not something related to references.

Quote:
in which case the cost of dereferencings is negligible compared to the cost of the copying required if the whole object was to be moved around to avoid such dereferences.


Copying any non-trivial object tens of millions of times per second will be worse. You will hit cache penalty for each copy.

Quote:
Seems to me references are not always the dominant option then. What do you think?


Design, then profile. A good design will leverage data locality vs. manageability.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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