From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 7D5573858C83 for ; Tue, 4 Oct 2022 14:09:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7D5573858C83 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1664892543; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=bBQ9R+kbjMf0p1E9M4h6+CRWrkH93msi6cFImBees8g=; b=KgD+ouqDpmU6B8Pumv1rN+4ru0KQVmaicRbUyo0GJphIMNNMKi3Ivuw+5ASO6X0bOcZkCS grTcru+2SoTaO/X1fEEglQ+Rnc7KukNS4gyyUZTFuRXcTFBlHmSddRY3tAJWQk1OQwodzP hC8L/M1LnimWtw3TW5AUZYgR0L0i+Ws= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-624-Fs2kgueZOcCK2phjYjZ3rw-1; Tue, 04 Oct 2022 10:09:00 -0400 X-MC-Unique: Fs2kgueZOcCK2phjYjZ3rw-1 Received: by mail-qt1-f198.google.com with SMTP id e8-20020ac85988000000b0035c39dd5eb9so9383486qte.16 for ; Tue, 04 Oct 2022 07:09:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bBQ9R+kbjMf0p1E9M4h6+CRWrkH93msi6cFImBees8g=; b=2PDKpv32tBkMj6zYDJNkbj9DCNPGAiT1gCdgcxJmLaUqL0VFgXDXECg5n0CAv4Q3fI lK+VGACJsUIg4xfGip+nf+FvaYa1OqzilelVYIhUqV5BSUcTTOYRiDExEOBvILlHk8c3 KbpH9m44azw96IljCTfteu+PmG9hb8kzPeQyNZr3bjmObVnn3cFthxecm8SnUyZqleDm jcAHl2j2pxypKAPgOUVTFx36gLtZzIgPRiu23aUV98CRFqORgVRXuikJznGkdYmT4KEM 5fSdOhL7r8PuCoCKWgkReTfn3RYpW6MBO5CmMbYbzSymJafXW/LqIuWDR4FbEQUUGV0E UvZQ== X-Gm-Message-State: ACrzQf1RVXZjCVk8qRR6a+HH+HR8vvL0uNMqnAl27dm0WKOVhog+LRWO gQG7bwUerOGJvI5vf70FeapzcH0OeiYQdzpBaOa316yn8abJT+8/iCeFVXPQPZN73Qp7VsEcGsi duTvsqUVJ0IxLVBY= X-Received: by 2002:ae9:ed46:0:b0:6e0:156c:f74c with SMTP id c67-20020ae9ed46000000b006e0156cf74cmr1302001qkg.405.1664892539808; Tue, 04 Oct 2022 07:08:59 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6MycALD6f3L26oL8YHpeqPd5b6WUOXSo/yu6w01FJsowUxSQJPmGvdEm3T+rLlbOAIzphNBg== X-Received: by 2002:ae9:ed46:0:b0:6e0:156c:f74c with SMTP id c67-20020ae9ed46000000b006e0156cf74cmr1301971qkg.405.1664892539513; Tue, 04 Oct 2022 07:08:59 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id y20-20020ac87c94000000b0038cdc487886sm459487qtv.80.2022.10.04.07.08.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Oct 2022 07:08:58 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Tue, 4 Oct 2022 10:08:57 -0400 (EDT) To: Jonathan Wakely cc: Patrick Palka , gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: Re: [PATCH] libstdc++: Implement ranges::join_with_view from P2441R2 In-Reply-To: Message-ID: <0c8cf283-5dc4-96a4-1bd7-7aa1ab71656f@idea> References: <20221004011115.2009591-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_NUMSUBJECT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable 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 Tue, 4 Oct 2022, Jonathan Wakely wrote: > On Tue, 4 Oct 2022 at 02:11, Patrick Palka via Libstdc++ > wrote: > > > > Tested on x86_64-pc-linux-gnu, does this look OK for trunk? FWIW using > > OK, thanks. Thanks a lot, patch committed. > > > variant<_PatternIter, _InnerIter> in the implementation means we need to > > include from , which increases the preprocessed size > > of by 3% (51.5k vs 53k). I suppose that's an acceptable cost? > > Yeah, I don't think we want to reimplement a lightweight std::variant, > because that would just add even more code. Sounds good. > > As I mentioned on IRC, maybe we could optimize the compilation time > for some of the visitation using P2637R0, but that can be done later. Ah, I didn't consider the compile time impact of using std::visit. Since we already use/instantiate std::get elsewhere in the implementation, what do you think about doing the visitation manually via index() and std::get like so? Seems to reduce compile time/memory usage for join_with/1.cc by around 6% and doesn't look too messy since we're dealing with only two alternatives. (And IIUC this should be equivalent to std::visit wrt valueless_by_exception handling, since the call to std::get<1> in each else branch will throw bad_variant_access for us like std::visit would.) -- >8 -- Subject: [PATCH] libstdc++: Avoid std::visit in ranges::join_with_view libstdc++-v3/ChangeLog: * include/std/ranges (join_with_view::_Iterator::operator*): Replace use of std::visit with manual visitation. (join_with_view::_Iterator::operator++): Likewise. (join_with_view::_Iterator::operator--): Likewise. (join_with_view::_Iterator::iter_move): Likewise. (join_with_view::_Iterator::iter_swap): Likewise. --- libstdc++-v3/include/std/ranges | 47 +++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index d0d6ce61a87..1f821128d2d 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -7165,18 +7165,23 @@ namespace views::__adaptor _M_inner_it.template emplace<1>(std::get<1>(std::move(__i._M_inner_it))); } - constexpr decltype(auto) + constexpr common_reference_t, + iter_reference_t<_PatternIter>> operator*() const { - using reference = common_reference_t, - iter_reference_t<_PatternIter>>; - return std::visit([](auto& __it) -> reference { return *__it; }, _M_inner_it); + if (_M_inner_it.index() == 0) + return *std::get<0>(_M_inner_it); + else + return *std::get<1>(_M_inner_it); } constexpr _Iterator& operator++() { - std::visit([](auto& __it){ ++__it; }, _M_inner_it); + if (_M_inner_it.index() == 0) + ++std::get<0>(_M_inner_it); + else + ++std::get<1>(_M_inner_it); _M_satisfy(); return *this; } @@ -7232,7 +7237,10 @@ namespace views::__adaptor } } - std::visit([](auto& __it){ --__it; }, _M_inner_it); + if (_M_inner_it.index() == 0) + --std::get<0>(_M_inner_it); + else + --std::get<1>(_M_inner_it); return *this; } @@ -7253,18 +7261,35 @@ namespace views::__adaptor && equality_comparable<_OuterIter> && equality_comparable<_InnerIter> { return __x._M_outer_it == __y._M_outer_it && __x._M_inner_it ==__y._M_inner_it; } - friend constexpr decltype(auto) + friend constexpr common_reference_t, + iter_rvalue_reference_t<_PatternIter>> iter_move(const _Iterator& __x) { - using __rval_ref = common_reference_t, - iter_rvalue_reference_t<_PatternIter>>; - return std::visit<__rval_ref>(ranges::iter_move, __x._M_inner_it); + if (__x._M_inner_it.index() == 0) + return ranges::iter_move(std::get<0>(__x._M_inner_it)); + else + return ranges::iter_move(std::get<1>(__x._M_inner_it)); } friend constexpr void iter_swap(const _Iterator& __x, const _Iterator& __y) requires indirectly_swappable<_InnerIter, _PatternIter> - { std::visit(ranges::iter_swap, __x._M_inner_it, __y._M_inner_it); } + { + if (__x._M_inner_it.index() == 0) + { + if (__y._M_inner_it.index() == 0) + ranges::iter_swap(std::get<0>(__x._M_inner_it), std::get<0>(__y._M_inner_it)); + else + ranges::iter_swap(std::get<0>(__x._M_inner_it), std::get<1>(__y._M_inner_it)); + } + else + { + if (__y._M_inner_it.index() == 0) + ranges::iter_swap(std::get<1>(__x._M_inner_it), std::get<0>(__y._M_inner_it)); + else + ranges::iter_swap(std::get<1>(__x._M_inner_it), std::get<1>(__y._M_inner_it)); + } + } }; template -- 2.38.0.rc2