https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102221

            Bug ID: 102221
           Summary: Missed optimizations for algorithms over
                    std::unique_ptr
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dangelog at gmail dot com
  Target Milestone: ---

Sorting/heaping/etc. an array/vector of unique_ptr generates worse codegen than
the equivalent code on an array of raw pointers. This is a missed optimization,
as the operations involved should just be mere swaps.

For instance:


#include <memory>
#include <algorithm>

#if defined(SMART)
using ptr = std::unique_ptr<int>;
#else
using ptr = int*;
#endif

void f()
{
    extern ptr *data;
    const auto cmp = [](const auto &a, const auto &b) { return *a < *b; };
    std::sort(data, data+1000, cmp);
}


https://gcc.godbolt.org/z/7zPYxr9q7


This generates quite some extra code (including calls to operator delete); the
result is that such a sort is slower than the countepart with a raw pointer.

Reply via email to