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); } /**