Boost logo

Boost-Commit :

From: d.frey_at_[hidden]
Date: 2008-04-22 15:48:40


Author: daniel_frey
Date: 2008-04-22 15:48:39 EDT (Tue, 22 Apr 2008)
New Revision: 44724
URL: http://svn.boost.org/trac/boost/changeset/44724

Log:
Reduce enable_shared_from_this overhead
Text files modified:
   trunk/boost/detail/shared_count.hpp | 12 ++++++++++++
   trunk/boost/enable_shared_from_this.hpp | 19 ++++++++-----------
   trunk/boost/shared_ptr.hpp | 15 +++++++++++++++
   3 files changed, 35 insertions(+), 11 deletions(-)

Modified: trunk/boost/detail/shared_count.hpp
==============================================================================
--- trunk/boost/detail/shared_count.hpp (original)
+++ trunk/boost/detail/shared_count.hpp 2008-04-22 15:48:39 EDT (Tue, 22 Apr 2008)
@@ -220,6 +220,18 @@
         if( pi_ != 0 ) pi_->add_ref_copy();
     }
 
+#if defined( BOOST_HAS_RVALUE_REFS )
+
+ shared_count(shared_count && r): pi_(r.pi_) // nothrow
+#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
+ , id_(shared_count_id)
+#endif
+ {
+ r.pi_ = 0;
+ }
+
+#endif
+
     explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
     shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
 

Modified: trunk/boost/enable_shared_from_this.hpp
==============================================================================
--- trunk/boost/enable_shared_from_this.hpp (original)
+++ trunk/boost/enable_shared_from_this.hpp 2008-04-22 15:48:39 EDT (Tue, 22 Apr 2008)
@@ -13,7 +13,7 @@
 // http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html
 //
 
-#include <boost/weak_ptr.hpp>
+#include <boost/detail/shared_count.hpp>
 #include <boost/shared_ptr.hpp>
 #include <boost/assert.hpp>
 #include <boost/config.hpp>
@@ -32,7 +32,7 @@
             T * p = dynamic_cast<T *>(const_cast<enable_shared_from_this*>(this));
             _internal_shared_this = shared_ptr<T>( p, detail::sp_deleter_wrapper() );
             BOOST_ASSERT(_internal_shared_this.get() == this);
- _internal_weak_this = _internal_shared_this;
+ _internal_weak_count = _internal_shared_this.get_shared_count();
         }
     }
 
@@ -43,7 +43,7 @@
 
     typedef T _internal_element_type; // for bcc 5.5.1
     mutable shared_ptr<_internal_element_type> _internal_shared_this;
- mutable weak_ptr<_internal_element_type> _internal_weak_this;
+ mutable detail::weak_count _internal_weak_count;
     mutable bool _owned;
 
 protected:
@@ -77,17 +77,15 @@
     shared_ptr<T> shared_from_this()
     {
         init_internal_shared_once();
- shared_ptr<T> p(_internal_weak_this);
- BOOST_ASSERT(p.get() == this);
- return p;
+ T * p = dynamic_cast<T *>(this);
+ return shared_ptr<T>( detail::shared_count( _internal_weak_count ), p );
     }
 
     shared_ptr<T const> shared_from_this() const
     {
         init_internal_shared_once();
- shared_ptr<T const> p(_internal_weak_this);
- BOOST_ASSERT(p.get() == this);
- return p;
+ T const * p = dynamic_cast<T const *>(this);
+ return shared_ptr<T const>( detail::shared_count( _internal_weak_count ), p );
     }
 
     template<typename U>
@@ -97,8 +95,7 @@
         {
             if( !_internal_shared_this )
             {
- T * p = dynamic_cast<T *>(const_cast<enable_shared_from_this*>(this));
- _internal_weak_this = shared_ptr<T>(owner, p);
+ _internal_weak_count = owner.get_shared_count();
             }else
             {
                 BOOST_ASSERT(owner.unique()); // no weak_ptrs to owner should exist either, but there's no way to check that

Modified: trunk/boost/shared_ptr.hpp
==============================================================================
--- trunk/boost/shared_ptr.hpp (original)
+++ trunk/boost/shared_ptr.hpp 2008-04-22 15:48:39 EDT (Tue, 22 Apr 2008)
@@ -228,6 +228,11 @@
     {
     }
 
+ template<class Y>
+ shared_ptr(detail::shared_count const & c, Y * p): px(p), pn(c) // never throws
+ {
+ }
+
     // aliasing
     template< class Y >
     shared_ptr( shared_ptr<Y> const & r, T * p ): px( p ), pn( r.pn ) // never throws
@@ -341,6 +346,11 @@
         r.px = 0;
     }
 
+ template<class Y>
+ shared_ptr(detail::shared_count && c, Y * p): px(p), pn( static_cast< detail::shared_count && >( c ) ) // never throws
+ {
+ }
+
     shared_ptr & operator=( shared_ptr && r ) // never throws
     {
         this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
@@ -467,6 +477,11 @@
         pn.swap(other.pn);
     }
 
+ detail::shared_count const & get_shared_count() const // never throws
+ {
+ return pn;
+ }
+
     template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
     {
         return pn < rhs.pn;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk