-
Notifications
You must be signed in to change notification settings - Fork 13.9k
[libc++] Optimize std::{,ranges}::{fill,fill_n} for segmented iterators #132665
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
base: main
Are you sure you want to change the base?
Conversation
892e5b2
to
bb949d4
Compare
@llvm/pr-subscribers-libcxx Author: Peng Liu (winner245) ChangesThis patch optimizes Below are the benchmark results comparing the before and after implementations. For reference purposes, we've also provided the benchmarks for Fixes two subtasks outlined in #102817.
|
9d18306
to
d89bca5
Compare
} | ||
|
||
template <class _OutIter, class _Tp> | ||
struct _FillSegment { |
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.
General thought: Can we forward to for_each
instead to get the same effect?
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.
Good point! I think that's possible to forward to for_each
, and we should get similar performance. I will do some tests before I update.
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.
After further thought, I realized that there is a limitation if we forward to for_each
. Currently, segmented iterator optimization for for_each
is only available for C++23 and above, due to constraints imposed by __movable_box
used in for_each
optimization. However, with the optimization specifically applied to fill
and fill_n
, we don't face the limitation; the optimization works for C++11. That's not surprising because for_each
is a more general algorithm than fill/fill_n
. I do agree that when it is possible, forwarding to for_each
should yield similar performance.
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.
#132896 extends for_each
to be applicable starting from C++11. So I will revisit the suggested approach to forward to for_each
.
d89bca5
to
b3a4fd9
Compare
d0338d9
to
2df2a8f
Compare
2df2a8f
to
5616913
Compare
This patch optimizes
std::fill
,std::fill_n
,std::ranges::fill
, andstd::ranges::fill_n
for segmented iterators, achieving substantial performance improvements. Specifically, fordeque<int>
iterators, the performance improvements are above 10x for all these algorithms. The optimization also enables filling segmented memory ofdeque<int>
to approach the performance of filling contiguous memory ofvector<int>
.Benchmark results comparing the before and after implementations are provided below. For additional context, we’ve included
vector<int>
results, which remain unchanged, as this patch specifically targets segmented iterators and leaves non-segmented iterator behavior untouched.Fixes two subtasks outlined in #102817.
fill_n
fill