Skip to content

Commit 46e8816

Browse files
[libcxx] Optimize std::generate for segmented iterators (llvm#163006)
Part of llvm#102817. This patch attempts to optimize the performance of `std::generate` for segmented iterators. Below are the benchmark numbers from `libcxx\test\benchmarks\algorithms\modifying\generate.bench.cpp`. Test cases that use segmented iterators have also been added. - before ``` std::generate(deque<int>)/32 194 ns 193 ns 3733333 std::generate(deque<int>)/50 276 ns 276 ns 2488889 std::generate(deque<int>)/1024 5096 ns 5022 ns 112000 std::generate(deque<int>)/8192 40806 ns 40806 ns 17231 ``` - after ``` std::generate(deque<int>)/32 106 ns 105 ns 6400000 std::generate(deque<int>)/50 139 ns 138 ns 4977778 std::generate(deque<int>)/1024 2713 ns 2699 ns 248889 std::generate(deque<int>)/8192 18983 ns 19252 ns 37333 ``` --------- Co-authored-by: A. Jiang <[email protected]>
1 parent 3027b4a commit 46e8816

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

libcxx/docs/ReleaseNotes/22.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Improvements and New Features
7575
- The ``std::{fill, fill_n}`` and ``std::ranges::{fill, fill_n}`` algorithms have been optimized for segmented iterators,
7676
resulting in a performance improvement of at least 10x for ``std::deque<int>`` iterators and
7777
``std::join_view<std::vector<std::vector<int>>>`` iterators.
78+
- The ``std::generate`` algorithm has been optimized for segmented iterators, resulting in a performance improvement for
79+
``std::deque<short>`` and ``std::join_view<vector<vector<short>>>`` iterators.
7880

7981
Deprecations and Removals
8082
-------------------------

libcxx/include/__algorithm/generate.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#ifndef _LIBCPP___ALGORITHM_GENERATE_H
1010
#define _LIBCPP___ALGORITHM_GENERATE_H
1111

12+
#include <__algorithm/for_each.h>
1213
#include <__config>
14+
#include <__utility/forward.h>
1315

1416
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1517
# pragma GCC system_header
@@ -20,8 +22,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2022
template <class _ForwardIterator, class _Generator>
2123
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2224
generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen) {
23-
for (; __first != __last; ++__first)
24-
*__first = __gen();
25+
using __iter_ref = decltype(*__first);
26+
std::for_each(__first, __last, [&](__iter_ref __element) { std::forward<__iter_ref>(__element) = __gen(); });
2527
}
2628

2729
_LIBCPP_END_NAMESPACE_STD

libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/generate.pass.cpp

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

1717
#include <algorithm>
1818
#include <cassert>
19+
#include <deque>
1920

2021
#include "test_macros.h"
2122
#include "test_iterators.h"
@@ -51,12 +52,22 @@ test()
5152
assert(ia[3] == 1);
5253
}
5354

55+
void deque_test() {
56+
int sizes[] = {0, 1, 2, 1023, 1024, 1025, 2047, 2048, 2049};
57+
for (const int size : sizes) {
58+
std::deque<int> d(size);
59+
std::generate(d.begin(), d.end(), gen_test());
60+
assert(std::all_of(d.begin(), d.end(), [](int x) { return x == 1; }));
61+
}
62+
}
63+
5464
int main(int, char**)
5565
{
5666
test<forward_iterator<int*> >();
5767
test<bidirectional_iterator<int*> >();
5868
test<random_access_iterator<int*> >();
5969
test<int*>();
70+
deque_test();
6071

6172
#if TEST_STD_VER > 17
6273
static_assert(test_constexpr());

0 commit comments

Comments
 (0)