From 28dbbe0596878c12e13507aad6b40d33e5a8fa20 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Tue, 16 Nov 2010 21:33:17 +0000 Subject: [PATCH] Dave Zarzycki showed how the efficiency of shared_ptr could be significantly increased. The following program is running 49% faster: #include #include #include #include #include "chrono_io" int main() { typedef std::chrono::high_resolution_clock Clock; Clock::time_point t0 = Clock::now(); { std::shared_ptr p(new int (1)); std::vector > v(1000000, p); v.insert(v.begin(), p); v.insert(v.begin(), p); v.insert(v.begin(), p); v.insert(v.begin(), p); } Clock::time_point t1 = Clock::now(); std::cout << (t1-t0) << '\n'; } git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@119388 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/memory | 2 +- src/memory.cpp | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/memory b/include/memory index c0161546..4752ba00 100644 --- a/include/memory +++ b/include/memory @@ -2535,7 +2535,7 @@ public: : __shared_owners_(__refs) {} void __add_shared(); - void __release_shared(); + bool __release_shared(); _LIBCPP_INLINE_VISIBILITY long use_count() const {return __shared_owners_ + 1;} }; diff --git a/src/memory.cpp b/src/memory.cpp index 9918531a..5bbf48e4 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -50,11 +50,15 @@ __shared_count::__add_shared() increment(__shared_owners_); } -void +bool __shared_count::__release_shared() { if (decrement(__shared_owners_) == -1) + { __on_zero_shared(); + return true; + } + return false; } __shared_weak_count::~__shared_weak_count() @@ -65,7 +69,6 @@ void __shared_weak_count::__add_shared() { __shared_count::__add_shared(); - __add_weak(); } void @@ -77,8 +80,8 @@ __shared_weak_count::__add_weak() void __shared_weak_count::__release_shared() { - __shared_count::__release_shared(); - __release_weak(); + if (__shared_count::__release_shared()) + __release_weak(); } void