Skip to content

Commit 345ac67

Browse files
committed
Fix insertion benchmarks, hopefully
1 parent d7c0679 commit 345ac67

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

libcxx/test/benchmarks/containers/container_benchmarks.h

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ void BM_insert_middle(benchmark::State& st, Generator gen) {
200200
}
201201
}
202202

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.
203205
template <class Container, class Generator>
204206
void BM_insert_begin_input_iter_with_reserve_no_realloc(benchmark::State& st, Generator gen) {
205207
using ValueType = typename Container::value_type;
@@ -225,8 +227,11 @@ void BM_insert_begin_input_iter_with_reserve_no_realloc(benchmark::State& st, Ge
225227
}
226228
}
227229

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.
228233
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) {
230235
using ValueType = typename Container::value_type;
231236
const int size = st.range(0);
232237
std::vector<ValueType> in;
@@ -235,19 +240,24 @@ void BM_insert_begin_input_iter_with_reserve_half_filled(benchmark::State& st, G
235240
auto first = in.data();
236241
auto last = in.data() + in.size();
237242

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);
245247

248+
for (auto _ : st) {
246249
c.insert(c.begin(), cpp17_input_iterator(first), cpp17_input_iterator(last));
247250
DoNotOptimizeData(c);
251+
252+
st.PauseTiming();
253+
c.erase(c.begin() + overflow, c.end()); // avoid growing indefinitely
254+
st.ResumeTiming();
248255
}
249256
}
250257

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.
251261
template <class Container, class Generator>
252262
void BM_insert_begin_input_iter_with_reserve_near_full(benchmark::State& st, Generator gen) {
253263
using ValueType = typename Container::value_type;
@@ -258,16 +268,18 @@ void BM_insert_begin_input_iter_with_reserve_near_full(benchmark::State& st, Gen
258268
auto first = in.data();
259269
auto last = in.data() + in.size();
260270

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);
268275

276+
for (auto _ : st) {
269277
c.insert(c.begin(), cpp17_input_iterator(first), cpp17_input_iterator(last));
270278
DoNotOptimizeData(c);
279+
280+
st.PauseTiming();
281+
c.erase(c.begin() + overflow, c.end()); // avoid growing indefinitely
282+
st.ResumeTiming();
271283
}
272284
}
273285

@@ -421,7 +433,7 @@ void sequence_container_benchmarks(std::string container) {
421433
for (auto gen : generators)
422434
benchmark::RegisterBenchmark(
423435
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); })
425437
->Arg(1024);
426438
for (auto gen : generators)
427439
benchmark::RegisterBenchmark(

0 commit comments

Comments
 (0)