From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by sourceware.org (Postfix) with ESMTPS id E115B3858D3C; Wed, 16 Aug 2023 17:32:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E115B3858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-51e28cac164so156206a12.1; Wed, 16 Aug 2023 10:32:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692207147; x=1692811947; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=fDtqIUHr9k7QOTTfZ3KA5rhV+Z7ZFAS2B0OlSxpGeSw=; b=V9ax9q66Oo/3gyqfrSr9IQBAm6FeCqt7mW2Au4WGDXB9CRAjDZwizzFM09ECAvW4UF N6Uiyg1BaHJgfGI+Jo1O8rgu7ee0rfm+b7uqZAQCmZhwi4RiOTE4eAVjD3oChRyCyaEQ jsA6bw87Uhu1QWSRtK31fieiZteVb7D0QieytVI/x+2L3P76tJ8wXrqReAJCaS8xJskZ Qb+snp1nIGuqWyDGJsbxMlZtJL4BKGf0nuszgSnM210z7k2Dy+DaDOSoCnNZwUpKusnE LQtGBV5ywb7XQshqiM93Ao77Jiins8FHwUyeOBRouUAXlKrjta/P0oKkT10wy/27ibWE 8wVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692207147; x=1692811947; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fDtqIUHr9k7QOTTfZ3KA5rhV+Z7ZFAS2B0OlSxpGeSw=; b=EwiZ1zLBA00QCxMDRi+gZgBkqKHWlbLCuVcKUO6k7Ty46VgkimcEBlF1qYsddc5TB1 RoSxO1HbJdF89xH1aak8yC2z9fJtflbc2+EXu3thFXH5eYdPDWJdQHq1Q2Zx1mZLveBe QY4eehX9eOrbjtumkWRrw/ZsP2O7vlHVMss18eaP1q7Wknx3i0z8z1qljEW9JTzXaTJS vMqVTIdrXvcvE73r8LZ9LW8DdSoN534puLB4QmIVYUrbz7zx6a695NSmCfShnlPRixIm fiQSzDJU/Gy5gqu7OIuC45gtpmSmUHdDN+DmkP8hV1464qKtkYaLbWi7gJSgdJ1fMfpR yGCA== X-Gm-Message-State: AOJu0YxQYPu7bXvI9yZwORAEr0grfu366afBHiSep1M3e8XHFpyv8TMM qWvY9TKWXbBi3z81G+MqGxWZSVIEXKosLy+oblA= X-Google-Smtp-Source: AGHT+IH/+bwcnn1cBWZN0tlnvpjseV0faBk8DZTzJ0SZqOpmCq7F/xgRSpXDfipbiv9aTV13uost8zQaiDmG26oVJlk= X-Received: by 2002:a17:906:d54a:b0:974:fb94:8067 with SMTP id cr10-20020a170906d54a00b00974fb948067mr330368ejc.23.1692207147002; Wed, 16 Aug 2023 10:32:27 -0700 (PDT) MIME-Version: 1.0 References: <20230414194512.2569383-1-ppalka@redhat.com> In-Reply-To: From: Jonathan Wakely Date: Wed, 16 Aug 2023 18:32:16 +0100 Message-ID: Subject: Re: [PATCH 1/2] libstdc++: Convert _RangeAdaptorClosure into a CRTP class [PR108827] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-6.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, 16 Aug 2023 at 17:06, Patrick Palka via Libstdc++ wrote: > > On Sun, Apr 16, 2023 at 11:24=E2=80=AFPM Patrick Palka wrote: > > > > On Fri, 14 Apr 2023, Patrick Palka wrote: > > > > > Using the CRTP idiom for this base class avoids bloating the size of = a > > > pipeline when adding distinct empty range adaptor closure objects to = it, > > > as detailed in section 4.1 of P2387R3. > > > > > > But it means we can no longer define its operator| overloads as hidde= n > > > friends, since each instantiation of _RangeAdaptorClosure would then > > > introduce its own logically different hidden friends. So for example > > > during overload resolution for the outer pipe operator in > > > > > > :x | (views::reverse | views::join) > > > > > > we'd have to consider 6 different hidden operator| friends: > > > > > > 2 from _RangeAdaptorClosure<_Reverse> > > > 2 from _RangeAdaptorClosure<_Join> > > > 2 from _RangeAdaptorClosure<_Pipe<_Reverse, _Join>> > > > > > > which is wasteful and can even cause hard errors in some cases. So w= e > > > instead define the operator| overloads at namespace scope in an isola= ted > > > namespace. > > > > On second thought, since this doesn't fix a bug or add new functionalit= y > > it seems more like GCC 14 material. The size reduction is nice but it'= s > > probably not a big deal in practice since adaptor pipelines are usually > > very transient objects that don't get passed around as function > > arguments etc. > > Ping, does this look OK for trunk? OK for trunk, thanks. > > > > > But perhaps the second patch implementing range_adaptor_closure would b= e > > desirable for GCC 13? I'll post an updated standalone version of that > > patch for separate consideration. > > > > > > > > PR libstdc++/108827 > > > > > > libstdc++-v3/ChangeLog: > > > > > > * include/std/ranges (__adaptor::_RangeAdaptorClosure): Move ..= . > > > (__adaptor::__closure::_RangeAdaptorClosure): ... here and turn > > > it into a CRTP class template. Move hidden operator| friends > > > into namespace scope and adjust their constraints. Add a > > > using-declaration for this at __adaptor::_RangeAdaptorClosure. > > > (__closure::__is_range_adaptor_closure_fn): Define. > > > (__closure::__is_range_adaptor_closure): Define. > > > (__adaptor::_Partial): Adjust use of _RangeAdaptorClosure. > > > (__adaptor::_Pipe): Likewise. > > > (views::_All): Likewise. > > > (views::_Join): Likewise. > > > (views::_Common): Likewise. > > > (views::_Reverse): Likewise. > > > (views::_Elements): Likewise. > > > (views::_Adjacent): Likewise. > > > (views::_AsRvalue): Likewise. > > > (views::_Enumerate): Likewise. > > > (views::_AsConst): Likewise. > > > * testsuite/std/ranges/adaptors/all.cc: Reintroduce > > > static_assert expecting that adding empty range adaptor > > > closure objects to a pipeline doesn't increase the size of a > > > pipeline. > > > --- > > > libstdc++-v3/include/std/ranges | 69 +++++++++++------= -- > > > .../testsuite/std/ranges/adaptors/all.cc | 7 -- > > > 2 files changed, 42 insertions(+), 34 deletions(-) > > > > > > diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/s= td/ranges > > > index 283d757faa4..531ec6f68b3 100644 > > > --- a/libstdc++-v3/include/std/ranges > > > +++ b/libstdc++-v3/include/std/ranges > > > @@ -872,30 +872,45 @@ namespace views::__adaptor > > > template > > > struct _Pipe; > > > > > > - // The base class of every range adaptor closure. > > > - // > > > - // The derived class should define the optional static data member > > > - // _S_has_simple_call_op to true if the behavior of this adaptor i= s > > > - // independent of the constness/value category of the adaptor obje= ct. > > > - struct _RangeAdaptorClosure > > > + namespace __closure > > > { > > > + // The base class of every range adaptor closure. > > > + // > > > + // The derived class should define the optional static data memb= er > > > + // _S_has_simple_call_op to true if the behavior of this adaptor= is > > > + // independent of the constness/value category of the adaptor ob= ject. > > > + template > > > + struct _RangeAdaptorClosure > > > + { }; > > > + > > > + template > > > + requires (!same_as<_Tp, _RangeAdaptorClosure<_Up>>) > > > + void __is_range_adaptor_closure_fn > > > + (const _Tp&, const _RangeAdaptorClosure<_Up>&); // not defined > > > + > > > + template > > > + concept __is_range_adaptor_closure > > > + =3D requires (_Tp __t) { __closure::__is_range_adaptor_closure_= fn(__t, __t); }; > > > + > > > // range | adaptor is equivalent to adaptor(range). > > > template > > > - requires derived_from, _RangeAdaptorClos= ure> > > > + requires __is_range_adaptor_closure<_Self> > > > && __adaptor_invocable<_Self, _Range> > > > - friend constexpr auto > > > + constexpr auto > > > operator|(_Range&& __r, _Self&& __self) > > > { return std::forward<_Self>(__self)(std::forward<_Range>(__r)= ); } > > > > > > // Compose the adaptors __lhs and __rhs into a pipeline, returni= ng > > > // another range adaptor closure object. > > > template > > > - requires derived_from<_Lhs, _RangeAdaptorClosure> > > > - && derived_from<_Rhs, _RangeAdaptorClosure> > > > - friend constexpr auto > > > + 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)}= ; } > > > - }; > > > + } // namespace __closure > > > + > > > + using __closure::_RangeAdaptorClosure; > > > > > > // The base class of every range adaptor non-closure. > > > // > > > @@ -937,7 +952,7 @@ namespace views::__adaptor > > > // A range adaptor closure that represents partial application of > > > // the range adaptor _Adaptor with arguments _Args. > > > template > > > - struct _Partial : _RangeAdaptorClosure > > > + struct _Partial : _RangeAdaptorClosure<_Partial<_Adaptor, _Args.= ..>> > > > { > > > tuple<_Args...> _M_args; > > > > > > @@ -978,7 +993,7 @@ namespace views::__adaptor > > > // A lightweight specialization of the above primary template for > > > // the common case where _Adaptor accepts a single extra argument. > > > template > > > - struct _Partial<_Adaptor, _Arg> : _RangeAdaptorClosure > > > + struct _Partial<_Adaptor, _Arg> : _RangeAdaptorClosure<_Partial<= _Adaptor, _Arg>> > > > { > > > _Arg _M_arg; > > > > > > @@ -1011,7 +1026,7 @@ namespace views::__adaptor > > > template > > > requires __adaptor_has_simple_extra_args<_Adaptor, _Args...> > > > && (is_trivially_copyable_v<_Args> && ...) > > > - struct _Partial<_Adaptor, _Args...> : _RangeAdaptorClosure > > > + struct _Partial<_Adaptor, _Args...> : _RangeAdaptorClosure<_Part= ial<_Adaptor, _Args...>> > > > { > > > tuple<_Args...> _M_args; > > > > > > @@ -1041,7 +1056,7 @@ namespace views::__adaptor > > > template > > > requires __adaptor_has_simple_extra_args<_Adaptor, _Arg> > > > && is_trivially_copyable_v<_Arg> > > > - struct _Partial<_Adaptor, _Arg> : _RangeAdaptorClosure > > > + struct _Partial<_Adaptor, _Arg> : _RangeAdaptorClosure<_Partial<= _Adaptor, _Arg>> > > > { > > > _Arg _M_arg; > > > > > > @@ -1066,7 +1081,7 @@ namespace views::__adaptor > > > // A range adaptor closure that represents composition of the rang= e > > > // adaptor closures _Lhs and _Rhs. > > > template > > > - struct _Pipe : _RangeAdaptorClosure > > > + struct _Pipe : _RangeAdaptorClosure<_Pipe<_Lhs, _Rhs>> > > > { > > > [[no_unique_address]] _Lhs _M_lhs; > > > [[no_unique_address]] _Rhs _M_rhs; > > > @@ -1102,7 +1117,7 @@ namespace views::__adaptor > > > template > > > requires __closure_has_simple_call_op<_Lhs> > > > && __closure_has_simple_call_op<_Rhs> > > > - struct _Pipe<_Lhs, _Rhs> : _RangeAdaptorClosure > > > + struct _Pipe<_Lhs, _Rhs> : _RangeAdaptorClosure<_Pipe<_Lhs, _Rhs= >> > > > { > > > [[no_unique_address]] _Lhs _M_lhs; > > > [[no_unique_address]] _Rhs _M_rhs; > > > @@ -1264,7 +1279,7 @@ namespace views::__adaptor > > > concept __can_owning_view =3D requires { owning_view{std::declv= al<_Range>()}; }; > > > } // namespace __detail > > > > > > - struct _All : __adaptor::_RangeAdaptorClosure > > > + struct _All : __adaptor::_RangeAdaptorClosure<_All> > > > { > > > template > > > static constexpr bool > > > @@ -3048,7 +3063,7 @@ namespace views::__adaptor > > > =3D requires { join_view>{std::declval<_Range>(= )}; }; > > > } // namespace __detail > > > > > > - struct _Join : __adaptor::_RangeAdaptorClosure > > > + struct _Join : __adaptor::_RangeAdaptorClosure<_Join> > > > { > > > template > > > requires __detail::__can_join_view<_Range> > > > @@ -3842,7 +3857,7 @@ namespace views::__adaptor > > > =3D requires { common_view{std::declval<_Range>()}; }; > > > } // namespace __detail > > > > > > - struct _Common : __adaptor::_RangeAdaptorClosure > > > + struct _Common : __adaptor::_RangeAdaptorClosure<_Common> > > > { > > > template > > > requires __detail::__already_common<_Range> > > > @@ -3963,7 +3978,7 @@ namespace views::__adaptor > > > =3D requires { reverse_view{std::declval<_Range>()}; }; > > > } // namespace __detail > > > > > > - struct _Reverse : __adaptor::_RangeAdaptorClosure > > > + struct _Reverse : __adaptor::_RangeAdaptorClosure<_Reverse> > > > { > > > template > > > requires __detail::__is_reverse_view> > > > @@ -4363,7 +4378,7 @@ namespace views::__adaptor > > > } // namespace __detail > > > > > > template > > > - struct _Elements : __adaptor::_RangeAdaptorClosure > > > + struct _Elements : __adaptor::_RangeAdaptorClosure<_Elements<_= Nm>> > > > { > > > template > > > requires __detail::__can_elements_view<_Nm, _Range> > > > @@ -5484,7 +5499,7 @@ namespace views::__adaptor > > > } > > > > > > template > > > - struct _Adjacent : __adaptor::_RangeAdaptorClosure > > > + struct _Adjacent : __adaptor::_RangeAdaptorClosure<_Adjacent<_= Nm>> > > > { > > > template > > > requires (_Nm =3D=3D 0) || __detail::__can_adjacent_view<_Nm,= _Range> > > > @@ -8609,7 +8624,7 @@ namespace views::__adaptor > > > concept __can_as_rvalue_view =3D requires { as_rvalue_view(std:= :declval<_Tp>()); }; > > > } > > > > > > - struct _AsRvalue : __adaptor::_RangeAdaptorClosure > > > + struct _AsRvalue : __adaptor::_RangeAdaptorClosure<_AsRvalue> > > > { > > > template > > > requires __detail::__can_as_rvalue_view<_Range> > > > @@ -8918,7 +8933,7 @@ namespace views::__adaptor > > > =3D requires { enumerate_view>(std::declval<_Tp>()= ); }; > > > } > > > > > > - struct _Enumerate : __adaptor::_RangeAdaptorClosure > > > + struct _Enumerate : __adaptor::_RangeAdaptorClosure<_Enumerate> > > > { > > > template > > > requires __detail::__can_enumerate_view<_Range> > > > @@ -9004,7 +9019,7 @@ namespace views::__adaptor > > > concept __can_as_const_view =3D requires { as_const_view(std::d= eclval<_Range>()); }; > > > } > > > > > > - struct _AsConst : __adaptor::_RangeAdaptorClosure > > > + struct _AsConst : __adaptor::_RangeAdaptorClosure<_AsConst> > > > { > > > template > > > constexpr auto > > > diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/all.cc b/libs= tdc++-v3/testsuite/std/ranges/adaptors/all.cc > > > index 57cb542174b..6bbaf73a20a 100644 > > > --- a/libstdc++-v3/testsuite/std/ranges/adaptors/all.cc > > > +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/all.cc > > > @@ -105,12 +105,6 @@ static_assert(std::is_empty_v > > | views::common > > > | views::keys > > > | views::reverse)>); > > > -#if 0 > > > -// Adding empty range adaptor closure objects to a pipeline used to = not > > > -// increase the size of the pipeline, but now that our range adaptor= closure > > > -// objects derive from a common empty base class, [[no_unique_addres= s]] can no > > > -// longer make two empty adjacent range adaptor closure objects occu= py the same > > > -// data member address. > > > static_assert(sizeof(decltype(views::take(5) | views::drop(5))) > > > =3D=3D sizeof(decltype(views::take(5) > > > | views::join > > > @@ -119,7 +113,6 @@ static_assert(sizeof(decltype(views::take(5) | vi= ews::drop(5))) > > > | views::keys > > > | views::drop(5) > > > | views::reverse))); > > > -#endif > > > > > > template > > > void > > > -- > > > 2.40.0.335.g9857273be0 > > > > > > >