-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc++] Optimize std::find_if #167697
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
Open
philnik777
wants to merge
1
commit into
llvm:main
Choose a base branch
from
philnik777:optimize_find_if
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−58
Open
[libc++] Optimize std::find_if #167697
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,65 @@ | ||||||||||||||||||||||||||||
| //===----------------------------------------------------------------------===// | ||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||||||||||||||||||||||
| // See https://llvm.org/LICENSE.txt for license information. | ||||||||||||||||||||||||||||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||
| //===----------------------------------------------------------------------===// | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #ifndef _LIBCPP___MEMORY_VALID_RANGE_H | ||||||||||||||||||||||||||||
| #define _LIBCPP___MEMORY_VALID_RANGE_H | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #include <__algorithm/comp.h> | ||||||||||||||||||||||||||||
| #include <__assert> | ||||||||||||||||||||||||||||
| #include <__config> | ||||||||||||||||||||||||||||
| #include <__iterator/iterator_traits.h> | ||||||||||||||||||||||||||||
| #include <__memory/assume_aligned.h> | ||||||||||||||||||||||||||||
| #include <__memory/pointer_traits.h> | ||||||||||||||||||||||||||||
| #include <__type_traits/is_constant_evaluated.h> | ||||||||||||||||||||||||||||
| #include <__type_traits/is_same.h> | ||||||||||||||||||||||||||||
| #include <__type_traits/remove_cvref.h> | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||||||||||||||||||||||||||||
| # pragma GCC system_header | ||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| _LIBCPP_BEGIN_NAMESPACE_STD | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| template <class _Tp> | ||||||||||||||||||||||||||||
| _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool | ||||||||||||||||||||||||||||
| __is_valid_range(const _Tp* __first, const _Tp* __last) { | ||||||||||||||||||||||||||||
| if (__libcpp_is_constant_evaluated()) { | ||||||||||||||||||||||||||||
| // If this is not a constant during constant evaluation, that is because __first and __last are not | ||||||||||||||||||||||||||||
| // part of the same allocation. If they are part of the same allocation, we must still make sure they | ||||||||||||||||||||||||||||
| // are ordered properly. | ||||||||||||||||||||||||||||
| return __builtin_constant_p(__first <= __last) && __first <= __last; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return !__less<>()(__last, __first); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // This functions allows the compiler to assume that [__first, __last) is a valid range to be given to an algortihm. | ||||||||||||||||||||||||||||
| // Specifically, this means that | ||||||||||||||||||||||||||||
| // - [__first, __last) is dereferenceable | ||||||||||||||||||||||||||||
| // - __first and __last are correctly aligned according to the language rules | ||||||||||||||||||||||||||||
| // This allows (curently only clang-based compilers) to auto-vectorize algorithms that contain early returns. | ||||||||||||||||||||||||||||
|
Comment on lines
+41
to
+45
Member
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.
Suggested change
|
||||||||||||||||||||||||||||
| template <class _Iter, class _Sent> | ||||||||||||||||||||||||||||
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __assume_valid_range(_Iter&& __first, _Sent&& __last) { | ||||||||||||||||||||||||||||
philnik777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
| #if __has_builtin(__builtin_assume_dereferenceable) && !defined(_LIBCPP_CXX03_LANG) | ||||||||||||||||||||||||||||
| if constexpr (__libcpp_is_contiguous_iterator<__remove_cvref_t<_Iter>>::value && | ||||||||||||||||||||||||||||
| is_same<__remove_cvref_t<_Iter>, __remove_cvref_t<_Sent>>::value) { | ||||||||||||||||||||||||||||
| _LIBCPP_ASSERT_INTERNAL(std::__is_valid_range(std::__to_address(__first), std::__to_address(__last)), | ||||||||||||||||||||||||||||
| "Valid range assumption does not hold"); | ||||||||||||||||||||||||||||
| if (!__libcpp_is_constant_evaluated()) { | ||||||||||||||||||||||||||||
| using __value_type = typename iterator_traits<__remove_cvref_t<_Iter>>::value_type; | ||||||||||||||||||||||||||||
| __builtin_assume_dereferenceable(std::__to_address(__first), (__last - __first) * sizeof(__value_type)); | ||||||||||||||||||||||||||||
| (void)std::__assume_aligned<_LIBCPP_ALIGNOF(__value_type)>(std::__to_address(__first)); | ||||||||||||||||||||||||||||
| (void)std::__assume_aligned<_LIBCPP_ALIGNOF(__value_type)>(std::__to_address(__last)); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| _LIBCPP_END_NAMESPACE_STD | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #endif // _LIBCPP___MEMORY_VALID_RANGE_H | ||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
libcxx/test/benchmarks/algorithms/nonmodifying/find_if.bench.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #include <benchmark/benchmark.h> | ||
|
|
||
| template <class Iter, class ValueT> | ||
| Iter my_find(Iter first, Iter last, const ValueT& i) { | ||
| for (; first != last; ++first) { | ||
| if (*first == i) | ||
| break; | ||
| } | ||
| return first; | ||
| } | ||
|
|
||
| static auto bm_find_if_no_vectorization(benchmark::State& state) { | ||
| std::size_t const size = 8192; | ||
| std::vector<short> c(size, 0); | ||
|
|
||
| for ([[maybe_unused]] auto _ : state) { | ||
| benchmark::DoNotOptimize(c); | ||
| std::vector<short>::iterator result; | ||
| result = my_find(c.begin(), c.end(), 1); | ||
| benchmark::DoNotOptimize(result); | ||
| } | ||
| } | ||
| BENCHMARK(bm_find_if_no_vectorization); | ||
|
|
||
| static auto bm_find_if_autovectorization(benchmark::State& state) { | ||
| std::size_t const size = 8192; | ||
| std::vector<short> c(size, 0); | ||
|
|
||
| for ([[maybe_unused]] auto _ : state) { | ||
| benchmark::DoNotOptimize(c); | ||
| std::vector<short>::iterator result; | ||
| result = find_if(c.begin(), c.end(), [](short i) { return i == 1; }); | ||
| benchmark::DoNotOptimize(result); | ||
| } | ||
| } | ||
| BENCHMARK(bm_find_if_autovectorization); | ||
|
|
||
| static auto bm_find_manual_vectorization(benchmark::State& state) { | ||
| std::size_t const size = 8192; | ||
| std::vector<short> c(size, 0); | ||
|
|
||
| for ([[maybe_unused]] auto _ : state) { | ||
| benchmark::DoNotOptimize(c); | ||
| std::vector<short>::iterator result; | ||
| result = find(c.begin(), c.end(), 1); | ||
| benchmark::DoNotOptimize(result); | ||
| } | ||
| } | ||
| BENCHMARK(bm_find_manual_vectorization); | ||
|
|
||
| BENCHMARK_MAIN(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the benifit of using
__lessto compare two pointers?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC we've used it since it's conceptually well-defined to use
lessfor comparing unrelated pointers, but not<.