[llvm-bugs] [Bug 45848] New: std::~unique_ptr is inefficient

via llvm-bugs llvm-bugs at lists.llvm.org
Fri May 8 11:18:33 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=45848

            Bug ID: 45848
           Summary: std::~unique_ptr is inefficient
           Product: libc++
           Version: unspecified
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: jsbache at adobe.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

The dtor of unique_ptr is implemented in terms of reset (which defaults to a
set of a nullptr).
  ~unique_ptr() { reset(); }

This is inefficient as the effect is that the dtor modifies the address held by
its ptr
    __ptr_.first() = __p;
This causes address invalidation (and a related cacheline invalidation).

Since this is in the dtor, noone should read that value again.

~unique_ptr should be implemented in terms of the contained destructor only:
~unique_ptr() {
    const pointer ptr = __ptr_.first();
    if (ptr)
        __ptr_.second()(ptr);
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200508/f0f2b8c4/attachment.html>


More information about the llvm-bugs mailing list