Commit d0cee69
[libc++] Optimize std::{,ranges}::{fill,fill_n} for segmented iterators (#132665)
This patch optimizes `std::fill`, `std::fill_n`, `std::ranges::fill`,
and `std::ranges::fill_n` for segmented iterators, achieving substantial
performance improvements. Specifically, for `deque<int>` iterators, the
performance improvements are above 10x for all these algorithms. The
optimization also enables filling segmented memory of `deque<int>` to
approach the performance of filling contiguous memory of `vector<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`
```
-----------------------------------------------------------------------------
Benchmark Before After Speedup
-----------------------------------------------------------------------------
std::fill_n(deque<int>)/32 11.4 ns 2.28 ns 5.0x
std::fill_n(deque<int>)/50 19.7 ns 3.40 ns 5.8x
std::fill_n(deque<int>)/1024 391 ns 37.3 ns 10.5x
std::fill_n(deque<int>)/8192 3174 ns 301 ns 10.5x
std::fill_n(deque<int>)/65536 26504 ns 2951 ns 9.0x
std::fill_n(deque<int>)/1048576 407960 ns 80658 ns 5.1x
rng::fill_n(deque<int>)/32 14.3 ns 2.15 ns 6.6x
rng::fill_n(deque<int>)/50 20.2 ns 3.22 ns 6.3x
rng::fill_n(deque<int>)/1024 381 ns 37.8 ns 10.1x
rng::fill_n(deque<int>)/8192 3101 ns 294 ns 10.5x
rng::fill_n(deque<int>)/65536 25098 ns 2926 ns 8.6x
rng::fill_n(deque<int>)/1048576 394342 ns 78874 ns 5.0x
std::fill_n(vector<int>)/32 1.76 ns 1.72 ns 1.0x
std::fill_n(vector<int>)/50 3.00 ns 2.73 ns 1.1x
std::fill_n(vector<int>)/1024 38.4 ns 37.9 ns 1.0x
std::fill_n(vector<int>)/8192 258 ns 252 ns 1.0x
std::fill_n(vector<int>)/65536 2993 ns 2889 ns 1.0x
std::fill_n(vector<int>)/1048576 80328 ns 80468 ns 1.0x
rng::fill_n(vector<int>)/32 1.99 ns 1.35 ns 1.5x
rng::fill_n(vector<int>)/50 2.66 ns 2.12 ns 1.3x
rng::fill_n(vector<int>)/1024 37.7 ns 35.8 ns 1.1x
rng::fill_n(vector<int>)/8192 253 ns 250 ns 1.0x
rng::fill_n(vector<int>)/65536 2922 ns 2930 ns 1.0x
rng::fill_n(vector<int>)/1048576 79739 ns 79742 ns 1.0x
```
#### `fill`
```
--------------------------------------------------------------------------
Benchmark Before After Speedup
--------------------------------------------------------------------------
std::fill(deque<int>)/32 13.7 ns 2.45 ns 5.6x
std::fill(deque<int>)/50 21.7 ns 4.57 ns 4.7x
std::fill(deque<int>)/1024 367 ns 38.5 ns 9.5x
std::fill(deque<int>)/8192 2896 ns 247 ns 11.7x
std::fill(deque<int>)/65536 23723 ns 2907 ns 8.2x
std::fill(deque<int>)/1048576 379043 ns 79885 ns 4.7x
rng::fill(deque<int>)/32 13.6 ns 2.70 ns 5.0x
rng::fill(deque<int>)/50 23.4 ns 3.94 ns 5.9x
rng::fill(deque<int>)/1024 377 ns 37.9 ns 9.9x
rng::fill(deque<int>)/8192 2914 ns 286 ns 10.2x
rng::fill(deque<int>)/65536 23612 ns 2939 ns 8.0x
rng::fill(deque<int>)/1048576 379841 ns 80079 ns 4.7x
std::fill(vector<int>)/32 1.99 ns 1.79 ns 1.1x
std::fill(vector<int>)/50 3.05 ns 3.06 ns 1.0x
std::fill(vector<int>)/1024 37.6 ns 38.0 ns 1.0x
std::fill(vector<int>)/8192 255 ns 257 ns 1.0x
std::fill(vector<int>)/65536 2966 ns 2981 ns 1.0x
std::fill(vector<int>)/1048576 78300 ns 80348 ns 1.0x
rng::fill(vector<int>)/32 1.77 ns 1.75 ns 1.0x
rng::fill(vector<int>)/50 4.85 ns 2.31 ns 2.1x
rng::fill(vector<int>)/1024 39.6 ns 36.1 ns 1.1x
rng::fill(vector<int>)/8192 238 ns 251 ns 0.9x
rng::fill(vector<int>)/65536 2941 ns 2918 ns 1.0x
rng::fill(vector<int>)/1048576 80497 ns 80442 ns 1.0x
```
---------
Co-authored-by: Louis Dionne <[email protected]>
Co-authored-by: A. Jiang <[email protected]>1 parent 65c895d commit d0cee69
File tree
8 files changed
+166
-24
lines changed- libcxx
- docs/ReleaseNotes
- include/__algorithm
- test/std/algorithms/alg.modifying.operations/alg.fill
8 files changed
+166
-24
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
74 | 77 | | |
75 | 78 | | |
76 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
| 16 | + | |
| 17 | + | |
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
| |||
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
24 | | - | |
25 | | - | |
26 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
27 | 30 | | |
28 | 31 | | |
| 32 | + | |
29 | 33 | | |
30 | 34 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
35 | 54 | | |
| 55 | + | |
36 | 56 | | |
37 | 57 | | |
38 | 58 | | |
39 | 59 | | |
40 | | - | |
| 60 | + | |
41 | 61 | | |
42 | 62 | | |
43 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| 16 | + | |
| 17 | + | |
15 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
16 | 23 | | |
17 | 24 | | |
18 | 25 | | |
| |||
26 | 33 | | |
27 | 34 | | |
28 | 35 | | |
29 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
30 | 46 | | |
31 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
32 | 68 | | |
33 | 69 | | |
34 | 70 | | |
| |||
68 | 104 | | |
69 | 105 | | |
70 | 106 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | 107 | | |
80 | 108 | | |
81 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| |||
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
34 | | - | |
35 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
36 | 39 | | |
37 | | - | |
38 | | - | |
39 | | - | |
| 40 | + | |
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| |||
Lines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
93 | 95 | | |
94 | 96 | | |
95 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
96 | 105 | | |
97 | 106 | | |
98 | 107 | | |
| |||
138 | 147 | | |
139 | 148 | | |
140 | 149 | | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
141 | 164 | | |
142 | 165 | | |
143 | 166 | | |
| |||
Lines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
126 | 128 | | |
127 | 129 | | |
128 | 130 | | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
129 | 138 | | |
130 | 139 | | |
131 | 140 | | |
| |||
221 | 230 | | |
222 | 231 | | |
223 | 232 | | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
224 | 247 | | |
225 | 248 | | |
226 | 249 | | |
| |||
Lines changed: 22 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
128 | 129 | | |
129 | 130 | | |
130 | 131 | | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
131 | 139 | | |
132 | 140 | | |
133 | 141 | | |
| |||
227 | 235 | | |
228 | 236 | | |
229 | 237 | | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
230 | 252 | | |
231 | 253 | | |
232 | 254 | | |
| |||
Lines changed: 22 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
104 | 112 | | |
105 | 113 | | |
106 | 114 | | |
| |||
175 | 183 | | |
176 | 184 | | |
177 | 185 | | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
178 | 200 | | |
179 | 201 | | |
180 | 202 | | |
| |||
0 commit comments