[cfe-commits] [libcxx] r119388 - in /libcxx/trunk: include/memory src/memory.cpp

Howard Hinnant hhinnant at apple.com
Tue Nov 16 13:33:18 PST 2010


Author: hhinnant
Date: Tue Nov 16 15:33:17 2010
New Revision: 119388

URL: http://llvm.org/viewvc/llvm-project?rev=119388&view=rev
Log:
Dave Zarzycki showed how the efficiency of shared_ptr could be significantly
increased.  The following program is running 49% faster:

#include <iostream>
#include <memory>
#include <chrono>
#include <vector>
#include "chrono_io"

int main()
{
    typedef std::chrono::high_resolution_clock Clock;
    Clock::time_point t0 = Clock::now();
    {
        std::shared_ptr<int> p(new int (1));
        std::vector<std::shared_ptr<int> > 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';
}

Modified:
    libcxx/trunk/include/memory
    libcxx/trunk/src/memory.cpp

Modified: libcxx/trunk/include/memory
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/memory?rev=119388&r1=119387&r2=119388&view=diff
==============================================================================
--- libcxx/trunk/include/memory (original)
+++ libcxx/trunk/include/memory Tue Nov 16 15:33:17 2010
@@ -2535,7 +2535,7 @@
         : __shared_owners_(__refs) {}
 
     void __add_shared();
-    void __release_shared();
+    bool __release_shared();
     _LIBCPP_INLINE_VISIBILITY
     long use_count() const {return __shared_owners_ + 1;}
 };

Modified: libcxx/trunk/src/memory.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/memory.cpp?rev=119388&r1=119387&r2=119388&view=diff
==============================================================================
--- libcxx/trunk/src/memory.cpp (original)
+++ libcxx/trunk/src/memory.cpp Tue Nov 16 15:33:17 2010
@@ -50,11 +50,15 @@
     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 @@
 __shared_weak_count::__add_shared()
 {
     __shared_count::__add_shared();
-    __add_weak();
 }
 
 void
@@ -77,8 +80,8 @@
 void
 __shared_weak_count::__release_shared()
 {
-    __shared_count::__release_shared();
-    __release_weak();
+    if (__shared_count::__release_shared())
+        __release_weak();
 }
 
 void





More information about the cfe-commits mailing list