• Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

The sound of premature optimization

Sign in to follow this  


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  


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