-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[libc++] Improve the performance of std::make_heap a bit #154092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,11 @@ | |
#include <__algorithm/comp.h> | ||
#include <__algorithm/comp_ref_type.h> | ||
#include <__algorithm/iterator_operations.h> | ||
#include <__algorithm/push_heap.h> | ||
#include <__algorithm/sift_down.h> | ||
#include <__config> | ||
#include <__iterator/iterator_traits.h> | ||
#include <__type_traits/is_arithmetic.h> | ||
#include <__utility/move.h> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
|
@@ -31,13 +33,23 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void | |
__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare&& __comp) { | ||
__comp_ref_type<_Compare> __comp_ref = __comp; | ||
|
||
using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type; | ||
difference_type __n = __last - __first; | ||
using __diff_t = __iter_diff_t<_RandomAccessIterator>; | ||
const __diff_t __n = __last - __first; | ||
|
||
static const bool __assume_both_children = is_arithmetic<__iter_value_type<_RandomAccessIterator> >::value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use of static within constexpr seems to be failing with gcc for C++ versions before 23. Does not seem to fail with clang. I am working on getting a simple reproducer but wanted to leave a message for the code owners to be aware of.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The warning is triggered in Clang when -Wsystem-headers is set. Godbolt link: https://godbolt.org/z/Wh7zsxsYY There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Clang, this seems to be a warning whereas in GCC its a hard error. Regardless, this code should be fixed given this violates the language spec. @philnik777 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which part of the spec is violated when using a conforming extension in system headers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apologies for lack of clarity in my previous comments where I was just thinking out loud. From what I understand static variables were not allowed within constexpr functions until C++ 23 (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2647r0.html). Your change creates a static variable |
||
|
||
// While it would be correct to always assume we have both children, in practice we observed this to be a performance | ||
// improvement only for arithmetic types. | ||
const __diff_t __sift_down_n = __assume_both_children ? ((__n & 1) ? __n : __n - 1) : __n; | ||
|
||
if (__n > 1) { | ||
// start from the first parent, there is no need to consider children | ||
for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start) { | ||
std::__sift_down<_AlgPolicy>(__first, __comp_ref, __n, __first + __start); | ||
|
||
for (__diff_t __start = (__sift_down_n - 2) / 2; __start >= 0; --__start) { | ||
std::__sift_down<_AlgPolicy, __assume_both_children>(__first, __comp_ref, __sift_down_n, __start); | ||
} | ||
if _LIBCPP_CONSTEXPR (__assume_both_children) | ||
std::__sift_up<_AlgPolicy>(__first, __last, __comp, __n); | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.