From patchwork Fri Oct 1 19:38:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1535498 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=IL2l66fR; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HLgTN2NP5z9t14 for ; Sat, 2 Oct 2021 05:41:10 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5472C3857828 for ; Fri, 1 Oct 2021 19:41:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5472C3857828 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1633117267; bh=MbzJGHJ0lU6JOnBtotLPUGz9LtrAKo6h5ipCMKh8KHk=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IL2l66fROImOW22NOPAlEi3s4pP8S7MJPsg8Lpk4GDNUEksD7oInlR1b72NBLRSb2 QcZh7FnBRyH3cd9EvjL4GWX7FO9+8TDfnzOdG4Fs1rVqMmVxbnEuCT1RvNyid1qS29 dxRLgZnGkseHmvw5QQAaxuBYxmQzjUqmUspO7MWA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id C85CF3857821 for ; Fri, 1 Oct 2021 19:38:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C85CF3857821 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-256-fpgN8-z-MwOYc-0bz_N1gQ-1; Fri, 01 Oct 2021 15:38:46 -0400 X-MC-Unique: fpgN8-z-MwOYc-0bz_N1gQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 72EB2802928; Fri, 1 Oct 2021 19:38:45 +0000 (UTC) Received: from localhost (unknown [10.33.36.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0A7F95C1B4; Fri, 1 Oct 2021 19:38:44 +0000 (UTC) Date: Fri, 1 Oct 2021 20:38:44 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Reduce header dependencies for C++20 std::erase [PR92546] Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This reduces the preprocessed size of , and by not including for std::remove and std::remove_if. Also unwrap iterators using __niter_base, to avoid redundant debug mode checks. PR libstdc++/92546 * include/bits/erase_if.h (__erase_nodes_if): Use __niter_base to unwrap debug iterators. * include/bits/refwrap.h: Do not error if included in C++03. * include/bits/stl_algo.h (__remove_if): Move to ... * include/bits/stl_algobase.h (__remove_if): ... here. * include/std/deque (erase, erase_if): Use __remove_if instead of remove and remove_if. * include/std/string (erase, erase_if): Likewise. * include/std/vector (erase, erase_if): Likewise. Tested powerpc64le-linux. Committed to trunk. ff7793bea46 34e9407b3b4 b7e8fb5e482 6ccffeb56b9 e79bde6ada4 e5c093e515c 20751fad19e 9b790acc220 e3869a48fc2 44967af830a dc1b29508d7 59ffa3e3dba d71476c9df9 a09bb4a852f cfb582f6279 c46ecb0112e fb4d55ef61c 10b6d89badd ce709ad3dc0 d335d73889d 681707ec28d 741c7350c08 commit acf3a21cbc26b39b73c0006300f35ff017ddd6cb Author: Jonathan Wakely Date: Fri Oct 1 20:37:02 2021 libstdc++: Reduce header dependencies for C++20 std::erase [PR92546] This reduces the preprocessed size of , and by not including for std::remove and std::remove_if. Also unwrap iterators using __niter_base, to avoid redundant debug mode checks. PR libstdc++/92546 * include/bits/erase_if.h (__erase_nodes_if): Use __niter_base to unwrap debug iterators. * include/bits/refwrap.h: Do not error if included in C++03. * include/bits/stl_algo.h (__remove_if): Move to ... * include/bits/stl_algobase.h (__remove_if): ... here. * include/std/deque (erase, erase_if): Use __remove_if instead of remove and remove_if. * include/std/string (erase, erase_if): Likewise. * include/std/vector (erase, erase_if): Likewise. diff --git a/libstdc++-v3/include/bits/erase_if.h b/libstdc++-v3/include/bits/erase_if.h index 8d1d23168fa..7716e1a953c 100644 --- a/libstdc++-v3/include/bits/erase_if.h +++ b/libstdc++-v3/include/bits/erase_if.h @@ -51,7 +51,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __erase_nodes_if(_Container& __cont, _Predicate __pred) { typename _Container::size_type __num = 0; - for (auto __iter = __cont.begin(), __last = __cont.end(); + for (auto __iter = std::__niter_base(__cont.begin()), + __last = std::__niter_base(__cont.end()); __iter != __last;) { if (__pred(*__iter)) diff --git a/libstdc++-v3/include/bits/refwrap.h b/libstdc++-v3/include/bits/refwrap.h index adfbe214693..a549efbce9a 100644 --- a/libstdc++-v3/include/bits/refwrap.h +++ b/libstdc++-v3/include/bits/refwrap.h @@ -32,9 +32,7 @@ #pragma GCC system_header -#if __cplusplus < 201103L -# include -#else +#if __cplusplus >= 201103L #include #include diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index 90f3162ff90..bc611a95ef4 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -810,26 +810,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } #endif // C++11 - template - _GLIBCXX20_CONSTEXPR - _ForwardIterator - __remove_if(_ForwardIterator __first, _ForwardIterator __last, - _Predicate __pred) - { - __first = std::__find_if(__first, __last, __pred); - if (__first == __last) - return __first; - _ForwardIterator __result = __first; - ++__first; - for (; __first != __last; ++__first) - if (!__pred(__first)) - { - *__result = _GLIBCXX_MOVE(*__first); - ++__result; - } - return __result; - } - /** * @brief Remove elements from a sequence. * @ingroup mutating_algorithms diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index 8627d59b589..0e0586836a6 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -2125,6 +2125,26 @@ _GLIBCXX_END_NAMESPACE_ALGO return __n; } + template + _GLIBCXX20_CONSTEXPR + _ForwardIterator + __remove_if(_ForwardIterator __first, _ForwardIterator __last, + _Predicate __pred) + { + __first = std::__find_if(__first, __last, __pred); + if (__first == __last) + return __first; + _ForwardIterator __result = __first; + ++__first; + for (; __first != __last; ++__first) + if (!__pred(__first)) + { + *__result = _GLIBCXX_MOVE(*__first); + ++__result; + } + return __result; + } + #if __cplusplus >= 201103L template diff --git a/libstdc++-v3/include/std/deque b/libstdc++-v3/include/std/deque index c9a82110ad7..b2a7cee483a 100644 --- a/libstdc++-v3/include/std/deque +++ b/libstdc++-v3/include/std/deque @@ -58,13 +58,11 @@ #pragma GCC system_header #include -#if __cplusplus > 201703L -# include // For remove and remove_if -#endif // C++20 #include #include #include #include +#include #include #include @@ -97,9 +95,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline typename deque<_Tp, _Alloc>::size_type erase_if(deque<_Tp, _Alloc>& __cont, _Predicate __pred) { + using namespace __gnu_cxx; const auto __osz = __cont.size(); - __cont.erase(std::remove_if(__cont.begin(), __cont.end(), __pred), - __cont.end()); + const auto __end = __cont.end(); + auto __removed(std::__remove_if(std::__niter_base(__cont.begin()), + std::__niter_base(__end), + __ops::__pred_iter(std::ref(__pred)))); + __cont.erase(std::__niter_wrap(__end, __removed), __end); return __osz - __cont.size(); } @@ -107,9 +109,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline typename deque<_Tp, _Alloc>::size_type erase(deque<_Tp, _Alloc>& __cont, const _Up& __value) { + using namespace __gnu_cxx; const auto __osz = __cont.size(); - __cont.erase(std::remove(__cont.begin(), __cont.end(), __value), - __cont.end()); + const auto __end = __cont.end(); + auto __removed(std::__remove_if(std::__niter_base(__cont.begin()), + std::__niter_base(__end), + __ops::__iter_equals_val(__value))); + __cont.erase(std::__niter_wrap(__end, __removed), __end); return __osz - __cont.size(); } _GLIBCXX_END_NAMESPACE_VERSION diff --git a/libstdc++-v3/include/std/string b/libstdc++-v3/include/std/string index 0147e48d47a..7f994147a33 100644 --- a/libstdc++-v3/include/std/string +++ b/libstdc++-v3/include/std/string @@ -48,9 +48,7 @@ #include // For less #include #include -#if __cplusplus > 201703L -# include // For remove and remove_if -#endif // C++20 +#include #include #include #include @@ -125,9 +123,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline typename basic_string<_CharT, _Traits, _Alloc>::size_type erase_if(basic_string<_CharT, _Traits, _Alloc>& __cont, _Predicate __pred) { + using namespace __gnu_cxx; + using _It = typename basic_string<_CharT, _Traits, _Alloc>::iterator; const auto __osz = __cont.size(); - __cont.erase(std::remove_if(__cont.begin(), __cont.end(), __pred), - __cont.end()); + _It __removed(std::__remove_if(std::__niter_base(__cont.begin()), + std::__niter_base(__cont.end()), + __ops::__pred_iter(std::ref(__pred)))); + __cont.erase(__removed, __cont.end()); return __osz - __cont.size(); } @@ -135,9 +137,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline typename basic_string<_CharT, _Traits, _Alloc>::size_type erase(basic_string<_CharT, _Traits, _Alloc>& __cont, const _Up& __value) { + using namespace __gnu_cxx; + using _It = typename basic_string<_CharT, _Traits, _Alloc>::iterator; const auto __osz = __cont.size(); - __cont.erase(std::remove(__cont.begin(), __cont.end(), __value), - __cont.end()); + _It __removed(std::__remove_if(std::__niter_base(__cont.begin()), + std::__niter_base(__cont.end()), + __ops::__iter_equals_val(__value))); + __cont.erase(__removed, __cont.end()); return __osz - __cont.size(); } _GLIBCXX_END_NAMESPACE_VERSION diff --git a/libstdc++-v3/include/std/vector b/libstdc++-v3/include/std/vector index f804f4078ea..3b275e249fc 100644 --- a/libstdc++-v3/include/std/vector +++ b/libstdc++-v3/include/std/vector @@ -58,14 +58,12 @@ #pragma GCC system_header #include -#if __cplusplus > 201703L -# include // For remove and remove_if -#endif // C++20 #include #include #include #include #include +#include #include #ifndef _GLIBCXX_EXPORT_TEMPLATE @@ -107,9 +105,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline typename vector<_Tp, _Alloc>::size_type erase_if(vector<_Tp, _Alloc>& __cont, _Predicate __pred) { + using namespace __gnu_cxx; const auto __osz = __cont.size(); - __cont.erase(std::remove_if(__cont.begin(), __cont.end(), __pred), - __cont.end()); + const auto __end = __cont.end(); + auto __removed(std::__remove_if(std::__niter_base(__cont.begin()), + std::__niter_base(__end), + __ops::__pred_iter(std::ref(__pred)))); + __cont.erase(std::__niter_wrap(__end, __removed), __end); return __osz - __cont.size(); } @@ -117,9 +119,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline typename vector<_Tp, _Alloc>::size_type erase(vector<_Tp, _Alloc>& __cont, const _Up& __value) { + using namespace __gnu_cxx; const auto __osz = __cont.size(); - __cont.erase(std::remove(__cont.begin(), __cont.end(), __value), - __cont.end()); + const auto __end = __cont.end(); + auto __removed(std::__remove_if(std::__niter_base(__cont.begin()), + std::__niter_base(__end), + __ops::__iter_equals_val(__value))); + __cont.erase(std::__niter_wrap(__end, __removed), __end); return __osz - __cont.size(); } _GLIBCXX_END_NAMESPACE_VERSION