Jump to content

  • Log In with Google      Sign In   
  • Create Account


How much slower is boost::shared_ptr


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 wildboar   Members   -  Reputation: 281

Like
0Likes
Like

Posted 23 July 2012 - 09:59 PM

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?

Sponsor:

#2 nobodynews   Crossbones+   -  Reputation: 1760

Like
2Likes
Like

Posted 23 July 2012 - 10:03 PM

You should look into profiling

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


#3 krippy2k8   Members   -  Reputation: 642

Like
1Likes
Like

Posted 23 July 2012 - 10:23 PM

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.

#4 Servant of the Lord   Crossbones+   -  Reputation: 17060

Like
4Likes
Like

Posted 23 July 2012 - 10:56 PM

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

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.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

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.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#5 SimonForsman   Crossbones+   -  Reputation: 5753

Like
1Likes
Like

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
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#6 rip-off   Moderators   -  Reputation: 7642

Like
1Likes
Like

Posted 24 July 2012 - 03:03 AM

... if I am just passing the pointer around and calling .get() as a function parameters, is there much overhead in that?

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, 24 July 2012 - 03:08 AM.


#7 mrbastard   Members   -  Reputation: 1573

Like
0Likes
Like

Posted 24 July 2012 - 05:32 AM

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.


#8 Matt-D   Crossbones+   -  Reputation: 1410

Like
1Likes
Like

Posted 24 July 2012 - 10:41 AM

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, 24 July 2012 - 10:44 AM.


#9 juggler   Members   -  Reputation: 119

Like
2Likes
Like

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.

#10 mrbastard   Members   -  Reputation: 1573

Like
0Likes
Like

Posted 25 July 2012 - 01:55 AM

I wasn't aware boost provided make_shared, thanks for correcting me. :)


#11 BitMaster   Crossbones+   -  Reputation: 3659

Like
0Likes
Like

Posted 25 July 2012 - 05:20 AM


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.

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.

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 rip-off   Moderators   -  Reputation: 7642

Like
1Likes
Like

Posted 25 July 2012 - 06:02 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.

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS