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 X-Patchwork-Id: 1953160 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=hAdMIQSD; 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 4W8wQR6wKQz20Xg for ; Thu, 27 Jun 2024 20:53:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 181C73834696 for ; Thu, 27 Jun 2024 10:53:02 +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 CFC713836E86 for ; Thu, 27 Jun 2024 10:52:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CFC713836E86 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 CFC713836E86 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=1719485546; cv=none; b=ssTUpN1SkIaBdk8d/PcN5fRNtiRki14rU3NTz+lmviUckEx5kUygeSvm+1YtDS+FS1FaTEqYD33i8PNxuoU58fGhQjXpXRWjjCdxZ6ZsIrnzFfspfq3sFT9SEDU38dig9+NX3JkL668B/Jzg1lAuzHWnEpCxop/sUd1m9HRStxE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719485546; c=relaxed/simple; bh=oicTPzhOTdp8+sHxNP6rcddNAqS+X47Mx/GTmgvdp40=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=cWgRkLw3DeHS0aTz6PoWJ8MhQ3mP5EgIcNH2dRfeZBjtlBfdAfGDaGw3YHYP+kyTBvyYIL3SuCBX7nPX9pp4fQcjZq1mcFIMFC5GNW88OONBKXLsX5hqivu4XN03YjoRLrkzYFz6g6fVK1KUjiIgDZzJC5cp/R9nmRyEJO4SaJ8= 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: in-reply-to:in-reply-to:references:references; bh=FvCGq2H1gJ8nQvUmFOQaQBoPfJ5vy7tfgWZ4l8Jez3Y=; b=hAdMIQSDd7KHtoKwKJInStR11n2fGup7nNGdgLAJhLemUkmr+lzBkmbIneY1HD98DoG/XQ iVmJgBotzwR0jm3Y3sqSqKpKxrNfjLrxwgvWhDI4vw/Wh5uM6XeolBoJ6AaMRokQ9S09+S bU9Hp1XchMxJUuPasvCJ0yKcwN7FDWE= Received: from mx-prod-mc-01.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-534-8PxoNdP8P4StpiqKgzN7Kg-1; Thu, 27 Jun 2024 06:52:22 -0400 X-MC-Unique: 8PxoNdP8P4StpiqKgzN7Kg-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A6BC519560B5; Thu, 27 Jun 2024 10:52:21 +0000 (UTC) Received: from localhost (unknown [10.42.28.171]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 19BD3300021A; Thu, 27 Jun 2024 10:52:20 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 2/3] libstdc++: Optimize __uninitialized_default using memset Date: Thu, 27 Jun 2024 11:39:53 +0100 Message-ID: <20240627105217.116315-2-jwakely@redhat.com> In-Reply-To: <20240627105217.116315-1-jwakely@redhat.com> References: <20240627105217.116315-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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=ham 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 For trivial types std::__uninitialized_default (which is used by std::uninitialized_value_construct) value-initializes the first element then copies that to the rest of the range using std::fill. Tamar is working on improved vectorization for std::fill, but for this value-initialized case where we just want to fill with zeros it seems sensible to just ... fill with zeros. We can use memset to do that. Tested x86_64-linux. -- >8 -- The current optimized path for __uninitialized_default and __uninitialized_default_n will use std::fill for trivial types, but we can just use memset to fill them with zeros instead. Because these functions are not defined for C++98 at all, we can use if-constexpr to simplify them and remove the dispatching to members of class template specializations. libstdc++-v3/ChangeLog: * include/bits/stl_uninitialized.h (__uninitialized_default_1) (__uninitialized_default_n_1): Remove. (__uninitialized_default, __uninitialized_default_n): Use memset for contiguous ranges of trivial types. * testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc: Check negative size. --- libstdc++-v3/include/bits/stl_uninitialized.h | 159 ++++++++---------- .../uninitialized_value_construct_n/sizes.cc | 13 ++ 2 files changed, 87 insertions(+), 85 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_uninitialized.h b/libstdc++-v3/include/bits/stl_uninitialized.h index a9965f26269..1216b319f66 100644 --- a/libstdc++-v3/include/bits/stl_uninitialized.h +++ b/libstdc++-v3/include/bits/stl_uninitialized.h @@ -61,6 +61,7 @@ #endif #include // copy +#include // __to_address #include // __alloc_traits #if __cplusplus >= 201703L @@ -590,89 +591,72 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Extensions: __uninitialized_default, __uninitialized_default_n, // __uninitialized_default_a, __uninitialized_default_n_a. - template - struct __uninitialized_default_1 - { - template - static void - __uninit_default(_ForwardIterator __first, _ForwardIterator __last) - { - _UninitDestroyGuard<_ForwardIterator> __guard(__first); - for (; __first != __last; ++__first) - std::_Construct(std::__addressof(*__first)); - __guard.release(); - } - }; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wc++17-extensions" - template<> - struct __uninitialized_default_1 + // If we can value-initialize *__first using memset then return + // std::to_address(__first), otherwise return nullptr. + template + _GLIBCXX20_CONSTEXPR + inline void* + __ptr_for_trivial_zero_init(_ForwardIterator __first) { - template - static void - __uninit_default(_ForwardIterator __first, _ForwardIterator __last) - { - if (__first == __last) - return; +#ifdef __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + return nullptr; // Cannot memset during constant evaluation. +#endif - typename iterator_traits<_ForwardIterator>::value_type* __val - = std::__addressof(*__first); - std::_Construct(__val); - if (++__first != __last) - std::fill(__first, __last, *__val); - } - }; - - template - struct __uninitialized_default_n_1 - { - template - _GLIBCXX20_CONSTEXPR - static _ForwardIterator - __uninit_default_n(_ForwardIterator __first, _Size __n) - { - _UninitDestroyGuard<_ForwardIterator> __guard(__first); - for (; __n > 0; --__n, (void) ++__first) - std::_Construct(std::__addressof(*__first)); - __guard.release(); - return __first; - } - }; - - template<> - struct __uninitialized_default_n_1 - { - template - _GLIBCXX20_CONSTEXPR - static _ForwardIterator - __uninit_default_n(_ForwardIterator __first, _Size __n) - { - if (__n > 0) +#if __cpp_lib_concepts + if constexpr (!contiguous_iterator<_ForwardIterator>) + return nullptr; // Need a raw pointer for memset. +#else + if constexpr (!is_pointer<_ForwardIterator>::value) + return nullptr; +#endif + else + { + using _ValueType + = typename iterator_traits<_ForwardIterator>::value_type; + // Need value-init to be equivalent to zero-init. + using __value_init_is_zero_init + = __and_, + is_trivially_constructible<_ValueType>>; + if constexpr (__value_init_is_zero_init::value) { - typename iterator_traits<_ForwardIterator>::value_type* __val - = std::__addressof(*__first); - std::_Construct(__val); - ++__first; - __first = std::fill_n(__first, __n - 1, *__val); + using _Ptr = decltype(std::__to_address(__first)); + // Cannot use memset if _Ptr is cv-qualified. + if constexpr (is_convertible<_Ptr, void*>::value) + return std::__to_address(__first); } - return __first; } - }; + return nullptr; + } // __uninitialized_default // Fills [first, last) with value-initialized value_types. template inline void - __uninitialized_default(_ForwardIterator __first, - _ForwardIterator __last) + __uninitialized_default(_ForwardIterator __first, _ForwardIterator __last) { - typedef typename iterator_traits<_ForwardIterator>::value_type - _ValueType; - // trivial types can have deleted assignment - const bool __assignable = is_copy_assignable<_ValueType>::value; + if constexpr (__is_random_access_iter<_ForwardIterator>::__value) + if (void* __ptr = std::__ptr_for_trivial_zero_init(__first)) + { + using _ValueType + = typename iterator_traits<_ForwardIterator>::value_type; + if (auto __dist = __last - __first) + { + __glibcxx_assert(__dist > 0); + const size_t __n = __dist; + __glibcxx_assert(__n < __SIZE_MAX__ / sizeof(_ValueType)); + __builtin_memset(__ptr, 0, __n * sizeof(_ValueType)); + } + return; + } - std::__uninitialized_default_1<__is_trivial(_ValueType) - && __assignable>:: - __uninit_default(__first, __last); + _UninitDestroyGuard<_ForwardIterator> __guard(__first); + for (; __first != __last; ++__first) + std::_Construct(std::__addressof(*__first)); + __guard.release(); } // __uninitialized_default_n @@ -682,23 +666,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline _ForwardIterator __uninitialized_default_n(_ForwardIterator __first, _Size __n) { -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) - return __uninitialized_default_n_1:: - __uninit_default_n(__first, __n); -#endif + if constexpr (is_integral<_Size>::value) + if constexpr (__is_random_access_iter<_ForwardIterator>::__value) + if (void* __ptr = std::__ptr_for_trivial_zero_init(__first)) + { + using _ValueType + = typename iterator_traits<_ForwardIterator>::value_type; + if (__n <= 0) + return __first; + else if (size_t(__n) < __SIZE_MAX__ / sizeof(_ValueType)) + { + __builtin_memset(__ptr, 0, __n * sizeof(_ValueType)); + return __first + __n; + } + } - typedef typename iterator_traits<_ForwardIterator>::value_type - _ValueType; - // See uninitialized_fill_n for the conditions for using std::fill_n. - constexpr bool __can_fill - = __and_, is_copy_assignable<_ValueType>>::value; - - return __uninitialized_default_n_1<__is_trivial(_ValueType) - && __can_fill>:: - __uninit_default_n(__first, __n); + _UninitDestroyGuard<_ForwardIterator> __guard(__first); + for (; __n > 0; --__n, (void) ++__first) + std::_Construct(std::__addressof(*__first)); + __guard.release(); + return __first; } - +#pragma GCC diagnostic pop // __uninitialized_default_a // Fills [first, last) with value_types constructed by the allocator diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc index 7705c6813e3..9c4198c1a98 100644 --- a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc @@ -52,9 +52,22 @@ test02() VERIFY( i[4] == 5 ); } +void +test03() +{ + int i[3] = { 1, 2, 3 }; + // The standard defines it in terms of a loop which only runs for positive n. + auto j = std::uninitialized_value_construct_n(i+1, -5); + VERIFY( j == i+1 ); + VERIFY( i[0] == 1 ); + VERIFY( i[1] == 2 ); + VERIFY( i[2] == 3 ); +} + int main() { test01(); test02(); + test03(); } From patchwork Thu Jun 27 10:39:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1953161 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=Ed3TJjyA; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4W8wRy5PBqz20Xg for ; Thu, 27 Jun 2024 20:54:22 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5CADE3834E58 for ; Thu, 27 Jun 2024 10:54:18 +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 2CFAD38346A7 for ; Thu, 27 Jun 2024 10:52:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2CFAD38346A7 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 2CFAD38346A7 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=1719485553; cv=none; b=Aygkt6ZNWWqHGCm8XMtHXjwzeB+JCSjNXJ4CKlZ1byJ48gP4wpnelfX01RIktg+/bqt0SqU4pbIgttYaX2fUtjRonh01YyZvnFxdFbhK0nS8cHBXPRre9g3SvRcrkU6EbKu9oDl79LBV//JQCgYr1XJYSgtXsrcH8azsCaWAb3k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719485553; c=relaxed/simple; bh=cK+vuzyMLFF8XMVan9J0NRPlzQfdG4y1YnKZteNyiqo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ghc4nF7LbvyTplhJQ4uzBsL1wTqn/yjLApldnUxRjDzppCY5g4Jr9Hcm+UPiHn/y1NEV040UDn409/0sLPxPmQtR5lErCpYCec1u4yHSGMtfCRSoLH7p70wF5KUbg7neLoiVSCY+0KgRc0vZj4P7W9Aouj0w1KqgVxgZcTZbr14= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719485548; 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: in-reply-to:in-reply-to:references:references; bh=TZ0AbN4EYTRrbH/yc3qMLORLKfCbHrFBkzDajBkVsjY=; b=Ed3TJjyA42WxYfItXq5y13Wl6ROkU1bF6PIiCDziYLV7jud4f5o+nszb7pT9IlHy53GvDM cu1CGHp7A6/fZ/Koi4NB9GZl3YnhBGig453OwQ/NL6Od7r9r01ANgQmlm+WCEjlLaLEkJH om4Bp96vyR0Y79MEp6ldmdhjzHWGGOw= Received: from mx-prod-mc-04.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-574-QJc9NeIqMniWO7UB6FQ1-A-1; Thu, 27 Jun 2024 06:52:25 -0400 X-MC-Unique: QJc9NeIqMniWO7UB6FQ1-A-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BCF6A19560AB; Thu, 27 Jun 2024 10:52:24 +0000 (UTC) Received: from localhost (unknown [10.42.28.171]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1AC9C1955D89; Thu, 27 Jun 2024 10:52:23 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 3/3] libstdc++: Use std::__uninitialized_default for ranges::uninitialized_value_construct Date: Thu, 27 Jun 2024 11:39:54 +0100 Message-ID: <20240627105217.116315-3-jwakely@redhat.com> In-Reply-To: <20240627105217.116315-1-jwakely@redhat.com> References: <20240627105217.116315-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 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 By generalizing std::__uninitialized_default to work with non-common ranges (i.e. iterator/sentinel pair) we can reuse it for the ranges::uninitialized_value_construct function. Doing that ensures that whatever optimizations we have for std::uninitialized_value_construct are automatically used for the ranges version too. Tested x86_64-linux. -- >8 -- This reuses the memset optimization in __uninitialized_default for the ranges equivalent, and similarly for uninitialized_value_construct_n. libstdc++-v3/ChangeLog: * include/bits/ranges_uninitialized.h (uninitialized_value_construct): Use __uninitialized_default. (uninitialized_value_construct_n): Use __uninitialized_default_n. * include/bits/stl_uninitialized.h (__uninitialized_default): Allow first and last to be different types, to support arbitrary sentinels. Return the end of the initialized range. (uninitialized_value_construct): Discard return value from __uninitialized_default. --- .../include/bits/ranges_uninitialized.h | 27 +++---------------- libstdc++-v3/include/bits/stl_uninitialized.h | 13 +++++---- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_uninitialized.h b/libstdc++-v3/include/bits/ranges_uninitialized.h index f16f2ef39f5..d84c8502eb9 100644 --- a/libstdc++-v3/include/bits/ranges_uninitialized.h +++ b/libstdc++-v3/include/bits/ranges_uninitialized.h @@ -201,18 +201,8 @@ namespace ranges _Iter operator()(_Iter __first, _Sent __last) const { - using _ValueType = remove_reference_t>; - if constexpr (is_trivial_v<_ValueType> - && is_copy_assignable_v<_ValueType>) - return ranges::fill(__first, __last, _ValueType()); - else - { - auto __guard = __detail::_DestroyGuard(__first); - for (; __first != __last; ++__first) - ::new (__detail::__voidify(*__first)) _ValueType(); - __guard.release(); - return __first; - } + return std::__uninitialized_default(std::move(__first), + std::move(__last)); } template<__detail::__nothrow_forward_range _Range> @@ -234,18 +224,7 @@ namespace ranges _Iter operator()(_Iter __first, iter_difference_t<_Iter> __n) const { - using _ValueType = remove_reference_t>; - if constexpr (is_trivial_v<_ValueType> - && is_copy_assignable_v<_ValueType>) - return ranges::fill_n(__first, __n, _ValueType()); - else - { - auto __guard = __detail::_DestroyGuard(__first); - for (; __n > 0; ++__first, (void) --__n) - ::new (__detail::__voidify(*__first)) _ValueType(); - __guard.release(); - return __first; - } + return std::__uninitialized_default_n(std::move(__first), __n); } }; diff --git a/libstdc++-v3/include/bits/stl_uninitialized.h b/libstdc++-v3/include/bits/stl_uninitialized.h index 1216b319f66..b13562992de 100644 --- a/libstdc++-v3/include/bits/stl_uninitialized.h +++ b/libstdc++-v3/include/bits/stl_uninitialized.h @@ -634,9 +634,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // __uninitialized_default // Fills [first, last) with value-initialized value_types. - template - inline void - __uninitialized_default(_ForwardIterator __first, _ForwardIterator __last) + template + _GLIBCXX20_CONSTEXPR + inline _ForwardIterator + __uninitialized_default(_ForwardIterator __first, _Sentinel __last) { if constexpr (__is_random_access_iter<_ForwardIterator>::__value) if (void* __ptr = std::__ptr_for_trivial_zero_init(__first)) @@ -649,14 +650,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const size_t __n = __dist; __glibcxx_assert(__n < __SIZE_MAX__ / sizeof(_ValueType)); __builtin_memset(__ptr, 0, __n * sizeof(_ValueType)); + return __first + __dist; } - return; + return __first; } _UninitDestroyGuard<_ForwardIterator> __guard(__first); for (; __first != __last; ++__first) std::_Construct(std::__addressof(*__first)); __guard.release(); + return __first; } // __uninitialized_default_n @@ -939,7 +942,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) { - return std::__uninitialized_default(__first, __last); + std::__uninitialized_default(__first, __last); } /**