[libcxx-commits] [PATCH] D80057: [libc++][NFCI] Optimization to std::~unique_ptr

Atmn Patel via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri May 15 21:11:28 PDT 2020


atmnpatel created this revision.
atmnpatel added a reviewer: ldionne.
Herald added subscribers: libcxx-commits, dexonsmith.
Herald added a project: libc++.
Herald added a reviewer: libc++.

The current implementation of std::~unique_ptr modifies the address held
by its ptr. This fix changes it so that it only calls the contained
destructor as suggested in the bug report.

Fixes https://llvm.org/PR45848


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80057

Files:
  libcxx/include/memory


Index: libcxx/include/memory
===================================================================
--- libcxx/include/memory
+++ libcxx/include/memory
@@ -2641,7 +2641,11 @@
 
 
   _LIBCPP_INLINE_VISIBILITY
-  ~unique_ptr() { reset(); }
+  ~unique_ptr() {
+    const pointer ptr = __ptr_.first();
+    if (ptr)
+        __ptr_.second()(ptr);
+  }
 
   _LIBCPP_INLINE_VISIBILITY
   unique_ptr& operator=(nullptr_t) _NOEXCEPT {
@@ -2864,7 +2868,11 @@
 
 public:
   _LIBCPP_INLINE_VISIBILITY
-  ~unique_ptr() { reset(); }
+  ~unique_ptr() {
+    const pointer ptr = __ptr_.first();
+    if (ptr)
+        __ptr_.second()(ptr);
+  }
 
   _LIBCPP_INLINE_VISIBILITY
   unique_ptr& operator=(nullptr_t) _NOEXCEPT {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80057.264412.patch
Type: text/x-patch
Size: 706 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200516/4807f00b/attachment.bin>


More information about the libcxx-commits mailing list