Skip to content

Commit dffc36a

Browse files
committed
[libc++] Optimize heap operations
1 parent 715b1d5 commit dffc36a

File tree

7 files changed

+93
-88
lines changed

7 files changed

+93
-88
lines changed

libcxx/include/__algorithm/make_heap.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compar
3232
__comp_ref_type<_Compare> __comp_ref = __comp;
3333

3434
using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
35-
difference_type __n = __last - __first;
36-
if (__n > 1) {
35+
difference_type __n = __last - __first, __start = __n / 2;
36+
while (__start != 0) {
3737
// start from the first parent, there is no need to consider children
38-
for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start) {
39-
std::__sift_down<_AlgPolicy>(__first, __comp_ref, __n, __first + __start);
40-
}
38+
std::__sift_down<_AlgPolicy>(__first, __comp_ref, __n, --__start);
4139
}
4240
}
4341

libcxx/include/__algorithm/partial_sort.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandomAccessIterator __part
4545
for (; __i != __last; ++__i) {
4646
if (__comp(*__i, *__first)) {
4747
_IterOps<_AlgPolicy>::iter_swap(__i, __first);
48-
std::__sift_down<_AlgPolicy>(__first, __comp, __len, __first);
48+
std::__sift_down<_AlgPolicy>(__first, __comp, __len, 0);
4949
}
5050
}
5151
std::__sort_heap<_AlgPolicy>(std::move(__first), std::move(__middle), __comp);

libcxx/include/__algorithm/partial_sort_copy.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <__algorithm/comp.h>
1313
#include <__algorithm/comp_ref_type.h>
14+
#include <__algorithm/copy.h>
1415
#include <__algorithm/iterator_operations.h>
1516
#include <__algorithm/make_heap.h>
1617
#include <__algorithm/make_projected.h>
@@ -49,19 +50,18 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InputIterator, _Random
4950
_Compare&& __comp,
5051
_Proj1&& __proj1,
5152
_Proj2&& __proj2) {
52-
_RandomAccessIterator __r = __result_first;
5353
auto&& __projected_comp = std::__make_projected(__comp, __proj2);
54+
_RandomAccessIterator __r = std::copy(__first, __last, std::move(__result_first));
5455

55-
if (__r != __result_last) {
56-
for (; __first != __last && __r != __result_last; ++__first, (void)++__r)
57-
*__r = *__first;
56+
if (__result_first != __result_last) {
5857
std::__make_heap<_AlgPolicy>(__result_first, __r, __projected_comp);
5958
typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
60-
for (; __first != __last; ++__first)
59+
for (; __first != __last; ++__first) {
6160
if (std::__invoke(__comp, std::__invoke(__proj1, *__first), std::__invoke(__proj2, *__result_first))) {
6261
*__result_first = *__first;
63-
std::__sift_down<_AlgPolicy>(__result_first, __projected_comp, __len, __result_first);
62+
std::__sift_down<_AlgPolicy>(__result_first, __projected_comp, __len, 0);
6463
}
64+
}
6565
std::__sort_heap<_AlgPolicy>(__result_first, __r, __projected_comp);
6666
}
6767

libcxx/include/__algorithm/pop_heap.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,21 @@ _LIBCPP_BEGIN_NAMESPACE_STD
3333
template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
3434
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
3535
__pop_heap(_RandomAccessIterator __first,
36-
_RandomAccessIterator __last,
36+
_RandomAccessIterator __bottom,
3737
_Compare& __comp,
3838
typename iterator_traits<_RandomAccessIterator>::difference_type __len) {
39-
// Calling `pop_heap` on an empty range is undefined behavior, but in practice it will be a no-op.
40-
_LIBCPP_ASSERT_PEDANTIC(__len > 0, "The heap given to pop_heap must be non-empty");
41-
42-
__comp_ref_type<_Compare> __comp_ref = __comp;
43-
4439
using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
40+
4541
if (__len > 1) {
4642
value_type __top = _IterOps<_AlgPolicy>::__iter_move(__first); // create a hole at __first
47-
_RandomAccessIterator __hole = std::__floyd_sift_down<_AlgPolicy>(__first, __comp_ref, __len);
48-
--__last;
43+
_RandomAccessIterator __hole = std::__floyd_sift_down<_AlgPolicy>(__first, __comp, __len);
4944

50-
if (__hole == __last) {
45+
if (__hole == __bottom) {
5146
*__hole = std::move(__top);
5247
} else {
53-
*__hole = _IterOps<_AlgPolicy>::__iter_move(__last);
54-
++__hole;
55-
*__last = std::move(__top);
56-
std::__sift_up<_AlgPolicy>(__first, __hole, __comp_ref, __hole - __first);
48+
*__hole = _IterOps<_AlgPolicy>::__iter_move(__bottom);
49+
*__bottom = std::move(__top);
50+
std::__sift_up<_AlgPolicy>(__first, __hole, __comp);
5751
}
5852
}
5953
}
@@ -64,8 +58,13 @@ pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare _
6458
static_assert(std::is_copy_constructible<_RandomAccessIterator>::value, "Iterators must be copy constructible.");
6559
static_assert(std::is_copy_assignable<_RandomAccessIterator>::value, "Iterators must be copy assignable.");
6660

61+
// Calling `pop_heap` on an empty range is undefined behavior, but in practice it will be a no-op.
62+
_LIBCPP_ASSERT_PEDANTIC(__len > 0, "The heap given to pop_heap must be non-empty");
63+
64+
__comp_ref_type<_Compare> __comp_ref = __comp;
65+
6766
typename iterator_traits<_RandomAccessIterator>::difference_type __len = __last - __first;
68-
std::__pop_heap<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp, __len);
67+
std::__pop_heap<_ClassicAlgPolicy>(std::move(__first), std::move(--__last), __comp_ref, __len);
6968
}
7069

7170
template <class _RandomAccessIterator>

libcxx/include/__algorithm/push_heap.h

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,35 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2929

3030
template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
3131
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
32-
__sift_up(_RandomAccessIterator __first,
33-
_RandomAccessIterator __last,
34-
_Compare&& __comp,
35-
typename iterator_traits<_RandomAccessIterator>::difference_type __len) {
36-
using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
37-
38-
if (__len > 1) {
39-
__len = (__len - 2) / 2;
40-
_RandomAccessIterator __ptr = __first + __len;
41-
42-
if (__comp(*__ptr, *--__last)) {
43-
value_type __t(_IterOps<_AlgPolicy>::__iter_move(__last));
44-
do {
45-
*__last = _IterOps<_AlgPolicy>::__iter_move(__ptr);
46-
__last = __ptr;
47-
if (__len == 0)
48-
break;
49-
__len = (__len - 1) / 2;
50-
__ptr = __first + __len;
51-
} while (__comp(*__ptr, __t));
52-
53-
*__last = std::move(__t);
54-
}
32+
__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare&& __comp) {
33+
using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
34+
using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
35+
36+
difference_type __parent = __last - __first;
37+
_LIBCPP_ASSERT_INTERNAL(__parent > 0, "shouldn't be called unless __last - __first > 0");
38+
__parent = (__parent - 1) / 2;
39+
_RandomAccessIterator __parent_i = __first + __parent;
40+
41+
if (__comp(*__parent_i, *__last)) {
42+
value_type __t(_IterOps<_AlgPolicy>::__iter_move(__last));
43+
do {
44+
*__last = _IterOps<_AlgPolicy>::__iter_move(__parent_i);
45+
__last = __parent_i;
46+
if (__parent == 0)
47+
break;
48+
__parent = (__parent - 1) / 2;
49+
__parent_i = __first + __parent;
50+
} while (__comp(*__parent_i, __t));
51+
52+
*__last = std::move(__t);
5553
}
5654
}
5755

5856
template <class _AlgPolicy, class _RandomAccessIterator, class _Compare>
5957
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
6058
__push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare& __comp) {
61-
typename iterator_traits<_RandomAccessIterator>::difference_type __len = __last - __first;
62-
std::__sift_up<_AlgPolicy, __comp_ref_type<_Compare> >(std::move(__first), std::move(__last), __comp, __len);
59+
if (__first != __last)
60+
std::__sift_up<_AlgPolicy, __comp_ref_type<_Compare> >(std::move(__first), std::move(--__last), __comp);
6361
}
6462

6563
template <class _RandomAccessIterator, class _Compare>

libcxx/include/__algorithm/sift_down.h

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,84 +29,94 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
2929
__sift_down(_RandomAccessIterator __first,
3030
_Compare&& __comp,
3131
typename iterator_traits<_RandomAccessIterator>::difference_type __len,
32-
_RandomAccessIterator __start) {
32+
typename iterator_traits<_RandomAccessIterator>::difference_type __start) {
3333
using _Ops = _IterOps<_AlgPolicy>;
3434

3535
typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3636
typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
37-
// left-child of __start is at 2 * __start + 1
38-
// right-child of __start is at 2 * __start + 2
39-
difference_type __child = __start - __first;
4037

41-
if (__len < 2 || (__len - 2) / 2 < __child)
38+
if (__len < 2)
4239
return;
4340

44-
__child = 2 * __child + 1;
45-
_RandomAccessIterator __child_i = __first + __child;
41+
// left-child of __start is at 2 * __start + 1
42+
// right-child of __start is at 2 * __start + 2
43+
difference_type __child = 2 * __start + 1;
44+
_RandomAccessIterator __child_i = __first + __child, __start_i = __first + __start;
4645

47-
if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
48-
// right-child exists and is greater than left-child
49-
++__child_i;
50-
++__child;
46+
if (__child < __len - 1) {
47+
_RandomAccessIterator __right_i = _Ops::next(__child_i);
48+
if (__comp(*__child_i, *__right_i)) {
49+
// right-child exists and is greater than left-child
50+
__child_i = __right_i;
51+
++__child;
52+
}
5153
}
5254

5355
// check if we are in heap-order
54-
if (__comp(*__child_i, *__start))
56+
if (__comp(*__child_i, *__start_i))
5557
// we are, __start is larger than its largest child
5658
return;
5759

58-
value_type __top(_Ops::__iter_move(__start));
60+
value_type __top(_Ops::__iter_move(__start_i));
5961
do {
6062
// we are not in heap-order, swap the parent with its largest child
61-
*__start = _Ops::__iter_move(__child_i);
62-
__start = __child_i;
63+
*__start_i = _Ops::__iter_move(__child_i);
64+
__start_i = __child_i;
6365

64-
if ((__len - 2) / 2 < __child)
66+
if (__len / 2 - 1 < __child)
6567
break;
6668

6769
// recompute the child based off of the updated parent
6870
__child = 2 * __child + 1;
6971
__child_i = __first + __child;
7072

71-
if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
72-
// right-child exists and is greater than left-child
73-
++__child_i;
74-
++__child;
73+
if (__child < __len - 1) {
74+
_RandomAccessIterator __right_i = _Ops::next(__child_i);
75+
if (__comp(*__child_i, *__right_i)) {
76+
// right-child exists and is greater than left-child
77+
__child_i = __right_i;
78+
++__child;
79+
}
7580
}
7681

7782
// check if we are in heap-order
7883
} while (!__comp(*__child_i, __top));
79-
*__start = std::move(__top);
84+
*__start_i = std::move(__top);
8085
}
8186

8287
template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
8388
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _RandomAccessIterator __floyd_sift_down(
8489
_RandomAccessIterator __first,
8590
_Compare&& __comp,
8691
typename iterator_traits<_RandomAccessIterator>::difference_type __len) {
87-
using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
88-
_LIBCPP_ASSERT_INTERNAL(__len >= 2, "shouldn't be called unless __len >= 2");
92+
_LIBCPP_ASSERT_INTERNAL(__len > 1, "shouldn't be called unless __len > 1");
8993

90-
_RandomAccessIterator __hole = __first;
91-
_RandomAccessIterator __child_i = __first;
92-
difference_type __child = 0;
94+
using _Ops = _IterOps<_AlgPolicy>;
9395

94-
while (true) {
95-
__child_i += difference_type(__child + 1);
96-
__child = 2 * __child + 1;
96+
typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
9797

98-
if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
99-
// right-child exists and is greater than left-child
100-
++__child_i;
101-
++__child;
98+
difference_type __child = 1;
99+
_RandomAccessIterator __hole = __first, __child_i = __first;
100+
101+
while (true) {
102+
__child_i += __child;
103+
__child *= 2;
104+
105+
if (__child < __len) {
106+
_RandomAccessIterator __right_i = _Ops::next(__child_i);
107+
if (__comp(*__child_i, *__right_i)) {
108+
// right-child exists and is greater than left-child
109+
__child_i = __right_i;
110+
++__child;
111+
}
102112
}
103113

104114
// swap __hole with its largest child
105-
*__hole = _IterOps<_AlgPolicy>::__iter_move(__child_i);
115+
*__hole = _Ops::__iter_move(__child_i);
106116
__hole = __child_i;
107117

108118
// if __hole is now a leaf, we're done
109-
if (__child > (__len - 2) / 2)
119+
if (__child > __len / 2)
110120
return __hole;
111121
}
112122
}

libcxx/include/__algorithm/sort_heap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compar
3636
__comp_ref_type<_Compare> __comp_ref = __comp;
3737

3838
using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
39-
for (difference_type __n = __last - __first; __n > 1; --__last, (void)--__n)
40-
std::__pop_heap<_AlgPolicy>(__first, __last, __comp_ref, __n);
39+
for (difference_type __n = __last - __first; __n > 1; --__n)
40+
std::__pop_heap<_AlgPolicy>(__first, --__last, __comp_ref, __n);
4141
std::__check_strict_weak_ordering_sorted(__first, __saved_last, __comp_ref);
4242
}
4343

0 commit comments

Comments
 (0)