From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id C39A63858D20; Sat, 13 Jan 2024 04:02:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C39A63858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705118560; bh=NMsNhA0meu+VxDb8E25twOPEUvnFzOd61dBjMjw0M+w=; h=From:To:Subject:Date:From; b=eShC16+p9GCrZ40pH0+XHW9mbP5gnNJH2EsEuDugXka8EOUPbts0PHhGlyr1OXCFD OgWN0fYiDZ19r2DLyE9n1nae2fGCQWdmbIPw4ZwWMcfW/ul2rqZZNtY2aKbT0vMeOF BEcrzqrCxg7U7VnSZutfvTcbAMmuwK3fFnhXh0K4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-7218] libstdc++/ranges: Use perfect forwarding in _Pipe and _Partial ctors X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: 444a31f3b3542ccbecb67cef3a01df8aa9a43802 X-Git-Newrev: c48bedd180672276cc58f379a6346309366b7ea7 Message-Id: <20240113040240.C39A63858D20@sourceware.org> Date: Sat, 13 Jan 2024 04:02:40 +0000 (GMT) List-Id: https://gcc.gnu.org/g:c48bedd180672276cc58f379a6346309366b7ea7 commit r14-7218-gc48bedd180672276cc58f379a6346309366b7ea7 Author: Patrick Palka Date: Fri Jan 12 22:54:59 2024 -0500 libstdc++/ranges: Use perfect forwarding in _Pipe and _Partial ctors This avoids redundant moves when composing and partially applying range adaptor objects. libstdc++-v3/ChangeLog: * include/std/ranges (views::__adaptor::operator|): Perform perfect forwarding of arguments. (views::__adaptor::_RangeAdaptor::operator()): Pass dummy first argument to _Partial. (views::__adaptor::_Partial::_Partial): Likewise. Add dummy first parameter. (views::__adaptor::_Pipe::_Pipe): Perform perfect forwarding of arguments. (to): Pass dummy first argument to _Partial. Diff: --- libstdc++-v3/include/std/ranges | 69 ++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 5ef4443fc64..3135e6f0c08 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -956,8 +956,11 @@ namespace views::__adaptor requires __is_range_adaptor_closure<_Lhs> && __is_range_adaptor_closure<_Rhs> constexpr auto - operator|(_Lhs __lhs, _Rhs __rhs) - { return _Pipe<_Lhs, _Rhs>{std::move(__lhs), std::move(__rhs)}; } + operator|(_Lhs&& __lhs, _Rhs&& __rhs) + { + return _Pipe, decay_t<_Rhs>>{std::forward<_Lhs>(__lhs), + std::forward<_Rhs>(__rhs)}; + } // The base class of every range adaptor non-closure. // @@ -980,7 +983,7 @@ namespace views::__adaptor constexpr auto operator()(_Args&&... __args) const { - return _Partial<_Derived, decay_t<_Args>...>{std::forward<_Args>(__args)...}; + return _Partial<_Derived, decay_t<_Args>...>{0, std::forward<_Args>(__args)...}; } }; @@ -1003,10 +1006,13 @@ namespace views::__adaptor { tuple<_Args...> _M_args; - constexpr - _Partial(_Args... __args) - : _M_args(std::move(__args)...) - { } + // First parameter is to ensure this constructor is never used + // instead of the copy/move constructor. + template + constexpr + _Partial(int, _Ts&&... __args) + : _M_args(std::forward<_Ts>(__args)...) + { } // Invoke _Adaptor with arguments __r, _M_args... according to the // value category of this _Partial object. @@ -1058,10 +1064,11 @@ namespace views::__adaptor { _Arg _M_arg; - constexpr - _Partial(_Arg __arg) - : _M_arg(std::move(__arg)) - { } + template + constexpr + _Partial(int, _Tp&& __arg) + : _M_arg(std::forward<_Tp>(__arg)) + { } #if __cpp_explicit_this_parameter template @@ -1099,10 +1106,11 @@ namespace views::__adaptor { tuple<_Args...> _M_args; - constexpr - _Partial(_Args... __args) - : _M_args(std::move(__args)...) - { } + template + constexpr + _Partial(int, _Ts&&... __args) + : _M_args(std::forward<_Ts>(__args)...) + { } // Invoke _Adaptor with arguments __r, const _M_args&... regardless // of the value category of this _Partial object. @@ -1129,10 +1137,11 @@ namespace views::__adaptor { _Arg _M_arg; - constexpr - _Partial(_Arg __arg) - : _M_arg(std::move(__arg)) - { } + template + constexpr + _Partial(int, _Tp&& __arg) + : _M_arg(std::forward<_Tp>(__arg)) + { } template requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> @@ -1155,10 +1164,11 @@ namespace views::__adaptor [[no_unique_address]] _Lhs _M_lhs; [[no_unique_address]] _Rhs _M_rhs; - constexpr - _Pipe(_Lhs __lhs, _Rhs __rhs) - : _M_lhs(std::move(__lhs)), _M_rhs(std::move(__rhs)) - { } + template + constexpr + _Pipe(_Tp&& __lhs, _Up&& __rhs) + : _M_lhs(std::forward<_Tp>(__lhs)), _M_rhs(std::forward<_Up>(__rhs)) + { } // Invoke _M_rhs(_M_lhs(__r)) according to the value category of this // range adaptor closure object. @@ -1203,10 +1213,11 @@ namespace views::__adaptor [[no_unique_address]] _Lhs _M_lhs; [[no_unique_address]] _Rhs _M_rhs; - constexpr - _Pipe(_Lhs __lhs, _Rhs __rhs) - : _M_lhs(std::move(__lhs)), _M_rhs(std::move(__rhs)) - { } + template + constexpr + _Pipe(_Tp&& __lhs, _Up&& __rhs) + : _M_lhs(std::forward<_Tp>(__lhs)), _M_rhs(std::forward<_Up>(__rhs)) + { } template requires __pipe_invocable @@ -9458,7 +9469,7 @@ namespace __detail { using __detail::_To; using views::__adaptor::_Partial; - return _Partial<_To<_Cont>, decay_t<_Args>...>{std::forward<_Args>(__args)...}; + return _Partial<_To<_Cont>, decay_t<_Args>...>{0, std::forward<_Args>(__args)...}; } /// @cond undocumented @@ -9503,7 +9514,7 @@ namespace __detail { using __detail::_To2; using views::__adaptor::_Partial; - return _Partial<_To2<_Cont>, decay_t<_Args>...>{std::forward<_Args>(__args)...}; + return _Partial<_To2<_Cont>, decay_t<_Args>...>{0, std::forward<_Args>(__args)...}; } } // namespace ranges