diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h index 7b82906769255..66f5fd9498eec 100644 --- a/libcxx/include/__vector/vector_bool.h +++ b/libcxx/include/__vector/vector_bool.h @@ -478,7 +478,6 @@ class vector { return (__new_size + (__bits_per_word - 1)) & ~((size_type)__bits_per_word - 1); } _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend(size_type __new_size) const; - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_at_end(size_type __n, bool __x); template _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n); @@ -567,20 +566,6 @@ vector::__recommend(size_type __new_size) const { return std::max(2 * __cap, __align_it(__new_size)); } -// Default constructs __n objects starting at __end_ -// Precondition: size() + __n <= capacity() -// Postcondition: size() == size() + __n -template -inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void -vector::__construct_at_end(size_type __n, bool __x) { - _LIBCPP_ASSERT_INTERNAL( - capacity() >= size() + __n, "vector::__construct_at_end called with insufficient capacity"); - std::fill_n(end(), __n, __x); - this->__size_ += __n; - if (end().__ctz_ != 0) // Ensure uninitialized leading bits in the last word are set to zero - std::fill_n(end(), __bits_per_word - end().__ctz_, 0); -} - template template _LIBCPP_CONSTEXPR_SINCE_CXX20 void @@ -613,7 +598,8 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 vector::vector(size_type __n) : __begin_(nullptr), __size_(0), __cap_(0) { if (__n > 0) { __vallocate(__n); - __construct_at_end(__n, false); + std::fill_n(__begin_, __external_cap_to_internal(__n), __storage_type(0)); + __size_ = __n; } } @@ -623,7 +609,8 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 vector::vector(size_type __n, co : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(static_cast<__storage_allocator>(__a)) { if (__n > 0) { __vallocate(__n); - __construct_at_end(__n, false); + std::fill_n(__begin_, __external_cap_to_internal(__n), __storage_type(0)); + __size_ = __n; } } #endif @@ -633,7 +620,8 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 vector::vector(size_type __n, co : __begin_(nullptr), __size_(0), __cap_(0) { if (__n > 0) { __vallocate(__n); - __construct_at_end(__n, __x); + std::fill_n(__begin_, __external_cap_to_internal(__n), __storage_type(0) - __x); + __size_ = __n; } } @@ -643,7 +631,8 @@ vector::vector(size_type __n, const value_type& __x, const all : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(static_cast<__storage_allocator>(__a)) { if (__n > 0) { __vallocate(__n); - __construct_at_end(__n, __x); + std::fill_n(__begin_, __external_cap_to_internal(__n), __storage_type(0) - __x); + __size_ = __n; } } diff --git a/libcxx/test/benchmarks/containers/sequence/vector_bool.bench.cpp b/libcxx/test/benchmarks/containers/sequence/vector_bool.bench.cpp new file mode 100644 index 0000000000000..6ecb268208cc8 --- /dev/null +++ b/libcxx/test/benchmarks/containers/sequence/vector_bool.bench.cpp @@ -0,0 +1,20 @@ +//===----------------------------------------------------------------------===// +// +// 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 +#include + +static void BM_vector_bool_size_ctor(benchmark::State& state) { + for (auto _ : state) { + std::vector vec(100, true); + benchmark::DoNotOptimize(vec); + } +} +BENCHMARK(BM_vector_bool_size_ctor); + +BENCHMARK_MAIN();