Message ID | 20240110214007.2478417-1-ppalka@redhat.com |
---|---|
State | New |
Headers | show |
Series | libstdc++/ranges: Use perfect forwarding in _Pipe and _Partial ctors | expand |
On Wed, 10 Jan 2024 at 21:40, Patrick Palka <ppalka@redhat.com> wrote: > > Tested on x86_64-pc-linux-gnu, does this look OK for trunk? > > -- >8 -- > > This avoids redundant moves when composing and partially applying range > adaptor objects. > > Note that the new constraints on _Partial's constructor templates are > needed so that it's not inadvertently chosen over the copy constructor > when constructing a _Partial object from a non-const _Partial lvalue. > > libstdc++-v3/ChangeLog: > > * include/std/ranges (views::__adaptor::operator|): Perform > perfect forwarding of arguments. > (views::__adaptor::_Partial::_Partial): Likewise. > (views::__adaptor::_Pipe::__Pipe): Likewise. > --- > libstdc++-v3/include/std/ranges | 65 ++++++++++++++++++++------------- > 1 file changed, 39 insertions(+), 26 deletions(-) > > diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges > index 81a857502e3..0734daa42bf 100644 > --- a/libstdc++-v3/include/std/ranges > +++ b/libstdc++-v3/include/std/ranges > @@ -957,8 +957,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. > // > @@ -1004,10 +1007,12 @@ namespace views::__adaptor > { > tuple<_Args...> _M_args; > > - constexpr > - _Partial(_Args... __args) > - : _M_args(std::move(__args)...) > - { } > + template<typename... _Ts> > + requires (sizeof...(_Ts) == sizeof...(_Args)) Do we also need a !same_as constraint here? If sizeof...(_Args) == 1 then this could be chosen instead of a constructor, no? Or is > + constexpr > + _Partial(_Ts&&... __args) > + : _M_args(std::forward<_Ts>(__args)...) > + { } > > // Invoke _Adaptor with arguments __r, _M_args... according to the > // value category of this _Partial object. > @@ -1046,10 +1051,12 @@ namespace views::__adaptor > { > _Arg _M_arg; > > - constexpr > - _Partial(_Arg __arg) > - : _M_arg(std::move(__arg)) > - { } > + template<typename _Tp> > + requires (!same_as<remove_cvref_t<_Tp>, _Partial>) > + constexpr > + _Partial(_Tp&& __arg) > + : _M_arg(std::forward<_Tp>(__arg)) > + { } > > template<typename _Range> > requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> > @@ -1079,10 +1086,12 @@ namespace views::__adaptor > { > tuple<_Args...> _M_args; > > - constexpr > - _Partial(_Args... __args) > - : _M_args(std::move(__args)...) > - { } > + template<typename... _Ts> > + requires (sizeof...(_Ts) == sizeof...(_Args)) > + constexpr > + _Partial(_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. > @@ -1109,10 +1118,12 @@ namespace views::__adaptor > { > _Arg _M_arg; > > - constexpr > - _Partial(_Arg __arg) > - : _M_arg(std::move(__arg)) > - { } > + template<typename _Tp> > + requires (!same_as<remove_cvref_t<_Tp>, _Partial>) > + constexpr > + _Partial(_Tp&& __arg) > + : _M_arg(std::forward<_Tp>(__arg)) > + { } > > template<typename _Range> > requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> > @@ -1135,10 +1146,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. > @@ -1172,10 +1184,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> > -- > 2.43.0.283.ga54a84b333 >
On Thu, 11 Jan 2024, Jonathan Wakely wrote: > On Wed, 10 Jan 2024 at 21:40, Patrick Palka <ppalka@redhat.com> wrote: > > > > Tested on x86_64-pc-linux-gnu, does this look OK for trunk? > > > > -- >8 -- > > > > This avoids redundant moves when composing and partially applying range > > adaptor objects. > > > > Note that the new constraints on _Partial's constructor templates are > > needed so that it's not inadvertently chosen over the copy constructor > > when constructing a _Partial object from a non-const _Partial lvalue. > > > > libstdc++-v3/ChangeLog: > > > > * include/std/ranges (views::__adaptor::operator|): Perform > > perfect forwarding of arguments. > > (views::__adaptor::_Partial::_Partial): Likewise. > > (views::__adaptor::_Pipe::__Pipe): Likewise. > > --- > > libstdc++-v3/include/std/ranges | 65 ++++++++++++++++++++------------- > > 1 file changed, 39 insertions(+), 26 deletions(-) > > > > diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges > > index 81a857502e3..0734daa42bf 100644 > > --- a/libstdc++-v3/include/std/ranges > > +++ b/libstdc++-v3/include/std/ranges > > @@ -957,8 +957,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. > > // > > @@ -1004,10 +1007,12 @@ namespace views::__adaptor > > { > > tuple<_Args...> _M_args; > > > > - constexpr > > - _Partial(_Args... __args) > > - : _M_args(std::move(__args)...) > > - { } > > + template<typename... _Ts> > > + requires (sizeof...(_Ts) == sizeof...(_Args)) > > Do we also need a !same_as constraint here? If sizeof...(_Args) == 1 > then this could be chosen instead of a constructor, no? For the == 1 case we would have used a different partial specialization of _Partial (one that doesn't use heavyweight std::tuple). So checking sizeof...(_Ts) == sizof...(_Args) instead is sufficient and simpler. Checking sizeof...(_Args) > 1 would be sufficient as well. > > Or is > > + constexpr > > + _Partial(_Ts&&... __args) > > + : _M_args(std::forward<_Ts>(__args)...) > > + { } > > > > // Invoke _Adaptor with arguments __r, _M_args... according to the > > // value category of this _Partial object. > > @@ -1046,10 +1051,12 @@ namespace views::__adaptor > > { > > _Arg _M_arg; > > > > - constexpr > > - _Partial(_Arg __arg) > > - : _M_arg(std::move(__arg)) > > - { } > > + template<typename _Tp> > > + requires (!same_as<remove_cvref_t<_Tp>, _Partial>) > > + constexpr > > + _Partial(_Tp&& __arg) > > + : _M_arg(std::forward<_Tp>(__arg)) > > + { } > > > > template<typename _Range> > > requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> > > @@ -1079,10 +1086,12 @@ namespace views::__adaptor > > { > > tuple<_Args...> _M_args; > > > > - constexpr > > - _Partial(_Args... __args) > > - : _M_args(std::move(__args)...) > > - { } > > + template<typename... _Ts> > > + requires (sizeof...(_Ts) == sizeof...(_Args)) > > + constexpr > > + _Partial(_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. > > @@ -1109,10 +1118,12 @@ namespace views::__adaptor > > { > > _Arg _M_arg; > > > > - constexpr > > - _Partial(_Arg __arg) > > - : _M_arg(std::move(__arg)) > > - { } > > + template<typename _Tp> > > + requires (!same_as<remove_cvref_t<_Tp>, _Partial>) > > + constexpr > > + _Partial(_Tp&& __arg) > > + : _M_arg(std::forward<_Tp>(__arg)) > > + { } > > > > template<typename _Range> > > requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> > > @@ -1135,10 +1146,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. > > @@ -1172,10 +1184,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> > > -- > > 2.43.0.283.ga54a84b333 > > > >
On Thu, 11 Jan 2024 at 16:12, Patrick Palka <ppalka@redhat.com> wrote: > > On Thu, 11 Jan 2024, Jonathan Wakely wrote: > > > On Wed, 10 Jan 2024 at 21:40, Patrick Palka <ppalka@redhat.com> wrote: > > > > > > Tested on x86_64-pc-linux-gnu, does this look OK for trunk? > > > > > > -- >8 -- > > > > > > This avoids redundant moves when composing and partially applying range > > > adaptor objects. > > > > > > Note that the new constraints on _Partial's constructor templates are > > > needed so that it's not inadvertently chosen over the copy constructor > > > when constructing a _Partial object from a non-const _Partial lvalue. > > > > > > libstdc++-v3/ChangeLog: > > > > > > * include/std/ranges (views::__adaptor::operator|): Perform > > > perfect forwarding of arguments. > > > (views::__adaptor::_Partial::_Partial): Likewise. > > > (views::__adaptor::_Pipe::__Pipe): Likewise. > > > --- > > > libstdc++-v3/include/std/ranges | 65 ++++++++++++++++++++------------- > > > 1 file changed, 39 insertions(+), 26 deletions(-) > > > > > > diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges > > > index 81a857502e3..0734daa42bf 100644 > > > --- a/libstdc++-v3/include/std/ranges > > > +++ b/libstdc++-v3/include/std/ranges > > > @@ -957,8 +957,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. > > > // > > > @@ -1004,10 +1007,12 @@ namespace views::__adaptor > > > { > > > tuple<_Args...> _M_args; > > > > > > - constexpr > > > - _Partial(_Args... __args) > > > - : _M_args(std::move(__args)...) > > > - { } > > > + template<typename... _Ts> > > > + requires (sizeof...(_Ts) == sizeof...(_Args)) > > > > Do we also need a !same_as constraint here? If sizeof...(_Args) == 1 > > then this could be chosen instead of a constructor, no? > > For the == 1 case we would have used a different partial specialization > of _Partial (one that doesn't use heavyweight std::tuple). So checking > sizeof...(_Ts) == sizof...(_Args) instead is sufficient and simpler. Aha thanks, I did wonder if that was the case but failed to see the other partial specialization. Looks fine for trunk then. > Checking sizeof...(_Args) > 1 would be sufficient as well. > > > > > Or is > > > + constexpr > > > + _Partial(_Ts&&... __args) > > > + : _M_args(std::forward<_Ts>(__args)...) > > > + { } > > > > > > // Invoke _Adaptor with arguments __r, _M_args... according to the > > > // value category of this _Partial object. > > > @@ -1046,10 +1051,12 @@ namespace views::__adaptor > > > { > > > _Arg _M_arg; > > > > > > - constexpr > > > - _Partial(_Arg __arg) > > > - : _M_arg(std::move(__arg)) > > > - { } > > > + template<typename _Tp> > > > + requires (!same_as<remove_cvref_t<_Tp>, _Partial>) > > > + constexpr > > > + _Partial(_Tp&& __arg) > > > + : _M_arg(std::forward<_Tp>(__arg)) > > > + { } > > > > > > template<typename _Range> > > > requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> > > > @@ -1079,10 +1086,12 @@ namespace views::__adaptor > > > { > > > tuple<_Args...> _M_args; > > > > > > - constexpr > > > - _Partial(_Args... __args) > > > - : _M_args(std::move(__args)...) > > > - { } > > > + template<typename... _Ts> > > > + requires (sizeof...(_Ts) == sizeof...(_Args)) > > > + constexpr > > > + _Partial(_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. > > > @@ -1109,10 +1118,12 @@ namespace views::__adaptor > > > { > > > _Arg _M_arg; > > > > > > - constexpr > > > - _Partial(_Arg __arg) > > > - : _M_arg(std::move(__arg)) > > > - { } > > > + template<typename _Tp> > > > + requires (!same_as<remove_cvref_t<_Tp>, _Partial>) > > > + constexpr > > > + _Partial(_Tp&& __arg) > > > + : _M_arg(std::forward<_Tp>(__arg)) > > > + { } > > > > > > template<typename _Range> > > > requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> > > > @@ -1135,10 +1146,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. > > > @@ -1172,10 +1184,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> > > > -- > > > 2.43.0.283.ga54a84b333 > > > > > > > >
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 81a857502e3..0734daa42bf 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -957,8 +957,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. // @@ -1004,10 +1007,12 @@ namespace views::__adaptor { tuple<_Args...> _M_args; - constexpr - _Partial(_Args... __args) - : _M_args(std::move(__args)...) - { } + template<typename... _Ts> + requires (sizeof...(_Ts) == sizeof...(_Args)) + constexpr + _Partial(_Ts&&... __args) + : _M_args(std::forward<_Ts>(__args)...) + { } // Invoke _Adaptor with arguments __r, _M_args... according to the // value category of this _Partial object. @@ -1046,10 +1051,12 @@ namespace views::__adaptor { _Arg _M_arg; - constexpr - _Partial(_Arg __arg) - : _M_arg(std::move(__arg)) - { } + template<typename _Tp> + requires (!same_as<remove_cvref_t<_Tp>, _Partial>) + constexpr + _Partial(_Tp&& __arg) + : _M_arg(std::forward<_Tp>(__arg)) + { } template<typename _Range> requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> @@ -1079,10 +1086,12 @@ namespace views::__adaptor { tuple<_Args...> _M_args; - constexpr - _Partial(_Args... __args) - : _M_args(std::move(__args)...) - { } + template<typename... _Ts> + requires (sizeof...(_Ts) == sizeof...(_Args)) + constexpr + _Partial(_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. @@ -1109,10 +1118,12 @@ namespace views::__adaptor { _Arg _M_arg; - constexpr - _Partial(_Arg __arg) - : _M_arg(std::move(__arg)) - { } + template<typename _Tp> + requires (!same_as<remove_cvref_t<_Tp>, _Partial>) + constexpr + _Partial(_Tp&& __arg) + : _M_arg(std::forward<_Tp>(__arg)) + { } template<typename _Range> requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> @@ -1135,10 +1146,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. @@ -1172,10 +1184,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>