Message ID | 20220823013500.1756466-1-ppalka@redhat.com |
---|---|
State | New, archived |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp747415wro; Mon, 22 Aug 2022 18:36:53 -0700 (PDT) X-Google-Smtp-Source: AA6agR7zpEkNBJSxNS6zFPWwTPu8wgfX4NJbHZLvEW6W0uPYnYE93nHstwZZGR0UMtcxF1Fv/nDo X-Received: by 2002:a05:6402:44c:b0:445:f2f1:4add with SMTP id p12-20020a056402044c00b00445f2f14addmr1544869edw.257.1661218612882; Mon, 22 Aug 2022 18:36:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661218612; cv=none; d=google.com; s=arc-20160816; b=Ea6WlazI73SLO4gnm7LXIc3JdeojdWfEC7tID4nw2UgoRQOHUjD2ThiyX09U5m2O4t NSfJ6yisyynTTM0tSZJF1n160qGyCCu48MRN1gOAIDkTEIozbYgo60Sp7q2yZdQZr2Fd qg3CYGFOxRGsnmsWmRU5Bj4kTrBMVjNbDtoETnciGdsYw+egWeMkL/gh70zqg1h0PXD0 glMBacRbJ4aW90aJJYthLHGFlkLrlLnCWmA+a9lPaBOz9jKrUZGhNjs1HNmZk/MmKCRi P5NmJTVXLc5LNtUmlVAuUktfPh4sSXW75dI4bJb6uWangcTHUyTGR1Fawu0qIKSFIUJe xcRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=xwJga+iKr7YMPjR2NLqP6/50JDlQiXx2+7MRt68EoOY=; b=ylcSgrGPgXrohIX0lMjE0WFK/ZoDiyj24EyFPpLNiSlhgTSeSXVLq0qpX/HZGY6YOE JB8vXB4AdfFLmaHgjsoWa8qfN7tMb5PTrltFefVFjKAdKZ2e9RAVkM58xNRbPu+smw/m ydGo7FCHzqxiM43H2Ts0fTGP3dc4iTSAxihN0Tnsn97/qqAteQD5Uk5cyOvQS//hLdIZ UKJn788Dysy+P9KatboQtHTTtCNFPLgnVnw9J1MN/RUm6Aviex9FyThzW7qpkBJQ2gL+ 08o73y8JNf8WMGJePYov8n2+nS9S8kpR9QCSm+JgmVNR1Qx3k8nMkgIuWBK4fCOtsrSo 4odg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=O4qEiJ8n; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id hq4-20020a1709073f0400b00726b8d2fbc6si11555137ejc.504.2022.08.22.18.36.52 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Aug 2022 18:36:52 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=O4qEiJ8n; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 511C1385085B for <ouuuleilei@gmail.com>; Tue, 23 Aug 2022 01:35:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 511C1385085B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661218549; bh=xwJga+iKr7YMPjR2NLqP6/50JDlQiXx2+7MRt68EoOY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=O4qEiJ8nstPrgqcrYDxF5gYmElduZxrQx+RQYhoTb4jV75a82BFmLsa3zcH5CK44c +fEMHlybtscw8mr1Vu4NB/penPRLjbzNDGbgcuiKQiN+2TXORGSk62rpOSQ5Tic/Jk pYUESWX+wo7tYPR+CULP4h56b1wI8nFyEy22lYZU= 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 B1DF03858D3C for <gcc-patches@gcc.gnu.org>; Tue, 23 Aug 2022 01:35:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B1DF03858D3C Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-418-FZ4Y4yc6PWyDfVouYcHcGA-1; Mon, 22 Aug 2022 21:35:04 -0400 X-MC-Unique: FZ4Y4yc6PWyDfVouYcHcGA-1 Received: by mail-qv1-f70.google.com with SMTP id ea4-20020ad458a4000000b0049682af0ca8so6600138qvb.21 for <gcc-patches@gcc.gnu.org>; Mon, 22 Aug 2022 18:35:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc; bh=xwJga+iKr7YMPjR2NLqP6/50JDlQiXx2+7MRt68EoOY=; b=qo7SNJvOGFgMExIJLsk8EOgyxyHTf/9UoAFauBoDQIMPuxc8nwYEID1Bfu98JLbcZw WOldMaoAcOQcMxKstp5mESXKCWsqldhT+KGngkz5+NGFeIHcOf24R5DdYgFC8QOM4aAr hq8l9ghzl/WCe24IhZM/AWdQn11dVopQ0O8fKLw9UcvONzTML4JoXv0MPEIFOZQa7EIK ZSTkFMvMDsBuaSJdOYxazgHKbmGcpujbqdSuc3y96ft8MV1f7ZV9ocKFW01dDU+/z2k7 1kBWr6Vj/GmBzXfkNiCob14ZYRow8dT/LPmUIlEn8COQq3S1bqpBx2q+/vTC4v0rXOrK S3LQ== X-Gm-Message-State: ACgBeo1Qigro+yGUdgg8y+woZc8rfywuA3ng6gAMZSsBEMvkUWND2cF1 ihSug23KVobdiJ50CQCv0sWO4nqUNrH/GpKKh03Q9ApCI8VuUxGv/9YKKqeLyaup7FEwMt6J2Xx L0qrtVd/4zlRJnIKdzGvSfbQz+zFcsnZkRt1sgMi40gwFVSAduFEnTRgaup6IAJ0Cyf4= X-Received: by 2002:a05:622a:3d0:b0:343:58db:d5c7 with SMTP id k16-20020a05622a03d000b0034358dbd5c7mr17364584qtx.21.1661218503700; Mon, 22 Aug 2022 18:35:03 -0700 (PDT) X-Received: by 2002:a05:622a:3d0:b0:343:58db:d5c7 with SMTP id k16-20020a05622a03d000b0034358dbd5c7mr17364566qtx.21.1661218503403; Mon, 22 Aug 2022 18:35:03 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id p11-20020ac8740b000000b00342fcdc2d46sm9816942qtq.56.2022.08.22.18.35.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Aug 2022 18:35:03 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/3] libstdc++: Separate construct/convertibility tests for std::tuple Date: Mon, 22 Aug 2022 21:34:58 -0400 Message-Id: <20220823013500.1756466-1-ppalka@redhat.com> X-Mailer: git-send-email 2.37.2.382.g795ea8776b MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-14.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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Patrick Palka via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Patrick Palka <ppalka@redhat.com> Cc: libstdc++@gcc.gnu.org Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1741913968550540636?= X-GMAIL-MSGID: =?utf-8?q?1741913968550540636?= |
Series |
[1/3] libstdc++: Separate construct/convertibility tests for std::tuple
|
|
Commit Message
Patrick Palka
Aug. 23, 2022, 1:34 a.m. UTC
P2321R2 adds new conditionally explicit constructors to std::tuple which we'll concisely implement in a subsequent patch using explicit(bool), like in our C++20 std::pair implementation. But before we can do that, this patch first adds members to _TupleConstraints that test for constructibility and convertibility separately; we'll use the first in the new constructors' constraints, and the second in their explicit specifier. In passing, this patch also redefines the existing predicates __is_ex/implicitly_constructible in terms of these new members. This seems to reduce compile time and memory usage by about 10% for large tuples when using the relevant constructors constrained by _Explicit/_ImplicitCtor (since we no longer have to redundantly expand and process is_constructible<_Types, _UTypes>... twice for each pair of such constructors). In order to retain maximal short circuiting, do this only when constexpr if is available. Tested on x86_64-pc-linux-gnu, does this look OK for trunk? libstdc++-v3/ChangeLog: * include/std/tuple (_TupleConstraints::__convertible): Define for C++17. (_TupleConstraints::__constructible): Likewise. (_TupleConstraints::__is_explicitly_constructible): For C++17 define this in terms of __convertible and __constructible using constexpr if. (_TupleConstraints::__is_implicitly_constructible): Likewise. --- libstdc++-v3/include/std/tuple | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
Comments
On Tue, 23 Aug 2022 at 02:35, Patrick Palka via Libstdc++ <libstdc++@gcc.gnu.org> wrote: > > P2321R2 adds new conditionally explicit constructors to std::tuple which > we'll concisely implement in a subsequent patch using explicit(bool), like > in our C++20 std::pair implementation. But before we can do that, this > patch first adds members to _TupleConstraints that test for constructibility > and convertibility separately; we'll use the first in the new constructors' > constraints, and the second in their explicit specifier. > > In passing, this patch also redefines the existing predicates > __is_ex/implicitly_constructible in terms of these new members. This > seems to reduce compile time and memory usage by about 10% for large Nice. > tuples when using the relevant constructors constrained by > _Explicit/_ImplicitCtor (since we no longer have to redundantly expand > and process is_constructible<_Types, _UTypes>... twice for each pair of > such constructors). In order to retain maximal short circuiting, do > this only when constexpr if is available. Would we get similar benefits for C++11 and C++14 by doing: return __and_<__and_<is_constructible<_Types, _UTypes>...>, __and_<is_convertible<_UTypes, _Types>...> >::value; This is slightly more work in total, but if we have __and_<A,B> and __and_<A,__not_<B>> then the A and B instantiations will be cached and can be reused. > Tested on x86_64-pc-linux-gnu, does this look OK for trunk? Yes, thanks.
On Tue, 23 Aug 2022, Jonathan Wakely wrote: > On Tue, 23 Aug 2022 at 02:35, Patrick Palka via Libstdc++ > <libstdc++@gcc.gnu.org> wrote: > > > > P2321R2 adds new conditionally explicit constructors to std::tuple which > > we'll concisely implement in a subsequent patch using explicit(bool), like > > in our C++20 std::pair implementation. But before we can do that, this > > patch first adds members to _TupleConstraints that test for constructibility > > and convertibility separately; we'll use the first in the new constructors' > > constraints, and the second in their explicit specifier. > > > > In passing, this patch also redefines the existing predicates > > __is_ex/implicitly_constructible in terms of these new members. This > > seems to reduce compile time and memory usage by about 10% for large > > Nice. > > > tuples when using the relevant constructors constrained by > > _Explicit/_ImplicitCtor (since we no longer have to redundantly expand > > and process is_constructible<_Types, _UTypes>... twice for each pair of > > such constructors). In order to retain maximal short circuiting, do > > this only when constexpr if is available. > > Would we get similar benefits for C++11 and C++14 by doing: > > return __and_<__and_<is_constructible<_Types, _UTypes>...>, > __and_<is_convertible<_UTypes, _Types>...> > >::value; > > This is slightly more work in total, but if we have __and_<A,B> and > __and_<A,__not_<B>> then the A and B instantiations will be cached and > can be reused. Good idea, it seems we get pretty much the same 10% improvement for C++11/14 with this approach. I reckon we might as well then define __convertible and __constructible as alias templates instead of as variable templates and use them unconditionally in __is_im/explicitly_constructible for benefit of all language versions. Using constexpr if instead of the outer __and_ seems to yield a marginal improvement at best and __and_v is currently just __and_::value, so it doesn't seem worth it to have different definitions for C++17 at least for now. What do you think about the following? -- >8 -- libstdc++-v3/ChangeLog: * include/std/tuple (_TupleConstraints::__convertible): Define. (_TupleConstraints::__constructible): Likewise. (_TupleConstraints::__is_explicitly_constructible): Redefine this in terms of __convertible and __constructible. (_TupleConstraints::__is_implicitly_constructible): Likewise. --- libstdc++-v3/include/std/tuple | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple index 6d0060a191c..f8f48ccc370 100644 --- a/libstdc++-v3/include/std/tuple +++ b/libstdc++-v3/include/std/tuple @@ -553,15 +553,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template<bool, typename... _Types> struct _TupleConstraints { + template<typename... _UTypes> + using __constructible = __and_<is_constructible<_Types, _UTypes>...>; + + template<typename... _UTypes> + using __convertible = __and_<is_convertible<_UTypes, _Types>...>; + // Constraint for a non-explicit constructor. // True iff each Ti in _Types... can be constructed from Ui in _UTypes... // and every Ui is implicitly convertible to Ti. template<typename... _UTypes> static constexpr bool __is_implicitly_constructible() { - return __and_<is_constructible<_Types, _UTypes>..., - is_convertible<_UTypes, _Types>... - >::value; + return __and_<__constructible<_UTypes...>, + __convertible<_UTypes...>>::value; } // Constraint for a non-explicit constructor. @@ -570,9 +575,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template<typename... _UTypes> static constexpr bool __is_explicitly_constructible() { - return __and_<is_constructible<_Types, _UTypes>..., - __not_<__and_<is_convertible<_UTypes, _Types>...>> - >::value; + return __and_<__constructible<_UTypes...>, + __not_<__convertible<_UTypes...>>>::value; } static constexpr bool __is_implicitly_default_constructible()
On Tue, 23 Aug 2022 at 14:44, Patrick Palka <ppalka@redhat.com> wrote: > > On Tue, 23 Aug 2022, Jonathan Wakely wrote: > > > On Tue, 23 Aug 2022 at 02:35, Patrick Palka via Libstdc++ > > <libstdc++@gcc.gnu.org> wrote: > > > > > > P2321R2 adds new conditionally explicit constructors to std::tuple which > > > we'll concisely implement in a subsequent patch using explicit(bool), like > > > in our C++20 std::pair implementation. But before we can do that, this > > > patch first adds members to _TupleConstraints that test for constructibility > > > and convertibility separately; we'll use the first in the new constructors' > > > constraints, and the second in their explicit specifier. > > > > > > In passing, this patch also redefines the existing predicates > > > __is_ex/implicitly_constructible in terms of these new members. This > > > seems to reduce compile time and memory usage by about 10% for large > > > > Nice. > > > > > tuples when using the relevant constructors constrained by > > > _Explicit/_ImplicitCtor (since we no longer have to redundantly expand > > > and process is_constructible<_Types, _UTypes>... twice for each pair of > > > such constructors). In order to retain maximal short circuiting, do > > > this only when constexpr if is available. > > > > Would we get similar benefits for C++11 and C++14 by doing: > > > > return __and_<__and_<is_constructible<_Types, _UTypes>...>, > > __and_<is_convertible<_UTypes, _Types>...> > > >::value; > > > > This is slightly more work in total, but if we have __and_<A,B> and > > __and_<A,__not_<B>> then the A and B instantiations will be cached and > > can be reused. > > Good idea, it seems we get pretty much the same 10% improvement for > C++11/14 with this approach. I reckon we might as well then define > __convertible and __constructible as alias templates instead of as > variable templates and use them unconditionally in > __is_im/explicitly_constructible for benefit of all language versions. I had a similar thought after hitting send. > Using constexpr if instead of the outer __and_ seems to yield a marginal > improvement at best and __and_v is currently just __and_::value, so it > doesn't seem worth it to have different definitions for C++17 at least > for now. > > What do you think about the following? OK for trunk - thanks.
diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple index 6d0060a191c..d0c168fd7e2 100644 --- a/libstdc++-v3/include/std/tuple +++ b/libstdc++-v3/include/std/tuple @@ -553,15 +553,31 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template<bool, typename... _Types> struct _TupleConstraints { +#if __cplusplus >= 201703L + template<typename... _UTypes> + static constexpr bool __constructible + = __and_v<is_constructible<_Types, _UTypes>...>; + + template<typename... _UTypes> + static constexpr bool __convertible + = __and_v<is_convertible<_UTypes, _Types>...>; +#endif + // Constraint for a non-explicit constructor. // True iff each Ti in _Types... can be constructed from Ui in _UTypes... // and every Ui is implicitly convertible to Ti. template<typename... _UTypes> static constexpr bool __is_implicitly_constructible() { +#if __cplusplus >= 201703L + if constexpr (__constructible<_UTypes...>) + return __convertible<_UTypes...>; + return false; +#else return __and_<is_constructible<_Types, _UTypes>..., is_convertible<_UTypes, _Types>... >::value; +#endif } // Constraint for a non-explicit constructor. @@ -570,9 +586,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template<typename... _UTypes> static constexpr bool __is_explicitly_constructible() { +#if __cplusplus >= 201703L + if constexpr (__constructible<_UTypes...>) + return !__convertible<_UTypes...>; + return false; +#else return __and_<is_constructible<_Types, _UTypes>..., __not_<__and_<is_convertible<_UTypes, _Types>...>> >::value; +#endif } static constexpr bool __is_implicitly_default_constructible()