@@ -417,6 +417,8 @@ class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator> {
417417 __guard.__complete ();
418418 }
419419
420+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __alloc_and_copy (const vector& __v);
421+
420422 template <class _Iterator , class _Sentinel >
421423 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel (_Iterator __first, _Sentinel __last);
422424
@@ -695,25 +697,30 @@ vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const alloca
695697
696698#endif // _LIBCPP_CXX03_LANG
697699
700+ // This function copies each storage word as a whole, which is substantially more efficient than copying
701+ // individual bits within each word
702+ template <class _Allocator >
703+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void vector<bool , _Allocator>::__alloc_and_copy(const vector& __v) {
704+ if (__v.__size_ ) {
705+ __vallocate (__v.__size_ );
706+ std::copy (__v.__begin_ , __v.__begin_ + __external_cap_to_internal (__v.__size_ ), __begin_);
707+ }
708+ __size_ = __v.__size_ ;
709+ }
710+
698711template <class _Allocator >
699712_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool , _Allocator>::vector(const vector& __v)
700713 : __begin_(nullptr ),
701714 __size_ (0 ),
702715 __cap_(0 ),
703716 __alloc_(__storage_traits::select_on_container_copy_construction(__v.__alloc_)) {
704- if (__v.size () > 0 ) {
705- __vallocate (__v.size ());
706- __construct_at_end (__v.begin (), __v.end (), __v.size ());
707- }
717+ __alloc_and_copy (__v);
708718}
709719
710720template <class _Allocator >
711721_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool , _Allocator>::vector(const vector& __v, const allocator_type& __a)
712722 : __begin_(nullptr ), __size_(0 ), __cap_(0 ), __alloc_(__a) {
713- if (__v.size () > 0 ) {
714- __vallocate (__v.size ());
715- __construct_at_end (__v.begin (), __v.end (), __v.size ());
716- }
723+ __alloc_and_copy (__v);
717724}
718725
719726template <class _Allocator >
@@ -758,9 +765,8 @@ vector<bool, _Allocator>::vector(vector&& __v, const __type_identity_t<allocator
758765 this ->__cap_ = __v.__cap_ ;
759766 __v.__begin_ = nullptr ;
760767 __v.__cap_ = __v.__size_ = 0 ;
761- } else if (__v.size () > 0 ) {
762- __vallocate (__v.size ());
763- __construct_at_end (__v.begin (), __v.end (), __v.size ());
768+ } else {
769+ __alloc_and_copy (__v);
764770 }
765771}
766772
0 commit comments