From patchwork Thu Nov 28 18:48:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 102044 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0121D3858C5F for ; Thu, 28 Nov 2024 18:51:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0121D3858C5F Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=H949t+e+ 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 ESMTP id DB0E63858D29 for ; Thu, 28 Nov 2024 18:49:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB0E63858D29 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 DB0E63858D29 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=1732819748; cv=none; b=WqVYblcvvbX4Ol9uG6KW6X3bXeTHT83WDY24H0VUVggzTgNy/7KzQVQnTR4Nl+941+gIqKfw62jlRAPlOUmtYuNLg4ZbtzPu9fjLKS8hHuEYhGKEBZcL4xSeuYGqbOVEgenmYr4dVVaIcTilgzE+E2Uok6fewB6Nfy+flSnwBgE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732819748; c=relaxed/simple; bh=pgm5Tjmll8ZABvYdcvd3QEmNm58mf04WlWxLMAE2TdY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=L+UvW2/KszQz7LrKJmzL7bszSx6v1oAdTYialloxdr3iRsGqf0FkLdFMic3OHGeAvgN5+u5BpsknqdAjC3HrHp3/pMf9RSr7h55kxiNB/U/l4PirNy3qDWlaOVqpqvpt6J4381/QDvN3UiK8SVV+6CkXN0fo/ZCL51yVJF4gK8s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB0E63858D29 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1732819747; 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=CfXujHGqEU05uuHNL8Giklmnf9/MEBBNxNwPvb3WTzY=; b=H949t+e+/NrjThtHievEWdFJ/iylT67iBrgKrYiyuz2B/jp/kELI+bJNoQM1K9QBO6WqpR VPyEo3Leo5wUss2B8L/yYF9Qe1pdFw0p/B1RA57lf7CUCOZtrRKE7yqaH52LoY+R6Ycx/Z sJckvTvbiCyQ+jJnSFCDt1ihR1l1hF8= 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-112-UEf6ya_9NdqimoL84FDEBg-1; Thu, 28 Nov 2024 13:49:03 -0500 X-MC-Unique: UEf6ya_9NdqimoL84FDEBg-1 X-Mimecast-MFC-AGG-ID: UEf6ya_9NdqimoL84FDEBg Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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 B0BD01955DC2; Thu, 28 Nov 2024 18:49:01 +0000 (UTC) Received: from localhost (unknown [10.42.28.168]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 10F07195605A; Thu, 28 Nov 2024 18:49:00 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Simplify std::_Destroy using 'if constexpr' Date: Thu, 28 Nov 2024 18:48:05 +0000 Message-ID: <20241128184859.934977-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 75sndiS_58EoGDMoRtAmrgY9IQ_DiK5OsieQQvkq1hI_1732819742 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 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_H3, 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~patchwork=sourceware.org@gcc.gnu.org This is another place where we can use 'if constexpr' to replace dispatching to a specialized class template, improving compile times and avoiding a function call. libstdc++-v3/ChangeLog: * include/bits/stl_construct.h (_Destroy(FwdIter, FwdIter)): Use 'if constexpr' instead of dispatching to a member function of a class template. (_Destroy_n(FwdIter, Size)): Likewise. (_Destroy_aux, _Destroy_n_aux): Only define for C++98. --- This seems worthwhile, as another small reduction in compile times, similar to a number of recent patches. Tested x86_64-linux. libstdc++-v3/include/bits/stl_construct.h | 33 ++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_construct.h b/libstdc++-v3/include/bits/stl_construct.h index 9d6111396e1..6889a9bfa0e 100644 --- a/libstdc++-v3/include/bits/stl_construct.h +++ b/libstdc++-v3/include/bits/stl_construct.h @@ -166,6 +166,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif } +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wc++17-extensions" // for if-constexpr + +#if __cplusplus < 201103L template struct _Destroy_aux { @@ -185,6 +189,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static void __destroy(_ForwardIterator, _ForwardIterator) { } }; +#endif /** * Destroy a range of objects. If the value_type of the object has @@ -201,15 +206,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // A deleted destructor is trivial, this ensures we reject such types: static_assert(is_destructible<_Value_type>::value, "value type is destructible"); -#endif + if constexpr (!__has_trivial_destructor(_Value_type)) + for (; __first != __last; ++__first) + std::_Destroy(std::__addressof(*__first)); #if __cpp_constexpr_dynamic_alloc // >= C++20 - if (std::__is_constant_evaluated()) - return std::_Destroy_aux::__destroy(__first, __last); + else if (std::__is_constant_evaluated()) + for (; __first != __last; ++__first) + std::destroy_at(std::__addressof(*__first)); #endif +#else std::_Destroy_aux<__has_trivial_destructor(_Value_type)>:: __destroy(__first, __last); +#endif } +#if __cplusplus < 201103L template struct _Destroy_n_aux { @@ -234,6 +245,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __first; } }; +#endif /** * Destroy a range of objects. If the value_type of the object has @@ -250,14 +262,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // A deleted destructor is trivial, this ensures we reject such types: static_assert(is_destructible<_Value_type>::value, "value type is destructible"); -#endif + if constexpr (!__has_trivial_destructor(_Value_type)) + for (; __count > 0; (void)++__first, --__count) + std::_Destroy(std::__addressof(*__first)); #if __cpp_constexpr_dynamic_alloc // >= C++20 - if (std::__is_constant_evaluated()) - return std::_Destroy_n_aux::__destroy_n(__first, __count); + else if (std::__is_constant_evaluated()) + for (; __count > 0; (void)++__first, --__count) + std::destroy_at(std::__addressof(*__first)); #endif + else + std::advance(__first, __count); + return __first; +#else return std::_Destroy_n_aux<__has_trivial_destructor(_Value_type)>:: __destroy_n(__first, __count); +#endif } +#pragma GCC diagnostic pop #if __glibcxx_raw_memory_algorithms // >= C++17 template