public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r12-4083] libstdc++: Reduce header dependencies for C++20 std::erase [PR92546]
Date: Fri,  1 Oct 2021 19:40:11 +0000 (GMT)	[thread overview]
Message-ID: <20211001194011.DC89D385781B@sourceware.org> (raw)

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

commit r12-4083-gacf3a21cbc26b39b73c0006300f35ff017ddd6cb
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Oct 1 20:37:02 2021 +0100

    libstdc++: Reduce header dependencies for C++20 std::erase [PR92546]
    
    This reduces the preprocessed size of <deque>, <string> and <vector> by
    not including <bits/stl_algo.h> 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:
---
 libstdc++-v3/include/bits/erase_if.h     |  3 ++-
 libstdc++-v3/include/bits/refwrap.h      |  4 +---
 libstdc++-v3/include/bits/stl_algo.h     | 20 --------------------
 libstdc++-v3/include/bits/stl_algobase.h | 20 ++++++++++++++++++++
 libstdc++-v3/include/std/deque           | 20 +++++++++++++-------
 libstdc++-v3/include/std/string          | 20 +++++++++++++-------
 libstdc++-v3/include/std/vector          | 20 +++++++++++++-------
 7 files changed, 62 insertions(+), 45 deletions(-)

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 <bits/c++0x_warning.h>
-#else
+#if __cplusplus >= 201103L
 
 #include <bits/move.h>
 #include <bits/invoke.h>
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<typename _ForwardIterator, typename _Predicate>
-    _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<typename _ForwardIterator, typename _Predicate>
+    _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<typename _ForwardIterator1, typename _ForwardIterator2,
 	   typename _BinaryPredicate>
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 <bits/stl_algobase.h>
-#if __cplusplus > 201703L
-#  include <bits/stl_algo.h> // For remove and remove_if
-#endif // C++20
 #include <bits/allocator.h>
 #include <bits/stl_construct.h>
 #include <bits/stl_uninitialized.h>
 #include <bits/stl_deque.h>
+#include <bits/refwrap.h>
 #include <bits/range_access.h>
 #include <bits/deque.tcc>
 
@@ -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 <bits/stl_function.h> // For less
 #include <ext/numeric_traits.h>
 #include <bits/stl_algobase.h>
-#if __cplusplus > 201703L
-#  include <bits/stl_algo.h> // For remove and remove_if
-#endif // C++20
+#include <bits/refwrap.h>
 #include <bits/range_access.h>
 #include <bits/basic_string.h>
 #include <bits/basic_string.tcc>
@@ -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 <bits/stl_algobase.h>
-#if __cplusplus > 201703L
-#  include <bits/stl_algo.h> // For remove and remove_if
-#endif // C++20
 #include <bits/allocator.h>
 #include <bits/stl_construct.h>
 #include <bits/stl_uninitialized.h>
 #include <bits/stl_vector.h>
 #include <bits/stl_bvector.h>
+#include <bits/refwrap.h>
 #include <bits/range_access.h>
 
 #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


                 reply	other threads:[~2021-10-01 19:40 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=20211001194011.DC89D385781B@sourceware.org \
    --to=redi@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).