public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org>
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
Date: Sat, 13 Jan 2024 04:02:40 +0000 (GMT)	[thread overview]
Message-ID: <20240113040240.C39A63858D20@sourceware.org> (raw)

https://gcc.gnu.org/g:c48bedd180672276cc58f379a6346309366b7ea7

commit r14-7218-gc48bedd180672276cc58f379a6346309366b7ea7
Author: Patrick Palka <ppalka@redhat.com>
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<_Lhs>, 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<typename... _Ts>
+	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<typename _Tp>
+	constexpr
+	_Partial(int, _Tp&& __arg)
+	  : _M_arg(std::forward<_Tp>(__arg))
+	{ }
 
 #if __cpp_explicit_this_parameter
       template<typename _Self, typename _Range>
@@ -1099,10 +1106,11 @@ namespace views::__adaptor
     {
       tuple<_Args...> _M_args;
 
-      constexpr
-      _Partial(_Args... __args)
-	: _M_args(std::move(__args)...)
-      { }
+      template<typename... _Ts>
+	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<typename _Tp>
+	constexpr
+	_Partial(int, _Tp&& __arg)
+	  : _M_arg(std::forward<_Tp>(__arg))
+	{ }
 
       template<typename _Range>
 	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<typename _Tp, typename _Up>
+	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<typename _Tp, typename _Up>
+	constexpr
+	_Pipe(_Tp&& __lhs, _Up&& __rhs)
+	  : _M_lhs(std::forward<_Tp>(__lhs)), _M_rhs(std::forward<_Up>(__rhs))
+	{ }
 
       template<typename _Range>
 	requires __pipe_invocable<const _Lhs&, const _Rhs&, _Range>
@@ -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

                 reply	other threads:[~2024-01-13  4:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240113040240.C39A63858D20@sourceware.org \
    --to=ppalka@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).