Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    195
  • comments
    198
  • views
    104377

The sound of premature optimization

Sign in to follow this  
SiCrane

84 views

Played some with smart pointers today. Specifically, mucking around with the guts of boost::shared_ptr. One of the interesting implementation details of boost::shared_ptr is that it can accept a deleter object as a parameter when constructing a shared_ptr. This gives shared_ptr quite a bit of flexibility. The implementation of boost::shared_ptr goes like:

shared_ptr contains two members: a pointer to the object and reference counter object. The reference counter object contains a pointer to a the shared count implementation. The implementation object is polymorphic object contains as members, another copy of the pointer to the object, a copy of the deleter object, and two counters, one for shared_ptr references and one for weak_ptr references. And, as a polymorhpic object, it also has the vtable pointer.

My current experiment differs from shared_ptr by having the shared count object be non-polymorphic. Instead of containing a copy of the deleter object, it contains a pointer to a statically allocated polymorphic object that contains a deleter object. So with the loss of some flexiblity (being able to have deleter objects with state), the size of the object allocated on the heap drops by a little. It seems like it goes from 20 bytes to 16 bytes when I tried it out. While 4 bytes isn't that big a deal, it does drop it down to a power of two, which makes it suitable for use with a binary buddy suballocator.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

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
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!