Skip to content

Commit 42fb027

Browse files
committed
[libcxx] Optimize std::generate_n for segmented iterators
1 parent 6dda3b1 commit 42fb027

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

libcxx/docs/ReleaseNotes/22.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +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.
78+
- The ``std::generate`` and ``std::generate_n`` algorithms have been optimized for segmented iterators, resulting in a
79+
performance improvement for ``std::deque<short>`` and ``std::join_view<vector<vector<short>>>`` iterators.
8080

8181
Deprecations and Removals
8282
-------------------------

libcxx/include/__algorithm/generate_n.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef _LIBCPP___ALGORITHM_GENERATE_N_H
1010
#define _LIBCPP___ALGORITHM_GENERATE_N_H
1111

12+
#include <__algorithm/for_each_n.h>
1213
#include <__config>
13-
#include <__utility/convert_to_integral.h>
1414

1515
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1616
# pragma GCC system_header
@@ -21,11 +21,10 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2121
template <class _OutputIterator, class _Size, class _Generator>
2222
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
2323
generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen) {
24-
typedef decltype(std::__convert_to_integral(__orig_n)) _IntegralSize;
25-
_IntegralSize __n = __orig_n;
26-
for (; __n > 0; ++__first, (void)--__n)
27-
*__first = __gen();
28-
return __first;
24+
using __iter_ref = decltype(*__first);
25+
return std::for_each_n(__first, __orig_n, [&](__iter_ref __element) {
26+
std::forward<__iter_ref>(__element) = __gen();
27+
});
2928
}
3029

3130
_LIBCPP_END_NAMESPACE_STD

libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/generate_n.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_iterators.h"
2122
#include "test_macros.h"
@@ -71,12 +72,22 @@ test()
7172
test2<Iter, long double>();
7273
}
7374

75+
void deque_test() {
76+
int sizes[] = {0, 1, 2, 1023, 1024, 1025, 2047, 2048, 2049};
77+
for (const int size : sizes) {
78+
std::deque<int> d(size);
79+
std::generate_n(d.begin(), size, gen_test());
80+
assert(std::all_of(d.begin(), d.end(), [](int x) { return x == 2; }));
81+
}
82+
}
83+
7484
int main(int, char**)
7585
{
7686
test<forward_iterator<int*> >();
7787
test<bidirectional_iterator<int*> >();
7888
test<random_access_iterator<int*> >();
7989
test<int*>();
90+
deque_test();
8091

8192
#if TEST_STD_VER > 17
8293
static_assert(test_constexpr());

0 commit comments

Comments
 (0)