How much slower is boost::shared_ptr
#1 Members - Reputation: 281
Posted 23 July 2012 - 09:59 PM
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?
#2 Members - Reputation: 1362
Posted 23 July 2012 - 10:03 PM
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!
#3 Members - Reputation: 629
Posted 23 July 2012 - 10:23 PM
#4 Marketplace Seller - Reputation: 9297
Posted 23 July 2012 - 10:56 PM
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.
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, 23 July 2012 - 10:59 PM.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal
#5 Members - Reputation: 3818
Posted 24 July 2012 - 12:46 AM
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
The voices in my head may not be real, but they have some good ideas!
#6 Moderators - Reputation: 5300
Posted 24 July 2012 - 03:03 AM
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.... if I am just passing the pointer around and calling .get() as a function parameters, is there much overhead in that?
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, 24 July 2012 - 03:08 AM.
#7 Members - Reputation: 1567
Posted 24 July 2012 - 05:32 AM
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.
#8 Members - Reputation: 895
Posted 24 July 2012 - 10:41 AM
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, 24 July 2012 - 10:44 AM.
#9 Members - Reputation: 119
Posted 24 July 2012 - 09:45 PM
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.
#11 Members - Reputation: 1570
Posted 25 July 2012 - 05:20 AM
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 a lot 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.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.
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.
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, 25 July 2012 - 06:16 AM.
#12 Moderators - Reputation: 5300
Posted 25 July 2012 - 06:02 AM
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).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.
But it is certainly worth understanding the trade-off. If you're not using weak_ptr, this isn't a problem.






