@@ -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
@@ -692,25 +694,30 @@ vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const alloca
692694
693695#endif // _LIBCPP_CXX03_LANG
694696
697+ // This function copies each storage word as a whole, which is substantially more efficient than copying
698+ // individual bits within each word
699+ template <class _Allocator >
700+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void vector<bool , _Allocator>::__alloc_and_copy(const vector& __v) {
701+ if (__v.__size_ ) {
702+ __vallocate (__v.__size_ );
703+ std::copy (__v.__begin_ , __v.__begin_ + __external_cap_to_internal (__v.__size_ ), __begin_);
704+ }
705+ __size_ = __v.__size_ ;
706+ }
707+
695708template <class _Allocator >
696709_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool , _Allocator>::vector(const vector& __v)
697710 : __begin_(nullptr ),
698711 __size_ (0 ),
699712 __cap_(0 ),
700713 __alloc_(__storage_traits::select_on_container_copy_construction(__v.__alloc_)) {
701- if (__v.size () > 0 ) {
702- __vallocate (__v.size ());
703- __construct_at_end (__v.begin (), __v.end (), __v.size ());
704- }
714+ __alloc_and_copy (__v);
705715}
706716
707717template <class _Allocator >
708718_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool , _Allocator>::vector(const vector& __v, const allocator_type& __a)
709719 : __begin_(nullptr ), __size_(0 ), __cap_(0 ), __alloc_(__a) {
710- if (__v.size () > 0 ) {
711- __vallocate (__v.size ());
712- __construct_at_end (__v.begin (), __v.end (), __v.size ());
713- }
720+ __alloc_and_copy (__v);
714721}
715722
716723template <class _Allocator >
@@ -755,9 +762,8 @@ vector<bool, _Allocator>::vector(vector&& __v, const __type_identity_t<allocator
755762 this ->__cap_ = __v.__cap_ ;
756763 __v.__begin_ = nullptr ;
757764 __v.__cap_ = __v.__size_ = 0 ;
758- } else if (__v.size () > 0 ) {
759- __vallocate (__v.size ());
760- __construct_at_end (__v.begin (), __v.end (), __v.size ());
765+ } else {
766+ __alloc_and_copy (__v);
761767 }
762768}
763769
0 commit comments