public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* std::forward_list optim for always equal allocator
@ 2017-07-17 20:10 François Dumont
  2017-07-17 20:14 ` Daniel Krügler
  2017-08-28 20:39 ` François Dumont
  0 siblings, 2 replies; 13+ messages in thread
From: François Dumont @ 2017-07-17 20:10 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1067 bytes --]

Hi

     Here is the patch to implement the always equal alloc optimization 
for forward_list. With this version there is no abi issue.

     I also prefer to implement the _Fwd_list_node_base move operator 
for consistency with the move constructor and used it where applicable.


     * include/bits/forward_list.h
     (_Fwd_list_node_base& operator=(_Fwd_list_node_base&&)): Implement.
     (_Fwd_list_impl(_Fwd_list_impl&&, _Node_alloc_type&&)): New.
     (_Fwd_list_base(_Fwd_list_base&&, _Node_alloc_type&&, std::true_type)):
     New, use latter.
     (forward_list(forward_list&&, _Node_alloc_type&&, std::false_type)):
     New.
     (forward_list(forward_list&&, _Node_alloc_type&&, std::true_type)):
     New.
     (forward_list(forward_list&&, const _Alloc&)): Adapt to use latters.
     * include/bits/forward_list.tcc
     (_Fwd_list_base(_Fwd_list_base&&, _Node_alloc_type&&)): Adapt to use
     _M_impl._M_head move assignment.
     (forward_list<>::merge(forward_list<>&&, _Comp)): Likewise.

Tested under Linux x86_64, ok to commit ?

François


[-- Attachment #2: forward_list.patch --]
[-- Type: text/x-patch, Size: 7763 bytes --]

diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h
index 9ddbcb2..dec91ea 100644
--- a/libstdc++-v3/include/bits/forward_list.h
+++ b/libstdc++-v3/include/bits/forward_list.h
@@ -60,7 +60,14 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
     _Fwd_list_node_base(const _Fwd_list_node_base&) = delete;
     _Fwd_list_node_base& operator=(const _Fwd_list_node_base&) = delete;
-    _Fwd_list_node_base& operator=(_Fwd_list_node_base&&) = delete;
+
+    _Fwd_list_node_base&
+    operator=(_Fwd_list_node_base&& __x)
+    {
+      _M_next = __x._M_next;
+      __x._M_next = nullptr;
+      return *this;
+    }
 
     _Fwd_list_node_base* _M_next = nullptr;
 
@@ -75,7 +82,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	  __end->_M_next = _M_next;
 	}
       else
-	__begin->_M_next = 0;
+	__begin->_M_next = nullptr;
       _M_next = __keep;
       return __end;
     }
@@ -180,7 +187,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	if (_M_node)
 	  return _Fwd_list_iterator(_M_node->_M_next);
 	else
-	  return _Fwd_list_iterator(0);
+	  return _Fwd_list_iterator(nullptr);
       }
 
       _Fwd_list_node_base* _M_node;
@@ -251,7 +258,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	if (this->_M_node)
 	  return _Fwd_list_const_iterator(_M_node->_M_next);
 	else
-	  return _Fwd_list_const_iterator(0);
+	  return _Fwd_list_const_iterator(nullptr);
       }
 
       const _Fwd_list_node_base* _M_node;
@@ -298,6 +305,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
 	_Fwd_list_impl(_Fwd_list_impl&&) = default;
 
+	_Fwd_list_impl(_Fwd_list_impl&& __fl, _Node_alloc_type&& __a)
+	: _Node_alloc_type(std::move(__a)), _M_head(std::move(__fl._M_head))
+	{ }
+
 	_Fwd_list_impl(_Node_alloc_type&& __a)
 	: _Node_alloc_type(std::move(__a))
 	{ }
@@ -323,15 +334,21 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       _Fwd_list_base(_Node_alloc_type&& __a)
       : _M_impl(std::move(__a)) { }
 
+      // When allocators are always equal.
+      _Fwd_list_base(_Fwd_list_base&& __lst, _Node_alloc_type&& __a,
+		     std::true_type)
+      : _M_impl(std::move(__lst._M_impl), std::move(__a))
+      { }
+
+      // When allocators are not always equal.
       _Fwd_list_base(_Fwd_list_base&& __lst, _Node_alloc_type&& __a);
 
       _Fwd_list_base(_Fwd_list_base&&) = default;
 
       ~_Fwd_list_base()
-      { _M_erase_after(&_M_impl._M_head, 0); }
+      { _M_erase_after(&_M_impl._M_head, nullptr); }
 
     protected:
-
       _Node*
       _M_get_node()
       {
@@ -448,7 +465,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       : _Base(_Node_alloc_type(__al))
       { }
 
-
       /**
        *  @brief  Copy constructor with allocator argument.
        *  @param  __list  Input list to copy.
@@ -458,14 +474,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       : _Base(_Node_alloc_type(__al))
       { _M_range_initialize(__list.begin(), __list.end()); }
 
-      /**
-       *  @brief  Move constructor with allocator argument.
-       *  @param  __list  Input list to move.
-       *  @param  __al    An allocator object.
-       */
-      forward_list(forward_list&& __list, const _Alloc& __al)
-      noexcept(_Node_alloc_traits::_S_always_equal())
-      : _Base(std::move(__list), _Node_alloc_type(__al))
+    private:
+      forward_list(forward_list&& __list, _Node_alloc_type&& __al,
+		   std::false_type)
+      : _Base(std::move(__list), std::move(__al))
       {
 	// If __list is not empty it means its allocator is not equal to __a,
 	// so we need to move from each element individually.
@@ -474,6 +486,24 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 		     std::__make_move_if_noexcept_iterator(__list.end()));
       }
 
+      forward_list(forward_list&& __list, _Node_alloc_type&& __al,
+		   std::true_type)
+      noexcept
+      : _Base(std::move(__list), _Node_alloc_type(__al), std::true_type{})
+      { }
+
+    public:
+      /**
+       *  @brief  Move constructor with allocator argument.
+       *  @param  __list  Input list to move.
+       *  @param  __al    An allocator object.
+       */
+      forward_list(forward_list&& __list, const _Alloc& __al)
+      noexcept(_Node_alloc_traits::_S_always_equal())
+      : forward_list(std::move(__list), _Node_alloc_type(__al),
+		     typename _Node_alloc_traits::is_always_equal{})
+      { }
+
       /**
        *  @brief  Creates a %forward_list with default constructed elements.
        *  @param  __n   The number of elements to initially create.
@@ -702,7 +732,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        */
       iterator
       end() noexcept
-      { return iterator(0); }
+      { return iterator(nullptr); }
 
       /**
        *  Returns a read-only iterator that points one past the last
@@ -711,7 +741,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        */
       const_iterator
       end() const noexcept
-      { return const_iterator(0); }
+      { return const_iterator(nullptr); }
 
       /**
        *  Returns a read-only (constant) iterator that points to the
@@ -738,7 +768,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        */
       const_iterator
       cend() const noexcept
-      { return const_iterator(0); }
+      { return const_iterator(nullptr); }
 
       /**
        *  Returns true if the %forward_list is empty.  (Thus begin() would
@@ -746,7 +776,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        */
       bool
       empty() const noexcept
-      { return this->_M_impl._M_head._M_next == 0; }
+      { return this->_M_impl._M_head._M_next == nullptr; }
 
       /**
        *  Returns the largest possible number of elements of %forward_list.
@@ -1051,7 +1081,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        */
       void
       clear() noexcept
-      { this->_M_erase_after(&this->_M_impl._M_head, 0); }
+      { this->_M_erase_after(&this->_M_impl._M_head, nullptr); }
 
       // 23.3.4.6 forward_list operations:
 
diff --git a/libstdc++-v3/include/bits/forward_list.tcc b/libstdc++-v3/include/bits/forward_list.tcc
index b7c906e..f13e959 100644
--- a/libstdc++-v3/include/bits/forward_list.tcc
+++ b/libstdc++-v3/include/bits/forward_list.tcc
@@ -41,10 +41,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     : _M_impl(std::move(__a))
     {
       if (__lst._M_get_Node_allocator() == _M_get_Node_allocator())
-	{
-	  this->_M_impl._M_head._M_next = __lst._M_impl._M_head._M_next;
-	  __lst._M_impl._M_head._M_next = 0;
-	}
+	this->_M_impl._M_head = std::move(__lst._M_impl._M_head);
     }
 
   template<typename _Tp, typename _Alloc>
@@ -362,11 +359,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 					__list._M_impl._M_head._M_next);
 	    __node = __node->_M_next;
 	  }
+
 	if (__list._M_impl._M_head._M_next)
-	  {
-	    __node->_M_next = __list._M_impl._M_head._M_next;
-	    __list._M_impl._M_head._M_next = 0;
-	  }
+	  *__node = std::move(__list._M_impl._M_head);
       }
 
   template<typename _Tp, typename _Alloc>
@@ -397,7 +392,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       forward_list<_Tp, _Alloc>::
       sort(_Comp __comp)
       {
-	// If `next' is 0, return immediately.
+	// If `next' is nullptr, return immediately.
 	_Node* __list = static_cast<_Node*>(this->_M_impl._M_head._M_next);
 	if (!__list)
 	  return;
@@ -407,8 +402,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	while (1)
 	  {
 	    _Node* __p = __list;
-	    __list = 0;
-	    _Node* __tail = 0;
+	    __list = nullptr;
+	    _Node* __tail = nullptr;
 
 	    // Count number of merges we do in this pass.
 	    unsigned long __nmerges = 0;
@@ -476,7 +471,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 		// Now p has stepped `insize' places along, and q has too.
 		__p = __q;
 	      }
-	    __tail->_M_next = 0;
+	    __tail->_M_next = nullptr;
 
 	    // If we have done only one merge, we're finished.
 	    // Allow for nmerges == 0, the empty list case.


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2018-01-08 13:53 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-17 20:10 std::forward_list optim for always equal allocator François Dumont
2017-07-17 20:14 ` Daniel Krügler
2017-08-28 20:39 ` François Dumont
2017-09-08 16:19   ` Jonathan Wakely
2017-09-11  5:12     ` François Dumont
2017-09-11  5:44       ` Daniel Krügler
2017-09-11 12:11         ` Jonathan Wakely
2017-09-11 20:36           ` François Dumont
2017-09-11 20:39             ` Daniel Krügler
2017-09-11 22:10               ` Jonathan Wakely
2017-09-12 20:41                 ` François Dumont
2017-11-23 21:49   ` François Dumont
2018-01-08 14:07     ` Jonathan Wakely

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).