From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id F2F93385C6C4 for ; Mon, 20 Nov 2023 12:09:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F2F93385C6C4 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F2F93385C6C4 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700482190; cv=none; b=HItE1MKAulnLua00SSrlpK5hD8nuk+3DL+B+Rx3U+e3E1DbbEujWnRBNcJWIQgKR0IzkKGBqM91BEvEQluXhajdaML6RyaRRTPFg4vK4dxTLub9a9zSIAdjJjlRzqSazEYdloKdvIDYemdWdxPh3M6hUsAUq/5g8o/cxLSxCsR4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700482190; c=relaxed/simple; bh=wsuIyDXorbbxH56ZRtoZcY98/903q5ozKt3DdYIB7M8=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=UEqNRa41t1J+4S6Ko0q9+G/Lj2RNYGNjSUu3bm6Gda/PlqWbhnvPjoiTKSLus/A6Q/GkumbUZPm5kcGDR+A9BKEcEyIBlOy8A2Ic7YCVqjjtfik5uqpdClQ/EH5Rbl+S4Fmdcvaw2oUQQ6teCAQ+TpbxIKmFXnkP9wKIaGIHa20= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700482188; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=UB2vUsPtwM9BMMSg+TlAs4jvmZjmKBjIkbshmhl++fU=; b=d+Y9zvOTOYz7uAqQ+q3+i9pZFT9crabxrN+2/RUWEpANaByzQA5TyHTaoUgpqYIA86ejs3 zhvJRviSsEhJH3dQHAtGs+711mSARkPCfloxtg+dj1il4OT+fDzEwreQshqMyqOEtby0tF JhMV0TiO5ycyzGZZ6fM0B7OdszrXTv0= Received: from mail-yb1-f198.google.com (mail-yb1-f198.google.com [209.85.219.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-33-Cfmrfmz-OPuYASUiSPLchw-1; Mon, 20 Nov 2023 07:09:47 -0500 X-MC-Unique: Cfmrfmz-OPuYASUiSPLchw-1 Received: by mail-yb1-f198.google.com with SMTP id 3f1490d57ef6-da1aa98ec19so5229207276.2 for ; Mon, 20 Nov 2023 04:09:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700482187; x=1701086987; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UB2vUsPtwM9BMMSg+TlAs4jvmZjmKBjIkbshmhl++fU=; b=vrvr+4SjWNXzvY7vbMkipnrTrtDTro3koTDXLt1loSto1iIsgHVsq9lAGsaymr9rno +cOF/rnO3wjwKd0p3MI+6FocHeEHf52d4SHZbIaeaS8jigFeov6++VOfVkaCG5h4RUBC GRsD3QAuPqoRcz5f4OERB7AE1DV8YxBtiMD9u7XFa9PSlWhcMsWm2eqBd+8P2/KVLthp diefNs37yU6k/oVy/WGn76p+A1ZJ6wyHEFGDEVrizT7xE/2jnpQF7Jnh76cqC2c3lfL0 zI/+RGYtkSxLCCRtFu6KNt2ZpWVBtwCdCXb9yOHpE7M+Ux2brahSI7efqR3vHt+37Zql K1Uw== X-Gm-Message-State: AOJu0YwCTwXQy1YbgVC2ISUco3RCq8TZIPzRN3WUtMF5bUg8maz7r6jx 4sjqzYqge9B6bd5vRUfkBGVMgWa/I5PX2+jg/CQTYGCHyUbsJQXeW3oHTgbIHPSQ4LwB8EtM74s JgiJ//I7tI7OqDngXrk43CeQWJaQxolc7Jw== X-Received: by 2002:a05:6902:100b:b0:da3:7c6b:acca with SMTP id w11-20020a056902100b00b00da37c6baccamr7715622ybt.28.1700482186890; Mon, 20 Nov 2023 04:09:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IH9GsPffZaPcrfDSC9NAa7dh+1SPo/za2Eba38wEh//qzPMyICucRQ+uDetsO7CdQJFWBJQkA6nbO7gFMN1KGc= X-Received: by 2002:a05:6902:100b:b0:da3:7c6b:acca with SMTP id w11-20020a056902100b00b00da37c6baccamr7715594ybt.28.1700482186518; Mon, 20 Nov 2023 04:09:46 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Mon, 20 Nov 2023 12:09:35 +0000 Message-ID: Subject: Re: libstdc++: Speed up push_back To: Jan Hubicka Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,RCVD_IN_SORBS_WEB,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Sun, 19 Nov 2023 at 21:53, Jan Hubicka wrote: > > Hi, > this patch speeds up the push_back at -O3 significantly by making the > reallocation to be inlined by default. _M_realloc_insert is general > insertion that takes iterator pointing to location where the value > should be inserted. As such it contains code to move other entries around > that is quite large. > > Since appending to the end of array is common operation, I think we should > have specialized code for that. Sadly it is really hard to work out this > from IPA passes, since we basically care whether the iterator points to > the same place as the end pointer, which are both passed by reference. > This is inter-procedural value numbering that is quite out of reach. > > I also added extra check making it clear that the new length of the vector > is non-zero. This saves extra conditionals. Again it is quite hard case > since _M_check_len seem to be able to return 0 if its parameter is 0. > This never happens here, but we are not able to propagate this early nor > at IPA stage. > > Would it be OK to duplciate code as this? The resulting code is still not quite > optimal. > > Regtested on x86_64-linux, OK? > > Honza > > void std::vector::_M_realloc_append (struct vector * const this, const struct pair_t & __args#0) > { > struct _Guard __guard; > struct pair_t * __new_finish; > struct pair_t * __old_finish; > struct pair_t * __old_start; > struct _Vector_impl * _1; > long unsigned int _2; > struct pair_t * _3; > struct pair_t * _4; > long int _5; > long int _6; > long unsigned int _7; > long unsigned int _8; > struct pair_t * _9; > const size_type _13; > struct pair_t * _16; > struct _Vector_impl * _18; > long int _27; > long unsigned int _34; > > [local count: 1073741824]: > _13 = std::vector::_M_check_len (this_11(D), 1, "vector::_M_realloc_append"); > if (_13 == 0) > goto ; [0.00%] > else > goto ; [100.00%] > > [count: 0]: > __builtin_unreachable (); > > [local count: 1073741824]: > __old_start_14 = this_11(D)->D.26060._M_impl.D.25361._M_start; > __old_finish_15 = this_11(D)->D.26060._M_impl.D.25361._M_finish; > _27 = __old_finish_15 - __old_start_14; > _18 = &MEM[(struct _Vector_base *)this_11(D)]._M_impl; > _16 = std::__new_allocator::allocate (_18, _13, 0B); > _1 = &this_11(D)->D.26060._M_impl; > __guard ={v} {CLOBBER}; > __guard._M_alloc = _1; > _2 = (long unsigned int) _27; > _3 = _16 + _2; > *_3 = *__args#0_17(D); > if (_27 > 0) > goto ; [41.48%] > else > goto ; [58.52%] > > [local count: 445388112]: > __builtin_memmove (_16, __old_start_14, _2); > > [local count: 1073741824]: > _34 = _2 + 8; > __new_finish_19 = _16 + _34; > __guard._M_storage = __old_start_14; > _4 = this_11(D)->D.26060._M_impl.D.25361._M_end_of_storage; > _5 = _4 - __old_start_14; > _6 = _5 /[ex] 8; > _7 = (long unsigned int) _6; > __guard._M_len = _7; > std::vector::_M_realloc_append(const pair_t&)::_Guard::~_Guard (&__guard); > __guard ={v} {CLOBBER(eol)}; > this_11(D)->D.26060._M_impl.D.25361._M_start = _16; > this_11(D)->D.26060._M_impl.D.25361._M_finish = __new_finish_19; > _8 = _13 * 8; > _9 = _16 + _8; > this_11(D)->D.26060._M_impl.D.25361._M_end_of_storage = _9; > return; > > } > > Notice that memmove can be memcopy and the test whether block size is non-zero is useless. > > > libstdc++-v3/ChangeLog: > > PR libstdc++/110287 > * include/bits/stl_vector.h (_M_realloc_append): New member function. > (push_back): Use it. > * include/bits/vector.tcc: (emplace_back): Use it. > (_M_realloc_insert): Let compiler know that new vector size is non-zero. > (_M_realloc_append): New member function. > > diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h > index 5e18f6eedce..973f4d7e2e9 100644 > --- a/libstdc++-v3/include/bits/stl_vector.h > +++ b/libstdc++-v3/include/bits/stl_vector.h > @@ -1288,7 +1288,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > _GLIBCXX_ASAN_ANNOTATE_GREW(1); > } > else > - _M_realloc_insert(end(), __x); > + _M_realloc_append(__x); > } > > #if __cplusplus >= 201103L > @@ -1822,6 +1822,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > > void > _M_realloc_insert(iterator __position, const value_type& __x); > + > + void > + _M_realloc_append(const value_type& __x); > #else > // A value_type object constructed with _Alloc_traits::construct() > // and destroyed with _Alloc_traits::destroy(). > @@ -1871,6 +1874,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > void > _M_realloc_insert(iterator __position, _Args&&... __args); > > + template > + _GLIBCXX20_CONSTEXPR > + void > + _M_realloc_append(_Args&&... __args); > + > // Either move-construct at the end, or forward to _M_insert_aux. > _GLIBCXX20_CONSTEXPR > iterator > diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc > index 80631d1e2a1..1306676e795 100644 > --- a/libstdc++-v3/include/bits/vector.tcc > +++ b/libstdc++-v3/include/bits/vector.tcc > @@ -120,7 +120,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > _GLIBCXX_ASAN_ANNOTATE_GREW(1); > } > else > - _M_realloc_insert(end(), std::forward<_Args>(__args)...); > + _M_realloc_append(std::forward<_Args>(__args)...); > #if __cplusplus > 201402L > return back(); > #endif > @@ -459,6 +459,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > #endif > { > const size_type __len = _M_check_len(1u, "vector::_M_realloc_insert"); > + if (__len <= 0) > + __builtin_unreachable (); > pointer __old_start = this->_M_impl._M_start; > pointer __old_finish = this->_M_impl._M_finish; > const size_type __elems_before = __position - begin(); > @@ -571,6 +573,129 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > this->_M_impl._M_end_of_storage = __new_start + __len; > } > > +#if __cplusplus >= 201103L > + template > + template > + _GLIBCXX20_CONSTEXPR > + void > + vector<_Tp, _Alloc>:: > + _M_realloc_append(_Args&&... __args) > +#else > + template > + void > + vector<_Tp, _Alloc>:: > + _M_realloc_append(const _Tp& __x) > +#endif > + { > + const size_type __len = _M_check_len(1u, "vector::_M_realloc_append"); > + if (__len <= 0) > + __builtin_unreachable (); > + pointer __old_start = this->_M_impl._M_start; > + pointer __old_finish = this->_M_impl._M_finish; > + const size_type __elems = end() - begin(); > + pointer __new_start(this->_M_allocate(__len)); > + pointer __new_finish(__new_start); > + > + // RAII guard for allocated storage. > + struct _Guard > + { > + pointer _M_storage; // Storage to deallocate > + size_type _M_len; > + _Tp_alloc_type& _M_alloc; > + > + _GLIBCXX20_CONSTEXPR > + _Guard(pointer __s, size_type __l, _Tp_alloc_type& __a) > + : _M_storage(__s), _M_len(__l), _M_alloc(__a) > + { } > + > + _GLIBCXX20_CONSTEXPR > + ~_Guard() > + { > + if (_M_storage) > + __gnu_cxx::__alloc_traits<_Tp_alloc_type>:: > + deallocate(_M_alloc, _M_storage, _M_len); > + } > + > + private: > + _Guard(const _Guard&); > + }; > + > + { > + _Guard __guard(__new_start, __len, _M_impl); > + > + // The order of the three operations is dictated by the C++11 > + // case, where the moves could alter a new element belonging > + // to the existing vector. This is an issue only for callers > + // taking the element by lvalue ref (see last bullet of C++11 > + // [res.on.arguments]). > + > + // If this throws, the existing elements are unchanged. > +#if __cplusplus >= 201103L > + _Alloc_traits::construct(this->_M_impl, > + std::__to_address(__new_start + __elems), > + std::forward<_Args>(__args)...); > +#else > + _Alloc_traits::construct(this->_M_impl, > + __new_start + __elems, > + __x); > +#endif > + > +#if __cplusplus >= 201103L > + if _GLIBCXX17_CONSTEXPR (_S_use_relocate()) > + { > + // Relocation cannot throw. > + __new_finish = _S_relocate(__old_start, __old_finish, > + __new_start, _M_get_Tp_allocator()); > + ++__new_finish; > + } > + else > +#endif > + { > + // RAII type to destroy initialized elements. There's only one initialized element, not "elements". > + struct _Guard_elts > + { > + pointer _M_first, _M_last; // Elements to destroy We only need to store one pointer here, call it _M_p. > + _Tp_alloc_type& _M_alloc; > + > + _GLIBCXX20_CONSTEXPR > + _Guard_elts(pointer __elt, _Tp_alloc_type& __a) > + : _M_first(__elt), _M_last(__elt + 1), _M_alloc(__a) > + { } > + > + _GLIBCXX20_CONSTEXPR > + ~_Guard_elts() > + { std::_Destroy(_M_first, _M_last, _M_alloc); } This should be either: std::_Destroy(_M_p, _M_p+1, _M_alloc); or avoid the loop that happens in that _Destroy function: _Alloc_traits::destroy(_M_alloc, _M_p); > + > + private: > + _Guard_elts(const _Guard_elts&); > + }; > + > + // Guard the new element so it will be destroyed if anything throws. > + _Guard_elts __guard_elts(__new_start + __elems, _M_impl); > + > + __new_finish = std::__uninitialized_move_if_noexcept_a( > + __old_start, __old_finish, > + __new_start, _M_get_Tp_allocator()); > + > + ++__new_finish; > + // Guard everything before the new element too. > + __guard_elts._M_first = __new_start; This seems redundant, we're not doing any more insertions now, and so this store is dead. > + > + // New storage has been fully initialized, destroy the old elements. > + __guard_elts._M_first = __old_start; > + __guard_elts._M_last = __old_finish; > + } > + __guard._M_storage = __old_start; > + __guard._M_len = this->_M_impl._M_end_of_storage - __old_start; > + } > + // deallocate should be called before assignments to _M_impl, > + // to avoid call-clobbering > + > + this->_M_impl._M_start = __new_start; > + this->_M_impl._M_finish = __new_finish; > + this->_M_impl._M_end_of_storage = __new_start + __len; > + } > + > template > _GLIBCXX20_CONSTEXPR > void >