• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
wildboar

How much slower is boost::shared_ptr

11 posts in this topic

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?
0

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.
1

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
1

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
1

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.
0

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
1

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.
2

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
0

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.
1

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  
Followers 0