public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Simplify allocator usage in unordered containers
@ 2017-08-18 20:04 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2017-08-18 20:04 UTC (permalink / raw)
  To: libstdc++, gcc-patches

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

While fixing PR 81891 I noticed that _Hashtable always creates a
__value_alloc_type for constructing and destroying the elements, which
is unnecessary. https://wg21.link/lwg2218 confirmed that.

We can avoid constructing new allocators just for these calls and can
call them directly on the stored allocator.

	* include/bits/hashtable_policy.h (_ReuseOrAllocNode): Remove
	__value_alloc_type and __value_alloc_traits typedefs.
	(_ReuseOrAllocNode::operator()): Call construct and destroy on the
	node allocator.
	(_Hashtable_alloc): Simplify __value_alloc_traits typedef.
	(_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&...)): Call
	construct on the node allocator.
	(_Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_type*)): Call
	destroy on the node allocator.

Tested powerpc64le-linux, committed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 3935 bytes --]

commit caa7283396a9f74a42c979697beeee0a53115cee
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Aug 18 19:12:00 2017 +0100

    Simplify allocator usage in unordered containers
    
            * include/bits/hashtable_policy.h (_ReuseOrAllocNode): Remove
            __value_alloc_type and __value_alloc_traits typedefs.
            (_ReuseOrAllocNode::operator()): Call construct and destroy on the
            node allocator.
            (_Hashtable_alloc): Simplify __value_alloc_traits typedef.
            (_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&...)): Call
            construct on the node allocator.
            (_Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_type*)): Call
            destroy on the node allocator.

diff --git a/libstdc++-v3/include/bits/hashtable_policy.h b/libstdc++-v3/include/bits/hashtable_policy.h
index a3a31d1fb11..5f2d8776aaa 100644
--- a/libstdc++-v3/include/bits/hashtable_policy.h
+++ b/libstdc++-v3/include/bits/hashtable_policy.h
@@ -111,9 +111,6 @@ namespace __detail
     private:
       using __node_alloc_type = _NodeAlloc;
       using __hashtable_alloc = _Hashtable_alloc<__node_alloc_type>;
-      using __value_alloc_type = typename __hashtable_alloc::__value_alloc_type;
-      using __value_alloc_traits =
-	typename __hashtable_alloc::__value_alloc_traits;
       using __node_alloc_traits =
 	typename __hashtable_alloc::__node_alloc_traits;
       using __node_type = typename __hashtable_alloc::__node_type;
@@ -135,18 +132,17 @@ namespace __detail
 	      __node_type* __node = _M_nodes;
 	      _M_nodes = _M_nodes->_M_next();
 	      __node->_M_nxt = nullptr;
-	      __value_alloc_type __a(_M_h._M_node_allocator());
-	      __value_alloc_traits::destroy(__a, __node->_M_valptr());
+	      auto& __a = _M_h._M_node_allocator();
+	      __node_alloc_traits::destroy(__a, __node->_M_valptr());
 	      __try
 		{
-		  __value_alloc_traits::construct(__a, __node->_M_valptr(),
-						  std::forward<_Arg>(__arg));
+		  __node_alloc_traits::construct(__a, __node->_M_valptr(),
+						 std::forward<_Arg>(__arg));
 		}
 	      __catch(...)
 		{
 		  __node->~__node_type();
-		  __node_alloc_traits::deallocate(_M_h._M_node_allocator(),
-						  __node, 1);
+		  __node_alloc_traits::deallocate(__a, __node, 1);
 		  __throw_exception_again;
 		}
 	      return __node;
@@ -2000,10 +1996,8 @@ namespace __detail
       // Use __gnu_cxx to benefit from _S_always_equal and al.
       using __node_alloc_traits = __gnu_cxx::__alloc_traits<__node_alloc_type>;
 
-      using __value_type = typename __node_type::value_type;
-      using __value_alloc_type =
-	__alloc_rebind<__node_alloc_type, __value_type>;
-      using __value_alloc_traits = std::allocator_traits<__value_alloc_type>;
+      using __value_alloc_traits = typename __node_alloc_traits::template
+	rebind_traits<typename __node_type::value_type>;
 
       using __node_base = __detail::_Hash_node_base;
       using __bucket_type = __node_base*;      
@@ -2057,10 +2051,10 @@ namespace __detail
 	__node_type* __n = std::__addressof(*__nptr);
 	__try
 	  {
-	    __value_alloc_type __a(_M_node_allocator());
 	    ::new ((void*)__n) __node_type;
-	    __value_alloc_traits::construct(__a, __n->_M_valptr(),
-					    std::forward<_Args>(__args)...);
+	    __node_alloc_traits::construct(_M_node_allocator(),
+					   __n->_M_valptr(),
+					   std::forward<_Args>(__args)...);
 	    return __n;
 	  }
 	__catch(...)
@@ -2076,8 +2070,7 @@ namespace __detail
     {
       typedef typename __node_alloc_traits::pointer _Ptr;
       auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__n);
-      __value_alloc_type __a(_M_node_allocator());
-      __value_alloc_traits::destroy(__a, __n->_M_valptr());
+      __node_alloc_traits::destroy(_M_node_allocator(), __n->_M_valptr());
       __n->~__node_type();
       __node_alloc_traits::deallocate(_M_node_allocator(), __ptr, 1);
     }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-08-18 18:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-18 20:04 [PATCH] Simplify allocator usage in unordered containers 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).