Boost logo

Boost :

From: Yuval Ronen (ronen_yuval_at_[hidden])
Date: 2004-06-21 10:52:19


Hi,
I'm new to boost, so this post might seem silly to some of you, and I
apologize if this is the case.

I have a tiny performance suggestion:

The c'tor

template<class Y>
shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag):
px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
{
    if(px == 0) // need to allocate new counter -- the cast failed
    {
        pn = detail::shared_count();
    }
}

should be changed to:

template<class Y>
shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag):
px(dynamic_cast<element_type *>(r.px)), pn(px ? r.pn :
detail::shared_count())
{
}

The difference is when the dynamic_cast fails and returns NULL. In such
cases the existing code copies the shared_count object, including mutex
locking, only to discover later that it was unnecessary (the 'if(px == 0)'
statement). The suggested code saves this copy of the shared_count object,
which also saves some mutex lockings.

Hope I'm not talking nonsense,
Yuval


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk