From patchwork Mon Nov 21 18:51:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 60941 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 458463836004 for ; Mon, 21 Nov 2022 18:52:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 458463836004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669056753; bh=fmRlIzQkKmpsNY4UQz8xI/9+ITLeQogolQT6d7Eg22I=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=oPGhGh2jP5ZpJiIPV6vue0Xbv/3l8s+p35biGR9Y6UCuEOya4F9XRrkXQhqaNad8E LwQpg5UtutiuxDc2SfUOt9IJM/yHlIkYLosohhnD2yCtQvqJS3YTULWsBky+6HFJL1 WtnwuVtEBlXHlM7eXx0JXS9EsjS+pFTpV5EW1Tq0= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id E2726384F6C2 for ; Mon, 21 Nov 2022 18:51:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E2726384F6C2 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-107-I8HM8drAOG2AQTHiwnanaA-1; Mon, 21 Nov 2022 13:51:31 -0500 X-MC-Unique: I8HM8drAOG2AQTHiwnanaA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2CBBD3815D27; Mon, 21 Nov 2022 18:51:31 +0000 (UTC) Received: from localhost (unknown [10.33.36.60]) by smtp.corp.redhat.com (Postfix) with ESMTP id E83C217595; Mon, 21 Nov 2022 18:51:30 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Reduce size of std::bind_front(F) result Date: Mon, 21 Nov 2022 18:51:30 +0000 Message-Id: <20221121185130.2021855-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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_H2, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux. Pushed to trunk. -- >8-- When there are no bound arguments to a std::bind_front call we don't need the overhead of compiling, initializing, and accessing an empty tuple. libstdc++-v3/ChangeLog: * include/std/functional (_Bind_front0): New class template. (_Bind_front_t): Use _Bind_front0 when there are no bound arguments. * testsuite/20_util/function_objects/bind_front/107784.cc: New test. --- libstdc++-v3/include/std/functional | 62 ++++++++++++++++++- .../function_objects/bind_front/107784.cc | 15 +++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/20_util/function_objects/bind_front/107784.cc diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index b396e8dbbdc..dddd22fcd04 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -995,9 +995,69 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION std::tuple<_BoundArgs...> _M_bound_args; }; + // Avoid the overhead of an empty tuple<> if there are no bound args. + template + struct _Bind_front0 + { + static_assert(is_move_constructible_v<_Fd>); + + // First parameter is to ensure this constructor is never used + // instead of the copy/move constructor. + template + explicit constexpr + _Bind_front0(int, _Fn&& __fn) + noexcept(is_nothrow_constructible_v<_Fd, _Fn>) + : _M_fd(std::forward<_Fn>(__fn)) + { } + + _Bind_front0(const _Bind_front0&) = default; + _Bind_front0(_Bind_front0&&) = default; + _Bind_front0& operator=(const _Bind_front0&) = default; + _Bind_front0& operator=(_Bind_front0&&) = default; + ~_Bind_front0() = default; + + template + constexpr + invoke_result_t<_Fd&, _CallArgs...> + operator()(_CallArgs&&... __call_args) & + noexcept(is_nothrow_invocable_v<_Fd&, _CallArgs...>) + { return std::invoke(_M_fd, std::forward<_CallArgs>(__call_args)...); } + + template + constexpr + invoke_result_t + operator()(_CallArgs&&... __call_args) const & + noexcept(is_nothrow_invocable_v) + { return std::invoke(_M_fd, std::forward<_CallArgs>(__call_args)...); } + + template + constexpr + invoke_result_t<_Fd, _CallArgs...> + operator()(_CallArgs&&... __call_args) && + noexcept(is_nothrow_invocable_v<_Fd, _CallArgs...>) + { + return std::invoke(std::move(_M_fd), + std::forward<_CallArgs>(__call_args)...); + } + + template + constexpr + invoke_result_t + operator()(_CallArgs&&... __call_args) const && + noexcept(is_nothrow_invocable_v) + { + return std::invoke(std::move(_M_fd), + std::forward<_CallArgs>(__call_args)...); + } + + private: + _Fd _M_fd; + }; + template using _Bind_front_t - = _Bind_front, decay_t<_Args>...>; + = __conditional_t>, + _Bind_front, decay_t<_Args>...>>; /** Create call wrapper by partial application of arguments to function. * diff --git a/libstdc++-v3/testsuite/20_util/function_objects/bind_front/107784.cc b/libstdc++-v3/testsuite/20_util/function_objects/bind_front/107784.cc new file mode 100644 index 00000000000..ec255f3ee36 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/function_objects/bind_front/107784.cc @@ -0,0 +1,15 @@ +// { dg-options "-std=gnu++20" } +// { dg-do compile { target c++20 } } + +#include + +struct Foo +{ + void func() {} +}; + +void bar() { } + +// PR libstdc++/107784 +static_assert( sizeof(std::bind_front(&Foo::func)) == sizeof(&Foo::func) ); +static_assert( sizeof(std::bind_front(&bar)) == sizeof(&bar) );