[libcxx-commits] [libcxx] [libcxx][algorithm] Optimize std::stable_sort via radix sort algorithm (PR #104683)

Дмитрий Изволов via libcxx-commits libcxx-commits at lists.llvm.org
Tue Sep 10 11:46:55 PDT 2024


================
@@ -189,13 +198,35 @@ struct __stable_sort_switch {
   static const unsigned value = 128 * is_trivially_copy_assignable<_Tp>::value;
 };
 
-template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
-void __stable_sort(_RandomAccessIterator __first,
-                   _RandomAccessIterator __last,
-                   _Compare __comp,
-                   typename iterator_traits<_RandomAccessIterator>::difference_type __len,
-                   typename iterator_traits<_RandomAccessIterator>::value_type* __buff,
-                   ptrdiff_t __buff_size) {
+template <class _Tp, class = void>
+struct __radix_sort_min_switch {
+  static const unsigned value = (1 << 10);
+};
+
+template <class _Int8>
+struct __radix_sort_min_switch<_Int8, __enable_if_t<is_integral<_Int8>::value && sizeof(_Int8) == 1> > {
+  static const unsigned value = (1 << 8);
+};
+
+template <class _Tp, class = void>
+struct __radix_sort_max_switch {
+  static const unsigned value = (1 << 16);
+};
+
+template <class _Int64>
+struct __radix_sort_max_switch<_Int64, __enable_if_t<is_integral<_Int64>::value && sizeof(_Int64) == 8> > {
+  static const unsigned value = (1 << 15);
+};
----------------
izvolov wrote:

Do you mean something like this?
```cpp
template <class _Tp>
constexpr unsigned __radix_sort_min_bound () {
  static_assert(std::is_integral_v<_Tp>);
  if constexpr (sizeof(_Tp) == 1) {
      return 1 << 8;
  }

  return 1 << 10;
}
```


Is is legal to use C++11 and later features in `stable_sort.h` since there aren't any version guards there?
If it is, I'd also rewrite the code to remove `_BoolConstant<_EnableRadixSort>`.

https://github.com/llvm/llvm-project/pull/104683


More information about the libcxx-commits mailing list