Skip to content

Commit d0338d9

Browse files
committed
Optimize {std,ranges}::{fill,fill_n} for segmented iterators
1 parent 4ed10db commit d0338d9

File tree

8 files changed

+166
-23
lines changed

8 files changed

+166
-23
lines changed

libcxx/docs/ReleaseNotes/21.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ Improvements and New Features
7676
- The ``bitset::to_string`` function has been optimized, resulting in a performance improvement of up to 8.3x for bitsets
7777
with uniformly distributed zeros and ones, and up to 13.5x and 16.1x for sparse and dense bitsets, respectively.
7878

79+
- The ``std::{fill, fill_n}``, ``std::ranges::{fill, fill_n}`` algorithms have been optimized for segmented iterators,
80+
resulting in a performance improvement of at least 10x for ``std::deque<int>`` iterators and
81+
``std::join_view<std::vector<std::vector<int>>>`` iterators.
82+
7983
Deprecations and Removals
8084
-------------------------
8185

libcxx/include/__algorithm/fill.h

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
#define _LIBCPP___ALGORITHM_FILL_H
1111

1212
#include <__algorithm/fill_n.h>
13+
#include <__algorithm/for_each_segment.h>
1314
#include <__config>
1415
#include <__iterator/iterator_traits.h>
16+
#include <__iterator/segmented_iterator.h>
17+
#include <__type_traits/enable_if.h>
1518

1619
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1720
# pragma GCC system_header
@@ -21,23 +24,42 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2124

2225
// fill isn't specialized for std::memset, because the compiler already optimizes the loop to a call to std::memset.
2326

24-
template <class _ForwardIterator, class _Tp>
25-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
26-
__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, forward_iterator_tag) {
27+
template <class _ForwardIterator, class _Sentinel, class _Tp>
28+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
29+
__fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __value) {
2730
for (; __first != __last; ++__first)
2831
*__first = __value;
32+
return __first;
2933
}
3034

31-
template <class _RandomAccessIterator, class _Tp>
32-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
33-
__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value, random_access_iterator_tag) {
34-
std::fill_n(__first, __last - __first, __value);
35+
template <class _RandomAccessIterator,
36+
class _Tp,
37+
__enable_if_t<__has_random_access_iterator_category<_RandomAccessIterator>::value &&
38+
!__is_segmented_iterator<_RandomAccessIterator>::value,
39+
int> = 0>
40+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandomAccessIterator
41+
__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value) {
42+
return std::__fill_n(__first, __last - __first, __value);
43+
}
44+
45+
#ifndef _LIBCPP_CXX03_LANG
46+
template <class _SegmentedIterator,
47+
class _Tp,
48+
__enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
49+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _SegmentedIterator
50+
__fill(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value) {
51+
using __local_iterator_t = typename __segmented_iterator_traits<_SegmentedIterator>::__local_iterator;
52+
std::__for_each_segment(__first, __last, [&](__local_iterator_t __lfirst, __local_iterator_t __llast) {
53+
std::__fill(__lfirst, __llast, __value);
54+
});
55+
return __last;
3556
}
57+
#endif // !_LIBCPP_CXX03_LANG
3658

3759
template <class _ForwardIterator, class _Tp>
3860
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
3961
fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
40-
std::__fill(__first, __last, __value, typename iterator_traits<_ForwardIterator>::iterator_category());
62+
std::__fill(__first, __last, __value);
4163
}
4264

4365
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__algorithm/fill_n.h

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
#ifndef _LIBCPP___ALGORITHM_FILL_N_H
1010
#define _LIBCPP___ALGORITHM_FILL_N_H
1111

12+
#include <__algorithm/for_each_n_segment.h>
1213
#include <__algorithm/min.h>
1314
#include <__config>
1415
#include <__fwd/bit_reference.h>
16+
#include <__iterator/iterator_traits.h>
17+
#include <__iterator/segmented_iterator.h>
1518
#include <__memory/pointer_traits.h>
19+
#include <__type_traits/disjunction.h>
20+
#include <__type_traits/enable_if.h>
21+
#include <__type_traits/negation.h>
1622
#include <__utility/convert_to_integral.h>
1723

1824
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -26,9 +32,39 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2632

2733
// fill_n isn't specialized for std::memset, because the compiler already optimizes the loop to a call to std::memset.
2834

29-
template <class _OutputIterator, class _Size, class _Tp>
35+
template <class _OutputIterator,
36+
class _Size,
37+
class _Tp
38+
#ifndef _LIBCPP_CXX03_LANG
39+
,
40+
__enable_if_t<_Or< _Not<__is_segmented_iterator<_OutputIterator> >,
41+
_Not<__has_random_access_local_iterator<_OutputIterator> > >::value,
42+
int> = 0
43+
#endif
44+
>
3045
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
31-
__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value);
46+
__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) {
47+
for (; __n > 0; ++__first, (void)--__n)
48+
*__first = __value;
49+
return __first;
50+
}
51+
52+
#ifndef _LIBCPP_CXX03_LANG
53+
template < class _OutputIterator,
54+
class _Size,
55+
class _Tp,
56+
__enable_if_t<__is_segmented_iterator<_OutputIterator>::value &&
57+
__has_random_access_iterator_category<
58+
typename __segmented_iterator_traits<_OutputIterator>::__local_iterator>::value,
59+
int> = 0>
60+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _OutputIterator
61+
__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) {
62+
using __local_iterator_t = typename __segmented_iterator_traits<_OutputIterator>::__local_iterator;
63+
return std::__for_each_n_segment(__first, __n, [&](__local_iterator_t __lfirst, __local_iterator_t __llast) {
64+
std::__fill_n(__lfirst, __llast - __lfirst, __value);
65+
});
66+
}
67+
#endif // !_LIBCPP_CXX03_LANG
3268

3369
template <bool _FillVal, class _Cp>
3470
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
@@ -68,14 +104,6 @@ __fill_n(__bit_iterator<_Cp, false> __first, _Size __n, const bool& __value) {
68104
return __first + __n;
69105
}
70106

71-
template <class _OutputIterator, class _Size, class _Tp>
72-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
73-
__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) {
74-
for (; __n > 0; ++__first, (void)--__n)
75-
*__first = __value;
76-
return __first;
77-
}
78-
79107
template <class _OutputIterator, class _Size, class _Tp>
80108
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
81109
fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) {

libcxx/include/__algorithm/ranges_fill.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ namespace ranges {
3131
struct __fill {
3232
template <class _Type, output_iterator<const _Type&> _Iter, sentinel_for<_Iter> _Sent>
3333
_LIBCPP_HIDE_FROM_ABI constexpr _Iter operator()(_Iter __first, _Sent __last, const _Type& __value) const {
34-
if constexpr (random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>) {
35-
return ranges::fill_n(__first, __last - __first, __value);
34+
if constexpr (sized_sentinel_for<_Sent, _Iter>) {
35+
auto __n = __last - __first;
36+
return std::__fill_n(std::move(__first), __n, __value);
3637
} else {
37-
for (; __first != __last; ++__first)
38-
*__first = __value;
39-
return __first;
38+
return std::__fill(std::move(__first), std::move(__last), __value);
4039
}
4140
}
4241

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <array>
1818
#include <cassert>
1919
#include <cstddef>
20+
#include <deque>
21+
#include <ranges>
2022
#include <vector>
2123

2224
#include "sized_allocator.h"
@@ -93,6 +95,13 @@ TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
9395
return true;
9496
}
9597

98+
/*TEST_CONSTEXPR_CXX26*/ void test_deque() { // TODO: Mark as TEST_CONSTEXPR_CXX26 once std::deque is constexpr
99+
std::deque<int> in(20);
100+
std::deque<int> expected(in.size(), 42);
101+
std::fill(in.begin(), in.end(), 42);
102+
assert(in == expected);
103+
}
104+
96105
TEST_CONSTEXPR_CXX20 bool test() {
97106
types::for_each(types::forward_iterator_list<char*>(), Test<char>());
98107
types::for_each(types::forward_iterator_list<int*>(), Test<int>());
@@ -138,6 +147,20 @@ TEST_CONSTEXPR_CXX20 bool test() {
138147
}
139148
}
140149

150+
if (!TEST_IS_CONSTANT_EVALUATED) // TODO: Use TEST_STD_AT_LEAST_26_OR_RUNTIME_EVALUATED when std::deque is made constexpr
151+
test_deque();
152+
153+
#if TEST_STD_VER >= 20
154+
{ // Verify that join_view of vectors work properly.
155+
std::vector<std::vector<int>> v{{1, 2}, {1, 2, 3}, {}, {3, 4, 5}, {6}, {7, 8, 9, 6}, {0, 1, 2, 3, 0, 1, 2}};
156+
auto jv = std::ranges::join_view(v);
157+
std::fill(jv.begin(), jv.end(), 42);
158+
for (const auto& vec : v)
159+
for (auto n : vec)
160+
assert(n == 42);
161+
}
162+
#endif
163+
141164
return true;
142165
}
143166

libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <array>
1818
#include <cassert>
1919
#include <cstddef>
20+
#include <deque>
21+
#include <ranges>
2022
#include <vector>
2123

2224
#include "sized_allocator.h"
@@ -126,6 +128,13 @@ TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
126128
return true;
127129
}
128130

131+
/*TEST_CONSTEXPR_CXX26*/ void test_deque() { // TODO: Mark as TEST_CONSTEXPR_CXX26 once std::deque is constexpr
132+
std::deque<int> in(20);
133+
std::deque<int> expected(in.size(), 42);
134+
std::fill_n(in.begin(), in.size(), 42);
135+
assert(in == expected);
136+
}
137+
129138
TEST_CONSTEXPR_CXX20 bool test() {
130139
types::for_each(types::forward_iterator_list<char*>(), Test<char>());
131140
types::for_each(types::forward_iterator_list<int*>(), Test<int>());
@@ -221,6 +230,20 @@ TEST_CONSTEXPR_CXX20 bool test() {
221230
}
222231
}
223232

233+
if (!TEST_IS_CONSTANT_EVALUATED) // TODO: Use TEST_STD_AT_LEAST_26_OR_RUNTIME_EVALUATED when std::deque is made constexpr
234+
test_deque();
235+
236+
#if TEST_STD_VER >= 20
237+
{
238+
std::vector<std::vector<int>> v{{1, 2}, {1, 2, 3}, {}, {3, 4, 5}, {6}, {7, 8, 9, 6}, {0, 1, 2, 3, 0, 1, 2}};
239+
auto jv = std::ranges::join_view(v);
240+
std::fill_n(jv.begin(), std::distance(jv.begin(), jv.end()), 42);
241+
for (const auto& vec : v)
242+
for (auto n : vec)
243+
assert(n == 42);
244+
}
245+
#endif
246+
224247
return true;
225248
}
226249

libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/ranges.fill.pass.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <algorithm>
1919
#include <array>
2020
#include <cassert>
21+
#include <deque>
2122
#include <ranges>
2223
#include <string>
2324
#include <vector>
@@ -128,6 +129,13 @@ constexpr bool test_vector_bool(std::size_t N) {
128129
}
129130
#endif
130131

132+
/*TEST_CONSTEXPR_CXX26*/ void test_deque() { // TODO: Mark as TEST_CONSTEXPR_CXX26 once std::deque is constexpr
133+
std::deque<int> in(20);
134+
std::deque<int> expected(in.size(), 42);
135+
std::ranges::fill(in, 42);
136+
assert(in == expected);
137+
}
138+
131139
constexpr bool test() {
132140
test_iterators<cpp17_output_iterator<int*>, sentinel_wrapper<cpp17_output_iterator<int*>>>();
133141
test_iterators<cpp20_output_iterator<int*>, sentinel_wrapper<cpp20_output_iterator<int*>>>();
@@ -227,6 +235,20 @@ constexpr bool test() {
227235
}
228236
#endif
229237

238+
if (!TEST_IS_CONSTANT_EVALUATED) // TODO: Use TEST_STD_AT_LEAST_26_OR_RUNTIME_EVALUATED when std::deque is made constexpr
239+
test_deque();
240+
241+
#if TEST_STD_VER >= 20
242+
{
243+
std::vector<std::vector<int>> v{{1, 2}, {1, 2, 3}, {}, {3, 4, 5}, {6}, {7, 8, 9, 6}, {0, 1, 2, 3, 0, 1, 2}};
244+
auto jv = std::ranges::join_view(v);
245+
std::ranges::fill(jv, 42);
246+
for (const auto& vec : v)
247+
for (auto n : vec)
248+
assert(n == 42);
249+
}
250+
#endif
251+
230252
return true;
231253
}
232254

libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/ranges.fill_n.pass.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <algorithm>
1717
#include <array>
1818
#include <cassert>
19+
#include <deque>
1920
#include <ranges>
2021
#include <string>
2122
#include <vector>
@@ -101,6 +102,13 @@ constexpr bool test_vector_bool(std::size_t N) {
101102
}
102103
#endif
103104

105+
/*TEST_CONSTEXPR_CXX26*/ void test_deque() { // TODO: Mark as TEST_CONSTEXPR_CXX26 once std::deque is constexpr
106+
std::deque<int> in(20);
107+
std::deque<int> expected(in.size(), 42);
108+
std::ranges::fill_n(std::ranges::begin(in), std::ranges::size(in), 42);
109+
assert(in == expected);
110+
}
111+
104112
constexpr bool test() {
105113
test_iterators<cpp17_output_iterator<int*>, sentinel_wrapper<cpp17_output_iterator<int*>>>();
106114
test_iterators<cpp20_output_iterator<int*>, sentinel_wrapper<cpp20_output_iterator<int*>>>();
@@ -175,6 +183,20 @@ constexpr bool test() {
175183
}
176184
#endif
177185

186+
if (!TEST_IS_CONSTANT_EVALUATED) // TODO: Use TEST_STD_AT_LEAST_26_OR_RUNTIME_EVALUATED when std::deque is made constexpr
187+
test_deque();
188+
189+
#if TEST_STD_VER >= 20
190+
{
191+
std::vector<std::vector<int>> v{{1, 2}, {1, 2, 3}, {}, {3, 4, 5}, {6}, {7, 8, 9, 6}, {0, 1, 2, 3, 0, 1, 2}};
192+
auto jv = std::ranges::join_view(v);
193+
std::ranges::fill_n(std::ranges::begin(jv), std::ranges::distance(jv), 42);
194+
for (const auto& vec : v)
195+
for (auto n : vec)
196+
assert(n == 42);
197+
}
198+
#endif
199+
178200
return true;
179201
}
180202

0 commit comments

Comments
 (0)