• Advertisement
Sign in to follow this  

How much slower is boost::shared_ptr

This topic is 2099 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

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
Advertisement
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, "Hey is this slow? *use something more dangerous* What about this? *throw it out* Maybe this thing? *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 not an issue of them being slow. wink.png

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

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?


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

... 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. "
http://nerds-central...issues-and.html

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."
http://eli.thegreenp...ary-containers/

"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."
http://www.codeguru....tduniqueptr.htm

See also: http://www.codesynth...ed-ptr-counter/

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

As http://herbsutter.com/gotw/_104/ 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

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.


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
Sign in to follow this  

  • Advertisement