@@ -200,6 +200,8 @@ void BM_insert_middle(benchmark::State& st, Generator gen) {
200
200
}
201
201
}
202
202
203
+ // Insert at the start of a vector in a scenario where the vector already
204
+ // has enough capacity to hold all the elements we are inserting.
203
205
template <class Container , class Generator >
204
206
void BM_insert_begin_input_iter_with_reserve_no_realloc (benchmark::State& st, Generator gen) {
205
207
using ValueType = typename Container::value_type;
@@ -225,8 +227,11 @@ void BM_insert_begin_input_iter_with_reserve_no_realloc(benchmark::State& st, Ge
225
227
}
226
228
}
227
229
230
+ // Insert at the start of a vector in a scenario where the vector already
231
+ // has almost enough capacity to hold all the elements we are inserting,
232
+ // but does need to reallocate.
228
233
template <class Container , class Generator >
229
- void BM_insert_begin_input_iter_with_reserve_half_filled (benchmark::State& st, Generator gen) {
234
+ void BM_insert_begin_input_iter_with_reserve_almost_no_realloc (benchmark::State& st, Generator gen) {
230
235
using ValueType = typename Container::value_type;
231
236
const int size = st.range (0 );
232
237
std::vector<ValueType> in;
@@ -235,19 +240,24 @@ void BM_insert_begin_input_iter_with_reserve_half_filled(benchmark::State& st, G
235
240
auto first = in.data ();
236
241
auto last = in.data () + in.size ();
237
242
238
- for (auto _ : st) {
239
- st.PauseTiming ();
240
- // Half the elements in [beg, end) can fit in the vector without reallocation, so we'll reallocate halfway through
241
- Container c;
242
- c.reserve (size);
243
- std::generate_n (std::back_inserter (c), size / 2 , gen);
244
- st.ResumeTiming ();
243
+ const int overflow = size / 10 ; // 10% of elements won't fit in the vector when we insert
244
+ Container c;
245
+ c.reserve (size);
246
+ std::generate_n (std::back_inserter (c), overflow, gen);
245
247
248
+ for (auto _ : st) {
246
249
c.insert (c.begin (), cpp17_input_iterator (first), cpp17_input_iterator (last));
247
250
DoNotOptimizeData (c);
251
+
252
+ st.PauseTiming ();
253
+ c.erase (c.begin () + overflow, c.end ()); // avoid growing indefinitely
254
+ st.ResumeTiming ();
248
255
}
249
256
}
250
257
258
+ // Insert at the start of a vector in a scenario where the vector can fit a few
259
+ // more elements, but needs to reallocate almost immediately to fit the remaining
260
+ // elements.
251
261
template <class Container , class Generator >
252
262
void BM_insert_begin_input_iter_with_reserve_near_full (benchmark::State& st, Generator gen) {
253
263
using ValueType = typename Container::value_type;
@@ -258,16 +268,18 @@ void BM_insert_begin_input_iter_with_reserve_near_full(benchmark::State& st, Gen
258
268
auto first = in.data ();
259
269
auto last = in.data () + in.size ();
260
270
261
- for (auto _ : st) {
262
- st.PauseTiming ();
263
- // Create an almost full container
264
- Container c;
265
- c.reserve (size + 5 );
266
- std::generate_n (std::back_inserter (c), size, gen);
267
- st.ResumeTiming ();
271
+ const int overflow = 9 * (size / 10 ); // 90% of elements won't fit in the vector when we insert
272
+ Container c;
273
+ c.reserve (size);
274
+ std::generate_n (std::back_inserter (c), overflow, gen);
268
275
276
+ for (auto _ : st) {
269
277
c.insert (c.begin (), cpp17_input_iterator (first), cpp17_input_iterator (last));
270
278
DoNotOptimizeData (c);
279
+
280
+ st.PauseTiming ();
281
+ c.erase (c.begin () + overflow, c.end ()); // avoid growing indefinitely
282
+ st.ResumeTiming ();
271
283
}
272
284
}
273
285
@@ -421,7 +433,7 @@ void sequence_container_benchmarks(std::string container) {
421
433
for (auto gen : generators)
422
434
benchmark::RegisterBenchmark (
423
435
container + " ::insert(begin, input-iter, input-iter) (half filled)" + tostr (gen),
424
- [=](auto & st) { BM_insert_begin_input_iter_with_reserve_half_filled <Container>(st, gen); })
436
+ [=](auto & st) { BM_insert_begin_input_iter_with_reserve_almost_no_realloc <Container>(st, gen); })
425
437
->Arg (1024 );
426
438
for (auto gen : generators)
427
439
benchmark::RegisterBenchmark (
0 commit comments