Sign in to follow this  
wildboar

How much slower is boost::shared_ptr

Recommended Posts

I know that is some overhead when doing lots of new allocations with boost shared_ptr but what if I am just passing the pointer around and calling .get() as a function parameters, is there much overhead in that?
The reason I am asking is that I was given a codebase to work with which is full of shared_ptrs, but I was told to remove them all for normal pointers if I see the need. Can someone please tell me if its worth it?

Share this post


Link to post
Share on other sites
There is some overhead, but it depends on how you're using them. Returning a shared_ptr as a return value involves quite a bit of overhead compared to returning a raw pointer because you have to copy the shared_ptr object and it's internals. Passing shared_ptr as a function parameter should not result in any additional overhead as long as you pass by const reference instead of passing by value which is typically not necessary (and which is something I see people do quite often). The .get method should be optimized away by most compilers, so there is typically no (or very little) overhead there either.

Share this post


Link to post
Share on other sites
For my own project, I found that the very little overhead caused by shared_ptr() was not the bottle neck of my program. You should find the bottleneck by profiling properly, and fix that instead of randomly saying, "[i]Hey is this slow? [/i]*use something more dangerous*[i] What about this? [/i]*throw it out*[i] Maybe this thing? [/i]*write your own slower version*"

The only place I found shared_ptr() to be not worth using, was when allocating over 3000 of them using make_shared() in a tightly controlled loop. It was an issue of me using them improperly, and [i]not[/i] an issue of them being slow. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

No matter how well designed and heavily optimized something is, anything can be slow if used improperly - even normal raw pointers.

Where are the bottlenecks of your program? Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
[quote name='wildboar' timestamp='1343102350' post='4962478']
I know that is some overhead when doing lots of new allocations with boost shared_ptr but what if I am just passing the pointer around and calling .get() as a function parameters, is there much overhead in that?
The reason I am asking is that I was given a codebase to work with which is full of shared_ptrs, but I was told to remove them all for normal pointers if I see the need. Can someone please tell me if its worth it?
[/quote]

shared_ptr is the boost smart pointer with the highest overhead but as long as you don't abuse them you should be fine. (if you need the functionality(shared ownership of data) provided you should use them, if you don't need it you can use scoped_ptr instead

Share this post


Link to post
Share on other sites
[quote]
... if I am just passing the pointer around and calling .get() as a function parameters, is there much overhead in that?
[/quote]
If you are causing the reference count to be modified, yes there is some overhead. If you pass the shared_ptr<> by reference then you have extra indirection accessing the object itself.

However, I would argue that unless the function you are passing the object to is allowed to assert ownership, do not pass a shared_ptr just for semantic reasons. Better to pass a const reference (if null values are not expected), or a raw pointer (if your coding convention supports the idea that a raw pointer passed to a function has no special ownership semantics). Edited by rip-off

Share this post


Link to post
Share on other sites
Worth noting that one of the potential performance issues with shared_ptr is that the ref count has to be allocated on the heap.

Boost also provides intrusive_ptr - same as shared_ptr but the ref count is a static member of the pointed-to class. If you care, and don't mind changing your pointed-to classes, that is.

Share this post


Link to post
Share on other sites
Overhead of which feature?
Since std::shared_ptr is a thread-safe (TS) reference-counted (RC) smart pointer, you can compare to non-thread-safe reference-counted smart pointer to measure TS overhead -- in this case "For the 8 thread version the mean of the two runs differed by a factor of 2.3 in favour of counted_ptr. "
[url="http://nerds-central.blogspot.com/2012/03/sharedptr-performance-issues-and.html"]http://nerds-central...issues-and.html[/url]

If you don't need reference-counting, you should obviously be using std::unique_ptr which doesn't have either overhead:
"For the sorting benchmark demonstrated in this article shared_ptr is about 10% slower than unique_ptr. As for size, while the size of unique_ptr is exactly the size of a raw pointer, shared_ptr is about twice as large."
[url="http://eli.thegreenplace.net/2012/06/20/c11-using-unique_ptr-with-standard-library-containers/"]http://eli.thegreenp...ary-containers/[/url]

"The first question you'd ask yourself is probably: "Why do I need to learn how to use another smart pointer class when shared_ptr fits the bill?" In one word: performance. shared_ptr uses reference counting to allow the sharing of a single resource by multiple objects. Additionally, its destructor and other member functions are virtual. These properties facilitate the customization of shared_ptr. However, the size of a typical shared_ptr object is 40 bytes, which is 10 times bigger than the size of a raw pointer on a 32-bit platform. The presence of virtual member functions means that in many cases, calls to member functions are resolved dynamically, incurring additional runtime overhead. These issues may not concern you if you're using shared_ptr sporadically. However, in time critical apps, or if you have containers that store a large number of shared_ptr objects, the performance overhead might be overwhelming. unique_ptr gives you a safe and reliable smart pointer alternative that can compete with the size and speed of raw pointers."
[url="http://www.codeguru.com/cpp/article.php/c17775/The-Smart-Pointer-That-Makes-Your-C-Applications-Safer--stduniqueptr.htm"]http://www.codeguru....tduniqueptr.htm[/url]

See also: [url="http://www.codesynthesis.com/~boris/blog/2009/01/11/shared-ptr-counter/"]http://www.codesynth...ed-ptr-counter/[/url]

With that said, instead of worrying about overhead it's better to just use the right pointer for the right job :-)

As [url="http://herbsutter.com/gotw/_104/"]http://herbsutter.com/gotw/_104/[/url] states: "unique_ptr by default, or a shared_ptr if ownership is to be shared." Edited by Matt-D

Share this post


Link to post
Share on other sites
[quote name='mrbastard' timestamp='1343129554' post='4962566']
Worth noting that one of the potential performance issues with shared_ptr is that the ref count has to be allocated on the heap.

Boost also provides intrusive_ptr - same as shared_ptr but the ref count is a static member of the pointed-to class. If you care, and don't mind changing your pointed-to classes, that is.
[/quote]

Not necessarily true for modern shared_ptr implementations. If you use make_shared to allocate your object the ref count will be merged with the object allocation. It also does some optimizations to reduce the memory usage of shared_ptr.

Share this post


Link to post
Share on other sites
[quote name='juggler' timestamp='1343187940' post='4962810']
[quote name='mrbastard' timestamp='1343129554' post='4962566']
Worth noting that one of the potential performance issues with shared_ptr is that the ref count has to be allocated on the heap.

Boost also provides intrusive_ptr - same as shared_ptr but the ref count is a static member of the pointed-to class. If you care, and don't mind changing your pointed-to classes, that is.
[/quote]
Not necessarily true for modern shared_ptr implementations. If you use make_shared to allocate your object the ref count will be merged with the object allocation. It also does some optimizations to reduce the memory usage of shared_ptr.
[/quote]
There is one caveat with boost::make_shared worth mentioning: in general, no memory can be freed until the last weak_ptr referencing the object has been released. This is usually completely irrelevant unless you weak reference [b]a lot[/b] of small objects or some really big ones. As said, it does not matter for normal use cases but in extremely special applications you might be better off with separate allocations.

Edit: rip-off added something I only implied instead of saying it outright. Since it is an important but subtle difference, I'm saying it once more using a different choice of words: if you use boost::make_shared to allocate an object of type T, the instance is destroyed (using its destructor) when the last shared_ptr holding it is reset. But the memory occupied (sizeof(T) + sizeof(what shared_ptr needs)) is not freed until the last weak_ptr holding that instance is reset. Edited by BitMaster

Share this post


Link to post
Share on other sites
[quote]
There is one caveat with boost::make_shared worth mentioning: in general, no memory can be freed until the last weak_ptr referencing the object has been released.
[/quote]
To be specific (and perhaps pedantic), the way make_shared() works is by allocating a contiguous chunk of memory for the reference count and the object data. Thus we need to wait until the reference count can be deallocate (last weak_ptr) to deallocate the entire chunk. Thus if you have a large object (e.g. containing an internal array) this could be significant. However, if the object itself allocates heap memory (e.g. contains a std::vector<T>) then this data will be deallocated when the object is logically deallocated (last shared_ptr).

But it is certainly worth understanding the trade-off. If you're not using weak_ptr, this isn't a problem.

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