-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[libc++] Optimize {std,ranges}::distance for segmented iterators #133612
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
winner245
wants to merge
3
commits into
llvm:main
Choose a base branch
from
winner245:segmented-distance
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.
Open
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,69 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | ||
|
||
#include <cstddef> | ||
#include <deque> | ||
#include <iterator> | ||
#include <ranges> | ||
#include <vector> | ||
|
||
#include <benchmark/benchmark.h> | ||
|
||
int main(int argc, char** argv) { | ||
auto std_distance = [](auto first, auto last) { return std::distance(first, last); }; | ||
|
||
// {std,ranges}::distance | ||
{ | ||
auto bm = []<class Container>(std::string name, auto distance, std::size_t seg_size) { | ||
benchmark::RegisterBenchmark( | ||
name, | ||
[distance, seg_size](auto& st) { | ||
std::size_t const size = st.range(0); | ||
std::size_t const segments = (size + seg_size - 1) / seg_size; | ||
Container c(segments); | ||
for (std::size_t i = 0, n = size; i < segments; ++i, n -= seg_size) { | ||
c[i].resize(std::min(seg_size, n)); | ||
} | ||
|
||
auto view = c | std::views::join; | ||
auto first = view.begin(); | ||
auto last = view.end(); | ||
|
||
for ([[maybe_unused]] auto _ : st) { | ||
benchmark::DoNotOptimize(c); | ||
auto result = distance(first, last); | ||
benchmark::DoNotOptimize(result); | ||
} | ||
}) | ||
->Arg(50) // non power-of-two | ||
->Arg(1024) | ||
->Arg(4096) | ||
->Arg(8192); | ||
}; | ||
bm.operator()<std::vector<std::vector<int>>>("std::distance(join_view(vector<vector<int>>))", std_distance, 256); | ||
bm.operator()<std::deque<std::deque<int>>>("std::distance(join_view(deque<deque<int>>))", std_distance, 256); | ||
bm.operator()<std::vector<std::vector<int>>>( | ||
"rng::distance(join_view(vector<vector<int>>)", std::ranges::distance, 256); | ||
bm.operator()<std::deque<std::deque<int>>>( | ||
"rng::distance(join_view(deque<deque<int>>)", std::ranges::distance, 256); | ||
|
||
bm.operator()<std::vector<std::vector<int>>>("std::distance(join_view(vector<vector<int>>))", std_distance, 1024); | ||
bm.operator()<std::deque<std::deque<int>>>("std::distance(join_view(deque<deque<int>>))", std_distance, 1024); | ||
bm.operator()<std::vector<std::vector<int>>>( | ||
"rng::distance(join_view(vector<vector<int>>)", std::ranges::distance, 1024); | ||
bm.operator()<std::deque<std::deque<int>>>( | ||
"rng::distance(join_view(deque<deque<int>>)", std::ranges::distance, 1024); | ||
} | ||
|
||
benchmark::Initialize(&argc, argv); | ||
benchmark::RunSpecifiedBenchmarks(); | ||
benchmark::Shutdown(); | ||
return 0; | ||
} |
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.
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.
We already have
__iter_diff_t
initerator_traits.h
. It doesn't do exactly what you want, but I think we should make it do what you need instead of introducing a new thing that's essentially identical.Basically, we should try re-defining
__iter_diff_t
initerator_traits.h
as:And if that's not possible for some reason, it would be interesting to understand why.
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.
It seems that the standard hasn't requires
iterator_traits<I>::difference_type
to be same asiter_difference_t<I>
yet whenI
is a non-random-access iterator type. One is allowed to make them different by specializingincrementable_traits
. This is perhaps a defect, for which I've submitted LWG4080.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.
Given the potential mismatch between
iterator_traits<I>::difference_type
anditer_difference_t<I>
(as pointed out by @frederick-vs-ja), I am now consistently usingiterator_traits<I>::difference_type
as the return type for the non-rangestd::__distance
algorithm, anditer_difference_t<I>
for the range algorithm.To enable the range algorithm to reuse the same optimization introduced for the non-range
std::__distance
, I am now adding astatic_cast
to explicitly cast the result ofstd::__distance
to the desired typeiter_difference_t<I>
.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.
However, this approach fails when the range algorithm
std::ranges::distance
forwards tostd::__distance
withI = cpp20_input_iterator
, resulting in a hard substitution error:no type named 'difference_type' in 'std::iterator_traits<cpp20_input_iterator<int *>>'
, as captured by the testsrange.pass.cpp
anditerator_sentinel.pass.cpp
(see the full log: stage1 (generic-modules, clang-21, clang++-21)).The root cause for this error is that
cpp20_input_iterator
does not have the member typeiterator_category
(although it provides theiterator_concept
member type), leading to a hard substitution failure instd::iterator_traits<cpp20_input_iterator<Iter>>::difference_type
(See A reproducer).To resolve this, I still need the above internal definition of
__iter_distance_t
, which is used as the return type of the internal functionstd::__distance
. This allows both the non-rangestd::distance
and range versionstd::ranges::distance
to share the same implementation and optimization. However, to handle the potential mismatch betweeniterator_traits<I>::difference_type
anditer_difference_t<I>
, I explicitly specifiediterator_traits<I>::difference_type
as the return type ofstd::distance
anditer_difference_t<I>
as the return type ofstd::ranges::distance
. This way the underlying implicit conversion would automatically take place when there is a type mismatch.I am a bit hesitant to make this change because
__iter_diff_t
is currently unconditionally defined asiterator_traits<_Iter>::difference_type
, and it is used by many algorithms. Making this change would introduce the potential type mismatch to many algorithms. My current approach with the introduction of__iter_distance_t
ensures that the potential type mismatch is confined and handled withindistance.h
.