[libcxx-commits] [libcxx] [libc++] Speed up vector<bool> copy/move-ctors [1/3] (PR #120132)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Feb 2 08:58:14 PST 2025
winner245 wrote:
> > LGTM with comments applied. I would also like @philnik777 to take another look.
> >
> >Personally, I find the code a lot easier to read after this PR. We call `std::copy` directly, which everyone knows well, and we set the size explicitly from the constructor. That removes the need for calling `__construct_at_end`, which has a strange contract like that of only being called when the vector is empty.
>
>What strange contract are you talking about? AFAICT the only precondition is that there is enough space to construct the elements.
Before my patch #119632, `__construct_at_end(,,__n)` had a documented precondition of `__n > 0`, alongside an undocumented precondition of `size() > 0`. After applying #119632, we simplified this into a single precondition `size() + __n <= capacity()`.
> I don't really care whether we call `__construct_at_end` or use `std::copy` directly, but I still find `std::copy(__v.begin(), __v.end(), begin())` _much_ more readable than the current proposal. If the only reason for this PR is readability I don't see how the current proposal is any better than the status quo.
Please note that we are not comparing `std::copy(__v.begin(), __v.end(), begin())` with `std::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.size()), __begin_);`, but rather
```cpp
__construct_at_end(__v.begin(), __v.end(), __v.size());
```
with
```cpp
std::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.size()), __begin_);
__size_ = __v.size();
```
I have no idea what `__construct_at_end` does without delving into its implementation, which has a contract and multiple lines of code that invoke specialized versions of `std::__copy`. To fully understand what `__construct_at_end` does, one has to figure out a lot more internal implementation details such as `__bit_iterator`, `__bit_reference`, and the optimized algorithms of `std::__copy` for `__bit_iterator`. In contrast, with this PR, one only needs to know the general purpose standard-form `std::copy`, which every C++ programmer is familiar with.
https://github.com/llvm/llvm-project/pull/120132
More information about the libcxx-commits
mailing list