@@ -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
@@ -688,25 +690,30 @@ vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const alloca
688690
689691#endif // _LIBCPP_CXX03_LANG
690692
693+ // This function copies each storage word as a whole, which is substantially more efficient than copying
694+ // individual bits within each word
695+ template <class _Allocator >
696+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void vector<bool , _Allocator>::__alloc_and_copy(const vector& __v) {
697+ if (__v.__size_ ) {
698+ __vallocate (__v.__size_ );
699+ std::copy (__v.__begin_ , __v.__begin_ + __external_cap_to_internal (__v.__size_ ), __begin_);
700+ }
701+ __size_ = __v.__size_ ;
702+ }
703+
691704template <class _Allocator >
692705_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool , _Allocator>::vector(const vector& __v)
693706 : __begin_(nullptr ),
694707 __size_ (0 ),
695708 __cap_(0 ),
696709 __alloc_(__storage_traits::select_on_container_copy_construction(__v.__alloc_)) {
697- if (__v.size () > 0 ) {
698- __vallocate (__v.size ());
699- __construct_at_end (__v.begin (), __v.end (), __v.size ());
700- }
710+ __alloc_and_copy (__v);
701711}
702712
703713template <class _Allocator >
704714_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool , _Allocator>::vector(const vector& __v, const allocator_type& __a)
705715 : __begin_(nullptr ), __size_(0 ), __cap_(0 ), __alloc_(__a) {
706- if (__v.size () > 0 ) {
707- __vallocate (__v.size ());
708- __construct_at_end (__v.begin (), __v.end (), __v.size ());
709- }
716+ __alloc_and_copy (__v);
710717}
711718
712719template <class _Allocator >
@@ -751,9 +758,8 @@ vector<bool, _Allocator>::vector(vector&& __v, const __type_identity_t<allocator
751758 this ->__cap_ = __v.__cap_ ;
752759 __v.__begin_ = nullptr ;
753760 __v.__cap_ = __v.__size_ = 0 ;
754- } else if (__v.size () > 0 ) {
755- __vallocate (__v.size ());
756- __construct_at_end (__v.begin (), __v.end (), __v.size ());
761+ } else {
762+ __alloc_and_copy (__v);
757763 }
758764}
759765
0 commit comments