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 CC08B3858C20 for ; Tue, 21 Nov 2023 13:08:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CC08B3858C20 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 CC08B3858C20 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=1700572087; cv=none; b=XmQ5KoVrRhT1CnPcVEHoNV6prtJyD80rm+Q53z/Y2MegPKHr+cG8h9xl2Bl882hBKn5g5ZArUTNaWtLFWHI78rgXL330OWshbiYR0jWd+PnXtGs8Z9jGBWqgatFdlCdPlpUaPhyLi/iVfGol+RqcbAJVNNjHRxvJWxWa2rcMD9I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700572087; c=relaxed/simple; bh=yrAgDrcPVtY9C+dYCb5ynE+ux0Sx1KKVQ4ttDw48j8M=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=PI/potjrWWjYzzcPDkOlIO5PiG5OjwQVVBtiVYFSpTOYVDmnjCWypG/AbuGz2J1svcQvbTvbujSIr5CleqQvtkRQ96VV7k1+p491diTv3pZUAC7EE0pfbNbzAWZt7SgtBXT4HVHsWNI5ijiFJiku0wtmsqdZvOq4uKCailh9T1M= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700572085; 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=awuQ31vJeanEpjA/aZG0FXwHsCY31EbEwIIp6x8MIg8=; b=BuDFe7Y077sePJHhe3zhEMQ5+E4kZRcwkjy99PS+k3w9cofSNU0v6ZdGpVOA1ZSuiUQcif QeCYD/uPFX6r//0wpgBbu3xTLyle1Exeu7ft7kC98iWpHs4X24Umd28/cRvNuUwgYwZ9Ot VG+VHhTlIBJN8+LAOcSaMp2zDj/JYo0= 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-465-aT5L5Qw-Pn6lvr0wvOII-w-1; Tue, 21 Nov 2023 08:08:03 -0500 X-MC-Unique: aT5L5Qw-Pn6lvr0wvOII-w-1 Received: by mail-yb1-f198.google.com with SMTP id 3f1490d57ef6-da0ccfc4fc8so7431330276.2 for ; Tue, 21 Nov 2023 05:08:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700572083; x=1701176883; 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=awuQ31vJeanEpjA/aZG0FXwHsCY31EbEwIIp6x8MIg8=; b=qhfYm8aIgQUM7QURr4pIIt61GU/yKHrn+RhKJAmpwW6ikgwaBWaQsivNAYTsLkPTvG d8YbVOxH5h4g6NEvbCXcOEUiPlI7gyRuV9aq1YI3+4EFUPfsYcRbdilJlx53pVtTS/5A XG5HPekUWY1nBvk1vdSXsvcfhrA1mST6Z6U+YTUDfYBNB5RyYexQ2FF8KkuN6IE8/4pM aOnzRN6xeRnUQd4s6ssFfrxH4wyrz28tKJGoZS6Ko6JoCuGpFfUZGSJNumFRPg4JndKN QNXj9xT9qZPtm7Vrntek3A8XMofOu0U2X9rgo7EY8BHni40umLnt29FwRBC9eMq0j8Hn rmXQ== X-Gm-Message-State: AOJu0YwG01pHxWlU3m04N55UIGSUpQ+vkU3yJ9kWtP87SexWu3rAK0Xc SJhRNWDqya/YCLQCf8e5OD0S45lo72/HZ3cfAFnmagtbqFm/OlExfNbvS0skaZmoKC8DzCe8fao z/JuGjy40YP8L1WRIHfOuEI4aHFwKe7E= X-Received: by 2002:a25:c74b:0:b0:daf:5829:5b66 with SMTP id w72-20020a25c74b000000b00daf58295b66mr9924027ybe.14.1700572083303; Tue, 21 Nov 2023 05:08:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IHzQnJUKYfA+kQOCiB9ac/vaLhg9eqf9nhIx94Mb/YYqj2DNbJl2ic8UwUsZDcP9PmUz/8zJFzm/NgqaWZanIU= X-Received: by 2002:a25:c74b:0:b0:daf:5829:5b66 with SMTP id w72-20020a25c74b000000b00daf58295b66mr9924006ybe.14.1700572082965; Tue, 21 Nov 2023 05:08:02 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Tue, 21 Nov 2023 13:07:52 +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=-12.2 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=unavailable 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 Tue, 21 Nov 2023 at 12:50, Jan Hubicka wrote: > > > > + // 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. > > I am attaching patch with this check removed. However I still think > Guard_elts needs to stay to be able to destroy the old ellements > > > > > + > > > + // New storage has been fully initialized, destroy the old elements. > > > + __guard_elts._M_first = __old_start; > > > + __guard_elts._M_last = __old_finish; > ... here > > Does it look better? (I am not really that confidend with libstdc++). Yes, looks good, thanks. > > I also was wondering if we do have some data on what libstdc++ functions > are perofrmance critical in practice. Given that the push_back can be > sped up very noticeably, I wonder if we don't have problems elsewhere? We don't have that data, no. It's possible that we could do similar things for insert(iterator pos, ...) for the case where pos==end(), i.e. inserting multiple elements at the end. > > Thank you, > Honza > > 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..0ccef7911b3 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,127 @@ _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. > + struct _Guard_elts > + { > + pointer _M_first, _M_last; // Elements to destroy > + _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); } > + > + 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; > + > + // 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 >