[libcxx-commits] [PATCH] D114133: [libc++] Minor fixups in the new introsort code.

Arthur O'Dwyer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 17 19:31:49 PST 2021


Quuxplusone created this revision.
Quuxplusone added reviewers: ldionne, nilayvaish, libc++.
Quuxplusone added a project: libc++.
Quuxplusone requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: libcxx-commits, sstefan1.
Herald added 1 blocking reviewer(s): libc++.

ADL-proof. (This is a sneaky one because it exists only in a codepath that is taken for non-pointer-type iterators, so I don't think it's physically possible for `robust_against_adl.pass.cpp` to catch it.)
Don't copy comparators.
Don't provide explicit arguments to `std::min_element` (but luckily we don't need to, because we know `_Compare` is cheap-to-copy at this point because we're inside a double-underscored algorithm).

This should fix the issue reported on D113413 <https://reviews.llvm.org/D113413> by @haowei.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114133

Files:
  libcxx/include/__algorithm/sort.h


Index: libcxx/include/__algorithm/sort.h
===================================================================
--- libcxx/include/__algorithm/sort.h
+++ libcxx/include/__algorithm/sort.h
@@ -131,7 +131,7 @@
     _BidirectionalIterator __lm1 = __last;
     for (--__lm1; __first != __lm1; ++__first)
     {
-        _BidirectionalIterator __i = _VSTD::min_element<_BidirectionalIterator, _Compare&>(__first, __last, __comp);
+        _BidirectionalIterator __i = _VSTD::min_element(__first, __last, __comp);
         if (__i != __first)
             swap(*__first, *__i);
     }
@@ -268,13 +268,12 @@
 template <class _Compare, class _RandomAccessIterator>
 void
 __introsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
-            typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type __depth)
+            typename iterator_traits<_RandomAccessIterator>::difference_type __depth)
 {
     typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
     typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
     const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
                                     is_trivially_copy_assignable<value_type>::value ? 30 : 6;
-    typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
     while (true)
     {
     __restart:
@@ -307,7 +306,7 @@
         if (__depth == 0)
         {
           // Fallback to heap sort as Introsort suggests.
-          _VSTD::__partial_sort<_Comp_ref>(__first, __last, __last, _Comp_ref(__comp));
+          _VSTD::__partial_sort<_Compare>(__first, __last, __last, __comp);
           return;
         }
         --__depth;
@@ -478,7 +477,7 @@
 void __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
   typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
   difference_type __depth_limit = 2 * __log2i(__last - __first);
-  __introsort(__first, __last, __comp, __depth_limit);
+  _VSTD::__introsort<_Compare>(__first, __last, __comp, __depth_limit);
 }
 
 template <class _Compare, class _Tp>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114133.388091.patch
Type: text/x-patch
Size: 2184 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211118/c72d357a/attachment-0001.bin>


More information about the libcxx-commits mailing list