From patchwork Wed Jun 13 15:13:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 928916 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-479641-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="yF/zlzhf"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 415Vdn386tz9ry1 for ; Thu, 14 Jun 2018 01:14:04 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=MpiM8FT948Men2Siha2QaCdBpRpmDXjJgdFRiHncIeXVIWWwuryyC BwUrvnIlqCEELVoU3HZC/pmYiB/RApXADPb/UqGaiHFk2Gcs7IXccndAkUp+H2KO SxCar+VvVXBn5ozJnziWno2sbLcFLedU9pSEncxhQrkrtvaAo5lWB8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=1OElW43F/0JW3ncTdjxj54sNm0Q=; b=yF/zlzhfeoDrEbLlzLIM /uZJMbNH/Nk9I641y4RerLnNuXgL/bW4oW8VZ5ywh9IkPC32V7W2cn6I3bkHVi9y 6fBymEOqc614hvZBOlss1olzSS//ZO/NqKKaJ6FadUQ71i/Fc54lHAwdy8RwcIsm WTC6Q6FJtHG5Fjpe4JqqJZ8= Received: (qmail 68010 invoked by alias); 13 Jun 2018 15:13:56 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 67992 invoked by uid 89); 13 Jun 2018 15:13:56 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 13 Jun 2018 15:13:54 +0000 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 190D07D84D; Wed, 13 Jun 2018 15:13:53 +0000 (UTC) Received: from localhost (unknown [10.33.36.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id C714E1116706; Wed, 13 Jun 2018 15:13:52 +0000 (UTC) Date: Wed, 13 Jun 2018 16:13:52 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] PR libstdc++/86127 avoid unnecessary allocator conversions Message-ID: <20180613151352.GA8928@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.2 (2017-12-15) There is no need to use an allocator of the correct value_type when calling allocator_traits::construct and allocator_traits::destroy. The existing node allocator can be used, instead of constructing a new allocator object every time. There's also no benefit to using __gnu_cxx::__alloc_traits instead of std::allocator_traits to get the pointer and const_pointer types. std::forward_list is only available for C++11 and later, when std::allocator_traits is available too. PR libstdc++/86127 * include/bits/forward_list.h (_Fwd_list_base::_Tp_alloc_type): Remove unused typedef. (_Fwd_list_base::_Node_alloc_traits): Use allocator_traits instead of __gnu_cxx::__alloc_traits. (_Fwd_list_base::_M_create_node, _Fwd_list_base::_M_erase_after): Use node allocator to create and destroy elements. (forward_list::_Tp_alloc_type): Remove unused typedef. (forward_list::_Alloc_traits): Use allocator_traits instead of __gnu_cxx::__alloc_traits. Tested powerpc64le-linux, committed to trunk. commit 777e063c0bb527a72539169ea25f06d7ad3de536 Author: Jonathan Wakely Date: Wed Jun 13 13:54:47 2018 +0100 PR libstdc++/86127 avoid unnecessary allocator conversions There is no need to use an allocator of the correct value_type when calling allocator_traits::construct and allocator_traits::destroy. The existing node allocator can be used, instead of constructing a new allocator object every time. There's also no benefit to using __gnu_cxx::__alloc_traits instead of std::allocator_traits to get the pointer and const_pointer types. std::forward_list is only available for C++11 and later, when std::allocator_traits is available too. PR libstdc++/86127 * include/bits/forward_list.h (_Fwd_list_base::_Tp_alloc_type): Remove unused typedef. (_Fwd_list_base::_Node_alloc_traits): Use allocator_traits instead of __gnu_cxx::__alloc_traits. (_Fwd_list_base::_M_create_node, _Fwd_list_base::_M_erase_after): Use node allocator to create and destroy elements. (forward_list::_Tp_alloc_type): Remove unused typedef. (forward_list::_Alloc_traits): Use allocator_traits instead of __gnu_cxx::__alloc_traits. diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h index b40256bd104..8c4c074e454 100644 --- a/libstdc++-v3/include/bits/forward_list.h +++ b/libstdc++-v3/include/bits/forward_list.h @@ -289,7 +289,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER struct _Fwd_list_base { protected: - typedef __alloc_rebind<_Alloc, _Tp> _Tp_alloc_type; typedef __alloc_rebind<_Alloc, _Fwd_list_node<_Tp>> _Node_alloc_type; typedef __gnu_cxx::__alloc_traits<_Node_alloc_type> _Node_alloc_traits; @@ -363,11 +362,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _Node* __node = this->_M_get_node(); __try { - _Tp_alloc_type __a(_M_get_Node_allocator()); - typedef allocator_traits<_Tp_alloc_type> _Alloc_traits; ::new ((void*)__node) _Node; - _Alloc_traits::construct(__a, __node->_M_valptr(), - std::forward<_Args>(__args)...); + _Node_alloc_traits::construct(_M_get_Node_allocator(), + __node->_M_valptr(), + std::forward<_Args>(__args)...); } __catch(...) { @@ -437,10 +435,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER typedef _Fwd_list_base<_Tp, _Alloc> _Base; typedef _Fwd_list_node<_Tp> _Node; typedef _Fwd_list_node_base _Node_base; - typedef typename _Base::_Tp_alloc_type _Tp_alloc_type; typedef typename _Base::_Node_alloc_type _Node_alloc_type; typedef typename _Base::_Node_alloc_traits _Node_alloc_traits; - typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits; + typedef allocator_traits<__alloc_rebind<_Alloc, _Tp>> _Alloc_traits; public: // types: diff --git a/libstdc++-v3/include/bits/forward_list.tcc b/libstdc++-v3/include/bits/forward_list.tcc index 3e12cd531d4..b41fbbb52f2 100644 --- a/libstdc++-v3/include/bits/forward_list.tcc +++ b/libstdc++-v3/include/bits/forward_list.tcc @@ -65,8 +65,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { _Node* __curr = static_cast<_Node*>(__pos->_M_next); __pos->_M_next = __curr->_M_next; - _Tp_alloc_type __a(_M_get_Node_allocator()); - allocator_traits<_Tp_alloc_type>::destroy(__a, __curr->_M_valptr()); + _Node_alloc_traits::destroy(_M_get_Node_allocator(), + __curr->_M_valptr()); __curr->~_Node(); _M_put_node(__curr); return __pos->_M_next; @@ -83,8 +83,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { _Node* __temp = __curr; __curr = static_cast<_Node*>(__curr->_M_next); - _Tp_alloc_type __a(_M_get_Node_allocator()); - allocator_traits<_Tp_alloc_type>::destroy(__a, __temp->_M_valptr()); + _Node_alloc_traits::destroy(_M_get_Node_allocator(), + __temp->_M_valptr()); __temp->~_Node(); _M_put_node(__temp); }