From patchwork Thu Jun 27 10:39:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1953159 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=fxJeiiKR; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W8wQK5SYxz20Xg for ; Thu, 27 Jun 2024 20:52:57 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 04182383541B for ; Thu, 27 Jun 2024 10:52:56 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id EED45383600F for ; Thu, 27 Jun 2024 10:52:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EED45383600F 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 EED45383600F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719485547; cv=none; b=hkTQmTcuTsnS1Oqs+tfBtlaQ4JXLww6VoD2YVQrxc+fvSu6L4thEvLzdnBKixUM2arbOraRteyU5KJ8o/qdRIXDUEa/MV3XLqfv+3/0/B/Yob2HbKhndZBHNX0IuXE9FvnGJFQbk9tJS0L+Xz5a8VvipNkYr66DPLqzFB2eOE34= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719485547; c=relaxed/simple; bh=njv54eYEYrDF7w/U1ohknHsvxauW0+s168P8+PCzEnU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Nu34CZK5NCCkF3YTSbvBP7vfSU8bnuHw3GCzA7aA64tAsJLWuUJY/W4NP/qv3RXswEaD96BH/FMJXgZQFd/ig46vvNBswXdzjYS6mXp3jAH6PIGCRAdzLmtzQRVpvZKw3K6VdjTBYqBw9UNrGv3udttI7dGa1XMgkL+K4S9SpsI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719485544; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=fnbvIpZncSj44SkB1YXuJx5OIoCad6iETjSapaO02hw=; b=fxJeiiKRNo08d02j4p6wsQDDfccQlQfhbf2O9c4/ZSEYMO8hBkpHwW7lQCWwpB2p5VrH8M YdBo6Sidks4ydMnkiEDGr1KP68nkm9c4otYfu6kALwxG0jMABb+4UJtkdQ1QDDup0c9Lzf Zg+nUgbyD0TpNp3CO/Um3oQqstq2NOY= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-272-a6LblmjSP0WEATG4h6b4Iw-1; Thu, 27 Jun 2024 06:52:21 -0400 X-MC-Unique: a6LblmjSP0WEATG4h6b4Iw-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9F86A19331B1; Thu, 27 Jun 2024 10:52:19 +0000 (UTC) Received: from localhost (unknown [10.42.28.171]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EA4E019773E1; Thu, 27 Jun 2024 10:52:18 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 1/3] libstdc++: Use RAII in Date: Thu, 27 Jun 2024 11:39:52 +0100 Message-ID: <20240627105217.116315-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org This refactoring to use RAII doesn't seem to make any difference in benchmarks, although the generated code for some std::vector operations seems to be slightly larger. Maybe it will be faster (or slower) in some cases I didn't test? I think I like the change anyway - any other opinions on whether it's an improvement? Tested x86_64-linux. -- >8 -- This adds an _UninitDestroyGuard class template, similar to ranges::_DestroyGuard used in . This allows us to remove all the try-catch blocks and rethrows, because any required cleanup gets done in the guard destructor. libstdc++-v3/ChangeLog: * include/bits/stl_uninitialized.h (_UninitDestroyGuard): New class template and partial specialization. (__do_uninit_copy, __do_uninit_fill, __do_uninit_fill_n) (__uninitialized_copy_a, __uninitialized_fill_a) (__uninitialized_fill_n_a, __uninitialized_copy_move) (__uninitialized_move_copy, __uninitialized_fill_move) (__uninitialized_move_fill, __uninitialized_default_1) (__uninitialized_default_n_a, __uninitialized_default_novalue_1) (__uninitialized_default_novalue_n_1, __uninitialized_copy_n) (__uninitialized_copy_n_pair): Use it. --- libstdc++-v3/include/bits/stl_uninitialized.h | 365 ++++++++---------- 1 file changed, 156 insertions(+), 209 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_uninitialized.h b/libstdc++-v3/include/bits/stl_uninitialized.h index 3c405d8fbe8..a9965f26269 100644 --- a/libstdc++-v3/include/bits/stl_uninitialized.h +++ b/libstdc++-v3/include/bits/stl_uninitialized.h @@ -107,24 +107,70 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __is_trivial(T) && __is_assignable(T&, U) #endif + template + struct _UninitDestroyGuard + { + _GLIBCXX20_CONSTEXPR + explicit + _UninitDestroyGuard(_ForwardIterator& __first, _Alloc& __a) + : _M_first(__first), _M_cur(__builtin_addressof(__first)), _M_alloc(__a) + { } + + _GLIBCXX20_CONSTEXPR + ~_UninitDestroyGuard() + { + if (__builtin_expect(_M_cur != 0, 0)) + std::_Destroy(_M_first, *_M_cur, _M_alloc); + } + + _GLIBCXX20_CONSTEXPR + void release() { _M_cur = 0; } + + private: + _ForwardIterator const _M_first; + _ForwardIterator* _M_cur; + _Alloc& _M_alloc; + + _UninitDestroyGuard(const _UninitDestroyGuard&); + }; + + template + struct _UninitDestroyGuard<_ForwardIterator, void> + { + _GLIBCXX20_CONSTEXPR + explicit + _UninitDestroyGuard(_ForwardIterator& __first) + : _M_first(__first), _M_cur(__builtin_addressof(__first)) + { } + + _GLIBCXX20_CONSTEXPR + ~_UninitDestroyGuard() + { + if (__builtin_expect(_M_cur != 0, 0)) + std::_Destroy(_M_first, *_M_cur); + } + + _GLIBCXX20_CONSTEXPR + void release() { _M_cur = 0; } + + _ForwardIterator const _M_first; + _ForwardIterator* _M_cur; + + private: + _UninitDestroyGuard(const _UninitDestroyGuard&); + }; + template _GLIBCXX20_CONSTEXPR _ForwardIterator __do_uninit_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result) { - _ForwardIterator __cur = __result; - __try - { - for (; __first != __last; ++__first, (void)++__cur) - std::_Construct(std::__addressof(*__cur), *__first); - return __cur; - } - __catch(...) - { - std::_Destroy(__result, __cur); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator> __guard(__result); + for (; __first != __last; ++__first, (void)++__result) + std::_Construct(std::__addressof(*__result), *__first); + __guard.release(); + return __result; } template @@ -192,17 +238,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __do_uninit_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __x) { - _ForwardIterator __cur = __first; - __try - { - for (; __cur != __last; ++__cur) - std::_Construct(std::__addressof(*__cur), __x); - } - __catch(...) - { - std::_Destroy(__first, __cur); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator> __guard(__first); + for (; __first != __last; ++__first) + std::_Construct(std::__addressof(*__first), __x); + __guard.release(); } template @@ -260,18 +299,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _ForwardIterator __do_uninit_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) { - _ForwardIterator __cur = __first; - __try - { - for (; __n > 0; --__n, (void) ++__cur) - std::_Construct(std::__addressof(*__cur), __x); - return __cur; - } - __catch(...) - { - std::_Destroy(__first, __cur); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator> __guard(__first); + for (; __n > 0; --__n, (void) ++__first) + std::_Construct(std::__addressof(*__first), __x); + __guard.release(); + return __first; } template @@ -344,19 +376,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __uninitialized_copy_a(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _Allocator& __alloc) { - _ForwardIterator __cur = __result; - __try - { - typedef __gnu_cxx::__alloc_traits<_Allocator> __traits; - for (; __first != __last; ++__first, (void)++__cur) - __traits::construct(__alloc, std::__addressof(*__cur), *__first); - return __cur; - } - __catch(...) - { - std::_Destroy(__result, __cur, __alloc); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator, _Allocator> + __guard(__result, __alloc); + + typedef __gnu_cxx::__alloc_traits<_Allocator> __traits; + for (; __first != __last; ++__first, (void)++__result) + __traits::construct(__alloc, std::__addressof(*__result), *__first); + __guard.release(); + return __result; } #if _GLIBCXX_HOSTED @@ -406,18 +433,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __x, _Allocator& __alloc) { - _ForwardIterator __cur = __first; - __try - { - typedef __gnu_cxx::__alloc_traits<_Allocator> __traits; - for (; __cur != __last; ++__cur) - __traits::construct(__alloc, std::__addressof(*__cur), __x); - } - __catch(...) - { - std::_Destroy(__first, __cur, __alloc); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator, _Allocator> + __guard(__first, __alloc); + + typedef __gnu_cxx::__alloc_traits<_Allocator> __traits; + for (; __first != __last; ++__first) + __traits::construct(__alloc, std::__addressof(*__first), __x); + + __guard.release(); } #if _GLIBCXX_HOSTED @@ -442,19 +465,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n, const _Tp& __x, _Allocator& __alloc) { - _ForwardIterator __cur = __first; - __try - { - typedef __gnu_cxx::__alloc_traits<_Allocator> __traits; - for (; __n > 0; --__n, (void) ++__cur) - __traits::construct(__alloc, std::__addressof(*__cur), __x); - return __cur; - } - __catch(...) - { - std::_Destroy(__first, __cur, __alloc); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator, _Allocator> + __guard(__first, __alloc); + typedef __gnu_cxx::__alloc_traits<_Allocator> __traits; + for (; __n > 0; --__n, (void) ++__first) + __traits::construct(__alloc, std::__addressof(*__first), __x); + __guard.release(); + return __first; } #if _GLIBCXX_HOSTED @@ -493,17 +510,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Allocator& __alloc) { _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1, - __result, - __alloc); - __try - { - return std::__uninitialized_move_a(__first2, __last2, __mid, __alloc); - } - __catch(...) - { - std::_Destroy(__result, __mid, __alloc); - __throw_exception_again; - } + __result, __alloc); + _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result, + __alloc); + __result = __mid; // Everything up to __mid is now guarded. + __result = std::__uninitialized_move_a(__first2, __last2, __mid, __alloc); + __guard.release(); + return __result; } // __uninitialized_move_copy @@ -521,17 +534,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Allocator& __alloc) { _ForwardIterator __mid = std::__uninitialized_move_a(__first1, __last1, - __result, - __alloc); - __try - { - return std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc); - } - __catch(...) - { - std::_Destroy(__result, __mid, __alloc); - __throw_exception_again; - } + __result, __alloc); + _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result, + __alloc); + __result = __mid; // Everything up to __mid is now guarded. + __result = std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc); + __guard.release(); } // __uninitialized_fill_move @@ -545,15 +553,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _InputIterator __last, _Allocator& __alloc) { std::__uninitialized_fill_a(__result, __mid, __x, __alloc); - __try - { - return std::__uninitialized_move_a(__first, __last, __mid, __alloc); - } - __catch(...) - { - std::_Destroy(__result, __mid, __alloc); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result, + __alloc); + __result = __mid; // Everything up to __mid is now guarded. + __result = std::__uninitialized_move_a(__first, __last, __mid, __alloc); + __guard.release(); + return __result; } // __uninitialized_move_fill @@ -570,15 +575,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _ForwardIterator __mid2 = std::__uninitialized_move_a(__first1, __last1, __first2, __alloc); - __try - { - std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc); - } - __catch(...) - { - std::_Destroy(__first2, __mid2, __alloc); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first2, + __alloc); + __first2 = __mid2; // Everything up to __mid2 is now guarded. + std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc); + __guard.release(); } /// @endcond @@ -596,17 +597,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static void __uninit_default(_ForwardIterator __first, _ForwardIterator __last) { - _ForwardIterator __cur = __first; - __try - { - for (; __cur != __last; ++__cur) - std::_Construct(std::__addressof(*__cur)); - } - __catch(...) - { - std::_Destroy(__first, __cur); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator> __guard(__first); + for (; __first != __last; ++__first) + std::_Construct(std::__addressof(*__first)); + __guard.release(); } }; @@ -636,18 +630,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _ForwardIterator __uninit_default_n(_ForwardIterator __first, _Size __n) { - _ForwardIterator __cur = __first; - __try - { - for (; __n > 0; --__n, (void) ++__cur) - std::_Construct(std::__addressof(*__cur)); - return __cur; - } - __catch(...) - { - std::_Destroy(__first, __cur); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator> __guard(__first); + for (; __n > 0; --__n, (void) ++__first) + std::_Construct(std::__addressof(*__first)); + __guard.release(); + return __first; } }; @@ -722,18 +709,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _ForwardIterator __last, _Allocator& __alloc) { - _ForwardIterator __cur = __first; - __try - { - typedef __gnu_cxx::__alloc_traits<_Allocator> __traits; - for (; __cur != __last; ++__cur) - __traits::construct(__alloc, std::__addressof(*__cur)); - } - __catch(...) - { - std::_Destroy(__first, __cur, __alloc); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first, + __alloc); + typedef __gnu_cxx::__alloc_traits<_Allocator> __traits; + for (; __first != __last; ++__first) + __traits::construct(__alloc, std::__addressof(*__first)); + __guard.release(); } #if _GLIBCXX_HOSTED @@ -753,19 +734,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __uninitialized_default_n_a(_ForwardIterator __first, _Size __n, _Allocator& __alloc) { - _ForwardIterator __cur = __first; - __try - { - typedef __gnu_cxx::__alloc_traits<_Allocator> __traits; - for (; __n > 0; --__n, (void) ++__cur) - __traits::construct(__alloc, std::__addressof(*__cur)); - return __cur; - } - __catch(...) - { - std::_Destroy(__first, __cur, __alloc); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first, + __alloc); + typedef __gnu_cxx::__alloc_traits<_Allocator> __traits; + for (; __n > 0; --__n, (void) ++__first) + __traits::construct(__alloc, std::__addressof(*__first)); + __guard.release(); + return __first; } #if _GLIBCXX_HOSTED @@ -787,17 +762,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __uninit_default_novalue(_ForwardIterator __first, _ForwardIterator __last) { - _ForwardIterator __cur = __first; - __try - { - for (; __cur != __last; ++__cur) - std::_Construct_novalue(std::__addressof(*__cur)); - } - __catch(...) - { - std::_Destroy(__first, __cur); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator> __guard(__first); + for (; __first != __last; ++__first) + std::_Construct_novalue(std::__addressof(*__first)); + __guard.release(); } }; @@ -818,18 +786,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _ForwardIterator __uninit_default_novalue_n(_ForwardIterator __first, _Size __n) { - _ForwardIterator __cur = __first; - __try - { - for (; __n > 0; --__n, (void) ++__cur) - std::_Construct_novalue(std::__addressof(*__cur)); - return __cur; - } - __catch(...) - { - std::_Destroy(__first, __cur); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator> __guard(__first); + for (; __n > 0; --__n, (void) ++__first) + std::_Construct_novalue(std::__addressof(*__first)); + __guard.release(); + return __first; } }; @@ -877,18 +838,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __uninitialized_copy_n(_InputIterator __first, _Size __n, _ForwardIterator __result, input_iterator_tag) { - _ForwardIterator __cur = __result; - __try - { - for (; __n > 0; --__n, (void) ++__first, ++__cur) - std::_Construct(std::__addressof(*__cur), *__first); - return __cur; - } - __catch(...) - { - std::_Destroy(__result, __cur); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator> __guard(__result); + for (; __n > 0; --__n, (void) ++__first, ++__result) + std::_Construct(std::__addressof(*__result), *__first); + __guard.release(); + return __result; } template pair<_InputIterator, _ForwardIterator> __uninitialized_copy_n_pair(_InputIterator __first, _Size __n, - _ForwardIterator __result, input_iterator_tag) + _ForwardIterator __result, input_iterator_tag) { - _ForwardIterator __cur = __result; - __try - { - for (; __n > 0; --__n, (void) ++__first, ++__cur) - std::_Construct(std::__addressof(*__cur), *__first); - return {__first, __cur}; - } - __catch(...) - { - std::_Destroy(__result, __cur); - __throw_exception_again; - } + _UninitDestroyGuard<_ForwardIterator> __guard(__result); + for (; __n > 0; --__n, (void) ++__first, ++__result) + std::_Construct(std::__addressof(*__result), *__first); + __guard.release(); + return {__first, __result}; } template