[libcxx-commits] [libcxx] [libc++] Simplify vector<bool>::__construct_at_end (Reopend) (PR #119632)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Feb 2 09:58:55 PST 2025
================
@@ -552,36 +552,29 @@ vector<bool, _Allocator>::__recommend(size_type __new_size) const {
}
// Default constructs __n objects starting at __end_
-// Precondition: __n > 0
// Precondition: size() + __n <= capacity()
// Postcondition: size() == size() + __n
template <class _Allocator>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x) {
- size_type __old_size = this->__size_;
+ _LIBCPP_ASSERT_INTERNAL(
+ capacity() >= size() + __n, "vector<bool>::__construct_at_end called with insufficient capacity");
+ std::fill_n(end(), __n, __x);
this->__size_ += __n;
- if (__old_size == 0 || ((__old_size - 1) / __bits_per_word) != ((this->__size_ - 1) / __bits_per_word)) {
- if (this->__size_ <= __bits_per_word)
- this->__begin_[0] = __storage_type(0);
- else
- this->__begin_[(this->__size_ - 1) / __bits_per_word] = __storage_type(0);
- }
- std::fill_n(__make_iter(__old_size), __n, __x);
+ 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);
----------------
winner245 wrote:
This ensures that the unused leading bits in the last word are initialized, which serves the same purpose as the pre-initialization logic in the previous implementation:
https://github.com/llvm/llvm-project/blob/743c84bb9b79ed70d9bed926c2a173db3b30f587/libcxx/include/__vector/vector_bool.h#L554-L561
Without this, we would have a Msan CI failure.
https://github.com/llvm/llvm-project/pull/119632
More information about the libcxx-commits
mailing list