Nikolas Klauser b97b1f2f49
[libc++] Optimize vector<bool>::resize() (#172853)
This both simplifies the implementation and improves the performance,
since the compiler is better able to see through what's going on.

```
Benchmark                                                                              old             new    Difference    % Difference
--------------------------------------------------------------------------  --------------  --------------  ------------  --------------
vector<bool>(const_vector<bool>&)                                                    11.99           12.26          0.27           2.25%
vector<bool>(size_type,_const_value_type&)                                            9.24            9.29          0.05           0.54%
vector<bool>(vector<bool>&&,_const_allocator_type&)_(different_allocators)           14.26           14.35          0.09           0.65%
vector<bool>(vector<bool>&&,_const_allocator_type&)_(equal_allocators)                2.67            2.67         -0.01          -0.29%
vector<bool>::reserve()                                                               9.30            9.29         -0.01          -0.12%
vector<bool>::resize()                                                               15.14           13.43         -1.71         -11.28%
Geomean                                                                               9.17            9.03         -0.14          -1.48%
2025-12-20 10:52:48 +01:00

81 lines
2.5 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <benchmark/benchmark.h>
#include <memory_resource>
#include <vector>
#include "test_macros.h"
static void BM_vector_bool_copy_ctor(benchmark::State& state) {
std::vector<bool> vec(100, true);
for (auto _ : state) {
benchmark::DoNotOptimize(vec);
std::vector<bool> vec2(vec);
benchmark::DoNotOptimize(vec2);
}
}
BENCHMARK(BM_vector_bool_copy_ctor)->Name("vector<bool>(const vector<bool>&)");
static void BM_vector_bool_move_ctor_alloc_equal(benchmark::State& state) {
std::vector<bool> vec(100, true);
for (auto _ : state) {
benchmark::DoNotOptimize(vec);
std::vector<bool> vec2(std::move(vec), std::allocator<bool>());
benchmark::DoNotOptimize(vec2);
swap(vec, vec2);
}
}
BENCHMARK(BM_vector_bool_move_ctor_alloc_equal)
->Name("vector<bool>(vector<bool>&&, const allocator_type&) (equal allocators)");
#if TEST_STD_VER >= 17
static void BM_vector_bool_move_ctor_alloc_different(benchmark::State& state) {
std::pmr::monotonic_buffer_resource resource;
std::pmr::vector<bool> vec(100, true, &resource);
for (auto _ : state) {
benchmark::DoNotOptimize(vec);
std::pmr::vector<bool> vec2(std::move(vec), std::pmr::new_delete_resource());
benchmark::DoNotOptimize(vec2);
}
}
BENCHMARK(BM_vector_bool_move_ctor_alloc_different)
->Name("vector<bool>(vector<bool>&&, const allocator_type&) (different allocators)");
#endif
static void BM_vector_bool_size_ctor(benchmark::State& state) {
for (auto _ : state) {
std::vector<bool> vec(100, true);
benchmark::DoNotOptimize(vec);
}
}
BENCHMARK(BM_vector_bool_size_ctor)->Name("vector<bool>(size_type, const value_type&)");
static void BM_vector_bool_reserve(benchmark::State& state) {
for (auto _ : state) {
std::vector<bool> vec;
vec.reserve(100);
benchmark::DoNotOptimize(vec);
}
}
BENCHMARK(BM_vector_bool_reserve)->Name("vector<bool>::reserve()");
static void BM_vector_bool_resize(benchmark::State& state) {
for (auto _ : state) {
std::vector<bool> vec;
vec.resize(100);
benchmark::DoNotOptimize(vec);
}
}
BENCHMARK(BM_vector_bool_resize)->Name("vector<bool>::resize()");
BENCHMARK_MAIN();