Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81608 - in trunk/boost/smart_ptr: . detail
From: glenfe_at_[hidden]
Date: 2012-11-28 01:07:46


Author: glenfe
Date: 2012-11-28 01:07:45 EST (Wed, 28 Nov 2012)
New Revision: 81608
URL: http://svn.boost.org/trac/boost/changeset/81608

Log:
Optimization: Add specializations of make_array_helper, allocate_array_helper, and array_deleter for fixed size arrays to avoid storing size.

Text files modified:
   trunk/boost/smart_ptr/allocate_shared_array.hpp | 48 +++++++++++++-----------
   trunk/boost/smart_ptr/detail/allocate_array_helper.hpp | 79 +++++++++++++++++++++++++++++++++++++--
   trunk/boost/smart_ptr/detail/array_deleter.hpp | 63 +++++++++++++++++++++++++++++++
   trunk/boost/smart_ptr/detail/make_array_helper.hpp | 72 ++++++++++++++++++++++++++++++++++--
   trunk/boost/smart_ptr/make_shared_array.hpp | 66 ++++++++++++++++++---------------
   5 files changed, 267 insertions(+), 61 deletions(-)

Modified: trunk/boost/smart_ptr/allocate_shared_array.hpp
==============================================================================
--- trunk/boost/smart_ptr/allocate_shared_array.hpp (original)
+++ trunk/boost/smart_ptr/allocate_shared_array.hpp 2012-11-28 01:07:45 EST (Wed, 28 Nov 2012)
@@ -62,14 +62,16 @@
         typedef typename boost::detail::array_base<T1>::type T2;
         T1* p1 = 0;
         T2* p2 = 0;
- std::size_t n1 = boost::detail::array_total<T>::size;
- boost::detail::allocate_array_helper<A, T2> a1(allocator, n1, &p2);
- boost::detail::array_deleter<T2> d1;
+ enum {
+ N = boost::detail::array_total<T>::size
+ };
+ boost::detail::allocate_array_helper<A, T2[N]> a1(allocator, &p2);
+ boost::detail::array_deleter<T2[N]> d1;
         boost::shared_ptr<T> s1(p1, d1, a1);
- boost::detail::array_deleter<T2>* d2;
+ boost::detail::array_deleter<T2[N]>* d2;
         p1 = reinterpret_cast<T1*>(p2);
- d2 = get_deleter<boost::detail::array_deleter<T2> >(s1);
- d2->construct(p2, n1, boost::detail::sp_forward<Args>(args)...);
+ d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
+ d2->construct(p2, boost::detail::sp_forward<Args>(args)...);
         return boost::shared_ptr<T>(s1, p1);
     }
 #endif
@@ -106,15 +108,17 @@
         T1* p1 = 0;
         T2* p2 = 0;
         T3* p3 = 0;
- std::size_t n1 = boost::detail::array_total<T>::size;
- boost::detail::allocate_array_helper<A, T2> a1(allocator, n1, &p2);
- boost::detail::array_deleter<T2> d1;
+ enum {
+ N = boost::detail::array_total<T>::size
+ };
+ boost::detail::allocate_array_helper<A, T2[N]> a1(allocator, &p2);
+ boost::detail::array_deleter<T2[N]> d1;
         boost::shared_ptr<T> s1(p1, d1, a1);
- boost::detail::array_deleter<T2>* d2;
+ boost::detail::array_deleter<T2[N]>* d2;
         p3 = reinterpret_cast<T3*>(list.begin());
         p1 = reinterpret_cast<T1*>(p2);
- d2 = get_deleter<boost::detail::array_deleter<T2> >(s1);
- d2->construct_list(p2, n1, p3);
+ d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
+ d2->construct_list(p2, p3);
         return boost::shared_ptr<T>(s1, p1);
     }
     template<typename T, typename A>
@@ -127,8 +131,8 @@
         T1* p1 = 0;
         T2* p2 = 0;
         T3* p3 = 0;
- std::size_t n0 = boost::detail::array_total<T1>::size;
- std::size_t n1 = n0 * list.size();
+ enum { M = boost::detail::array_total<T1>::size };
+ std::size_t n1 = M * list.size();
         boost::detail::allocate_array_helper<A, T2> a1(allocator, n1, &p2);
         boost::detail::array_deleter<T2> d1;
         boost::shared_ptr<T> s1(p1, d1, a1);
@@ -136,7 +140,7 @@
         p3 = reinterpret_cast<T3*>(list.begin());
         p1 = reinterpret_cast<T1*>(p2);
         d2 = get_deleter<boost::detail::array_deleter<T2> >(s1);
- d2->construct_list(p2, n1, p3, n0);
+ d2->construct_list(p2, n1, p3, M);
         return boost::shared_ptr<T>(s1, p1);
     }
     template<typename T, typename A>
@@ -150,16 +154,16 @@
         T1* p1 = 0;
         T2* p2 = 0;
         T3* p3 = 0;
- std::size_t n0 = boost::detail::array_total<T1>::size;
- std::size_t n1 = boost::detail::array_total<T>::size;
- boost::detail::allocate_array_helper<A, T2> a1(allocator, n1, &p2);
- boost::detail::array_deleter<T2> d1;
+ enum { M = boost::detail::array_total<T1>::size };
+ enum { N = boost::detail::array_total<T>::size };
+ boost::detail::allocate_array_helper<A, T2[N]> a1(allocator, &p2);
+ boost::detail::array_deleter<T2[N]> d1;
         boost::shared_ptr<T> s1(p1, d1, a1);
- boost::detail::array_deleter<T2>* d2;
+ boost::detail::array_deleter<T2[N]>* d2;
         p3 = reinterpret_cast<T3*>(list.begin());
         p1 = reinterpret_cast<T1*>(p2);
- d2 = get_deleter<boost::detail::array_deleter<T2> >(s1);
- d2->construct_list(p2, n1, p3, n0);
+ d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
+ d2->construct_list(p2, p3, M);
         return boost::shared_ptr<T>(s1, p1);
     }
 #endif

Modified: trunk/boost/smart_ptr/detail/allocate_array_helper.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/allocate_array_helper.hpp (original)
+++ trunk/boost/smart_ptr/detail/allocate_array_helper.hpp 2012-11-28 01:07:45 EST (Wed, 28 Nov 2012)
@@ -36,11 +36,6 @@
                   size(sizeof(T) * size),
                   data(data) {
             }
- allocate_array_helper(const allocate_array_helper& other)
- : allocator(other.allocator),
- size(other.size),
- data(other.data) {
- }
             template<class U>
             allocate_array_helper(const allocate_array_helper<A, T, U>& other)
                 : allocator(other.allocator),
@@ -92,6 +87,80 @@
             std::size_t size;
             T** data;
         };
+ template<typename A, typename T, std::size_t N, typename Y>
+ class allocate_array_helper<A, T[N], Y> {
+ template<typename A9, typename T9, typename Y9>
+ friend class allocate_array_helper;
+ typedef typename A::template rebind<Y> ::other A2;
+ typedef typename A::template rebind<char>::other A3;
+ public:
+ typedef typename A2::value_type value_type;
+ typedef typename A2::pointer pointer;
+ typedef typename A2::const_pointer const_pointer;
+ typedef typename A2::reference reference;
+ typedef typename A2::const_reference const_reference;
+ typedef typename A2::size_type size_type;
+ typedef typename A2::difference_type difference_type;
+ template<typename U>
+ struct rebind {
+ typedef allocate_array_helper<A, T[N], U> other;
+ };
+ allocate_array_helper(const A& allocator, T** data)
+ : allocator(allocator),
+ data(data) {
+ }
+ template<class U>
+ allocate_array_helper(const allocate_array_helper<A, T[N], U>& other)
+ : allocator(other.allocator),
+ data(other.data) {
+ }
+ pointer address(reference value) const {
+ return allocator.address(value);
+ }
+ const_pointer address(const_reference value) const {
+ return allocator.address(value);
+ }
+ size_type max_size() const {
+ return allocator.max_size();
+ }
+ pointer allocate(size_type count, const void* value = 0) {
+ std::size_t a1 = boost::alignment_of<T>::value;
+ std::size_t n1 = count * sizeof(Y) + a1 - 1;
+ char* p1 = A3(allocator).allocate(n1 + N1, value);
+ char* p2 = p1 + n1;
+ while (std::size_t(p2) % a1 != 0) {
+ p2--;
+ }
+ *data = reinterpret_cast<T*>(p2);
+ return reinterpret_cast<Y*>(p1);
+ }
+ void deallocate(pointer memory, size_type count) {
+ std::size_t a1 = boost::alignment_of<T>::value;
+ std::size_t n1 = count * sizeof(Y) + a1 - 1;
+ char* p1 = reinterpret_cast<char*>(memory);
+ A3(allocator).deallocate(p1, n1 + N1);
+ }
+ void construct(pointer memory, const Y& value) {
+ allocator.construct(memory, value);
+ }
+ void destroy(pointer memory) {
+ allocator.destroy(memory);
+ }
+ template<typename U>
+ bool operator==(const allocate_array_helper<A, T[N], U>& other) const {
+ return allocator == other.allocator;
+ }
+ template<typename U>
+ bool operator!=(const allocate_array_helper<A, T[N], U>& other) const {
+ return !(*this == other);
+ }
+ private:
+ enum {
+ N1 = N * sizeof(T)
+ };
+ A2 allocator;
+ T** data;
+ };
     }
 }
 

Modified: trunk/boost/smart_ptr/detail/array_deleter.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/array_deleter.hpp (original)
+++ trunk/boost/smart_ptr/detail/array_deleter.hpp 2012-11-28 01:07:45 EST (Wed, 28 Nov 2012)
@@ -70,6 +70,69 @@
             std::size_t size;
             T* object;
         };
+ template<typename T, std::size_t N>
+ class array_deleter<T[N]> {
+ public:
+ array_deleter()
+ : object(0) {
+ }
+ ~array_deleter() {
+ destroy();
+ }
+ void construct(T* memory) {
+ object = memory;
+ for (std::size_t i = 0; i < N; i++) {
+ void* p1 = object + i;
+ ::new(p1) T();
+ }
+ }
+#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
+ template<typename... Args>
+ void construct(T* memory, Args&&... args) {
+ object = memory;
+ for (std::size_t i = 0; i < N; i++) {
+ void* p1 = object + i;
+ ::new(p1) T(args...);
+ }
+ }
+#endif
+#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
+ void construct_list(T* memory, const T* list) {
+ object = memory;
+ for (std::size_t i = 0; i < N; i++) {
+ void* p1 = object + i;
+ ::new(p1) T(list[i]);
+ }
+ }
+ void construct_list(T* memory, const T* list, std::size_t n) {
+ object = memory;
+ for (std::size_t i = 0; i < N; i++) {
+ void* p1 = object + i;
+ ::new(p1) T(list[i % n]);
+ }
+ }
+#endif
+ void construct_noinit(T* memory) {
+ object = memory;
+ for (std::size_t i = 0; i < N; i++) {
+ void* p1 = object + i;
+ ::new(p1) T;
+ }
+ }
+ void operator()(const void*) {
+ destroy();
+ }
+ private:
+ void destroy() {
+ if (object) {
+ for (std::size_t i = N; i > 0; ) {
+ object[--i].~T();
+ }
+ object = 0;
+ }
+ }
+ T* object;
+ };
     }
 }
 

Modified: trunk/boost/smart_ptr/detail/make_array_helper.hpp
==============================================================================
--- trunk/boost/smart_ptr/detail/make_array_helper.hpp (original)
+++ trunk/boost/smart_ptr/detail/make_array_helper.hpp 2012-11-28 01:07:45 EST (Wed, 28 Nov 2012)
@@ -33,10 +33,6 @@
                 : size(sizeof(T) * size),
                   data(data) {
             }
- make_array_helper(const make_array_helper& other)
- : size(other.size),
- data(other.data) {
- }
             template<class U>
             make_array_helper(const make_array_helper<T, U>& other)
                 : size(other.size),
@@ -85,6 +81,74 @@
             std::size_t size;
             T** data;
         };
+ template<typename T, std::size_t N, typename Y>
+ class make_array_helper<T[N], Y> {
+ template<typename T2, typename Y2>
+ friend class make_array_helper;
+ public:
+ typedef Y value_type;
+ typedef Y* pointer;
+ typedef const Y* const_pointer;
+ typedef Y& reference;
+ typedef const Y& const_reference;
+ typedef std::size_t size_type;
+ typedef ptrdiff_t difference_type;
+ template<typename U>
+ struct rebind {
+ typedef make_array_helper<T[N], U> other;
+ };
+ make_array_helper(T** data)
+ : data(data) {
+ }
+ template<class U>
+ make_array_helper(const make_array_helper<T[N], U>& other)
+ : data(other.data) {
+ }
+ pointer address(reference value) const {
+ return &value;
+ }
+ const_pointer address(const_reference value) const {
+ return &value;
+ }
+ size_type max_size() const {
+ return static_cast<std::size_t>(-1) / sizeof(Y);
+ }
+ pointer allocate(size_type count, const void* = 0) {
+ std::size_t a1 = boost::alignment_of<T>::value;
+ std::size_t n1 = count * sizeof(Y) + a1 - 1;
+ void* p1 = ::operator new(n1 + N1);
+ char* p2 = static_cast<char*>(p1) + n1;
+ while (std::size_t(p2) % a1 != 0) {
+ p2--;
+ }
+ *data = reinterpret_cast<T*>(p2);
+ return reinterpret_cast<Y*>(p1);
+ }
+ void deallocate(pointer memory, size_type) {
+ void* p1 = memory;
+ ::operator delete(p1);
+ }
+ void construct(pointer memory, const Y& value) {
+ void* p1 = memory;
+ ::new(p1) Y(value);
+ }
+ void destroy(pointer memory) {
+ memory->~Y();
+ }
+ template<typename U>
+ bool operator==(const make_array_helper<T[N], U>& other) const {
+ return true;
+ }
+ template<typename U>
+ bool operator!=(const make_array_helper<T[N], U>& other) const {
+ return !(*this == other);
+ }
+ private:
+ enum {
+ N1 = N * sizeof(T)
+ };
+ T** data;
+ };
     }
 }
 

Modified: trunk/boost/smart_ptr/make_shared_array.hpp
==============================================================================
--- trunk/boost/smart_ptr/make_shared_array.hpp (original)
+++ trunk/boost/smart_ptr/make_shared_array.hpp 2012-11-28 01:07:45 EST (Wed, 28 Nov 2012)
@@ -62,14 +62,16 @@
         typedef typename boost::detail::array_base<T1>::type T2;
         T1* p1 = 0;
         T2* p2 = 0;
- std::size_t n1 = boost::detail::array_total<T>::size;
- boost::detail::make_array_helper<T2> a1(n1, &p2);
- boost::detail::array_deleter<T2> d1;
+ enum {
+ N = boost::detail::array_total<T>::size
+ };
+ boost::detail::make_array_helper<T2[N]> a1(&p2);
+ boost::detail::array_deleter<T2[N]> d1;
         boost::shared_ptr<T> s1(p1, d1, a1);
- boost::detail::array_deleter<T2>* d2;
+ boost::detail::array_deleter<T2[N]>* d2;
         p1 = reinterpret_cast<T1*>(p2);
- d2 = get_deleter<boost::detail::array_deleter<T2> >(s1);
- d2->construct(p2, n1, boost::detail::sp_forward<Args>(args)...);
+ d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
+ d2->construct(p2, boost::detail::sp_forward<Args>(args)...);
         return boost::shared_ptr<T>(s1, p1);
     }
 #endif
@@ -104,15 +106,17 @@
         T1* p1 = 0;
         T2* p2 = 0;
         T3* p3 = 0;
- std::size_t n1 = boost::detail::array_total<T>::size;
- boost::detail::make_array_helper<T2> a1(n1, &p2);
- boost::detail::array_deleter<T2> d1;
+ enum {
+ N = boost::detail::array_total<T>::size
+ };
+ boost::detail::make_array_helper<T2[N]> a1(&p2);
+ boost::detail::array_deleter<T2[N]> d1;
         boost::shared_ptr<T> s1(p1, d1, a1);
- boost::detail::array_deleter<T2>* d2;
+ boost::detail::array_deleter<T2[N]>* d2;
         p3 = reinterpret_cast<T3*>(list.begin());
         p1 = reinterpret_cast<T1*>(p2);
- d2 = get_deleter<boost::detail::array_deleter<T2> >(s1);
- d2->construct_list(p2, n1, p3);
+ d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
+ d2->construct_list(p2, p3);
         return boost::shared_ptr<T>(s1, p1);
     }
     template<typename T>
@@ -125,8 +129,8 @@
         T1* p1 = 0;
         T2* p2 = 0;
         T3* p3 = 0;
- std::size_t n0 = boost::detail::array_total<T1>::size;
- std::size_t n1 = n0 * size;
+ enum { M = boost::detail::array_total<T1>::size };
+ std::size_t n1 = M * size;
         boost::detail::make_array_helper<T2> a1(n1, &p2);
         boost::detail::array_deleter<T2> d1;
         boost::shared_ptr<T> s1(p1, d1, a1);
@@ -134,7 +138,7 @@
         p3 = reinterpret_cast<T3*>(list.begin());
         p1 = reinterpret_cast<T1*>(p2);
         d2 = get_deleter<boost::detail::array_deleter<T2> >(s1);
- d2->construct_list(p2, n1, p3, n0);
+ d2->construct_list(p2, n1, p3, M);
         return boost::shared_ptr<T>(s1, p1);
     }
     template<typename T>
@@ -147,16 +151,16 @@
         T1* p1 = 0;
         T2* p2 = 0;
         T3* p3 = 0;
- std::size_t n0 = boost::detail::array_total<T1>::size;
- std::size_t n1 = boost::detail::array_total<T>::size;
- boost::detail::make_array_helper<T2> a1(n1, &p2);
- boost::detail::array_deleter<T2> d1;
+ enum { M = boost::detail::array_total<T1>::size };
+ enum { N = boost::detail::array_total<T>::size };
+ boost::detail::make_array_helper<T2[N]> a1(&p2);
+ boost::detail::array_deleter<T2[N]> d1;
         boost::shared_ptr<T> s1(p1, d1, a1);
- boost::detail::array_deleter<T2>* d2;
+ boost::detail::array_deleter<T2[N]>* d2;
         p3 = reinterpret_cast<T3*>(list.begin());
         p1 = reinterpret_cast<T1*>(p2);
- d2 = get_deleter<boost::detail::array_deleter<T2> >(s1);
- d2->construct_list(p2, n1, p3, n0);
+ d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
+ d2->construct_list(p2, p3, M);
         return boost::shared_ptr<T>(s1, p1);
     }
 #endif
@@ -184,14 +188,16 @@
         typedef typename boost::detail::array_base<T1>::type T2;
         T1* p1 = 0;
         T2* p2 = 0;
- std::size_t n1 = boost::detail::array_total<T>::size;
- boost::detail::make_array_helper<T2> a1(n1, &p2);
- boost::detail::array_deleter<T2> d1;
- boost::shared_ptr<T> s1(p1, d1, a1);
- boost::detail::array_deleter<T2>* d2;
- p1 = reinterpret_cast<T1*>(p2);
- d2 = get_deleter<boost::detail::array_deleter<T2> >(s1);
- d2->construct_noinit(p2, n1);
+ enum {
+ N = boost::detail::array_total<T>::size
+ };
+ boost::detail::make_array_helper<T2[N]> a1(&p2);
+ boost::detail::array_deleter<T2[N]> d1;
+ boost::shared_ptr<T> s1(p1, d1, a1);
+ boost::detail::array_deleter<T2[N]>* d2;
+ p1 = reinterpret_cast<T1*>(p2);
+ d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
+ d2->construct_noinit(p2);
         return boost::shared_ptr<T>(s1, p1);
     }
 }


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