From patchwork Wed Oct 29 18:40:54 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 404761 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 9C9DF14007B for ; Thu, 30 Oct 2014 05:41:15 +1100 (AEDT) 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=B4JKwpqOxsB6Rqjokb25zj/ixptuWNQ57vc5Q40iiPSGyqubRzpAY +JXyGSIWlnO7Xaq5nObE6IdM6fXtPE68KMHZKvXXDIy1+XbORa0PzVT3CKlr3Y97 zUKPmrI2sgdZQ479mkg0e6Ps3FCJpoYzEgZpqoW3npK6jFArtCb8CA= 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=Ry0O4NjUKPox28JP+nxL6VONysY=; b=P3SMjf8/Jzay+7cL6YNe k/5wNasob9BeuSxcf6/1Xg+fGj3QOSvlAV2SOKQAfraGl6QqF8GOwmQU/uphCjcQ fr9zKGZzryD3ZuF7JhaJ8pJsvFH02wGCxUvg+j5qPwiBlXJ5ZfzNMhPaxKdwRpOv hywnDEtXZrxdnz7YBYUcM38= Received: (qmail 19210 invoked by alias); 29 Oct 2014 18:40:59 -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 19192 invoked by uid 89); 29 Oct 2014 18:40:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Wed, 29 Oct 2014 18:40:57 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s9TIeuLq002586 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 29 Oct 2014 14:40:56 -0400 Received: from localhost (ovpn-116-112.ams2.redhat.com [10.36.116.112]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s9TIetZZ026435; Wed, 29 Oct 2014 14:40:55 -0400 Date: Wed, 29 Oct 2014 18:40:54 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [patch] Use perfect forwarding in std::function's invokers Message-ID: <20141029184054.GH3033@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) As pointed out in http://stackoverflow.com/q/26543242/981959 our std::function does more copying/moving than necessary. The solution is to use perfect forwarding for the internal invoker functions, even if function Date: Wed Oct 29 18:04:16 2014 +0000 Use perfect forwarding in std::function invokers. * include/std/functional: (_Function_base::_Function_base()): Use nullptr instead of literal zero. (function::operator=(nullptr_t)): Likewise. (_Function_handler::_M_invoke): Use perfect forwarding for _ArgTypes. (function::_Invoker_type): Likewise. * testsuite/20_util/function/invoke/forwarding.cc: New. diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index bed1eea..5bc2730 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -1990,7 +1990,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) } }; - _Function_base() : _M_manager(0) { } + _Function_base() : _M_manager(nullptr) { } ~_Function_base() { @@ -2019,7 +2019,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) public: static _Res - _M_invoke(const _Any_data& __functor, _ArgTypes... __args) + _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { return (*_Base::_M_get_pointer(__functor))( std::forward<_ArgTypes>(__args)...); @@ -2034,7 +2034,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) public: static void - _M_invoke(const _Any_data& __functor, _ArgTypes... __args) + _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { (*_Base::_M_get_pointer(__functor))( std::forward<_ArgTypes>(__args)...); @@ -2049,7 +2049,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) public: static _Res - _M_invoke(const _Any_data& __functor, _ArgTypes... __args) + _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { return __callable_functor(**_Base::_M_get_pointer(__functor))( std::forward<_ArgTypes>(__args)...); @@ -2064,7 +2064,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) public: static void - _M_invoke(const _Any_data& __functor, _ArgTypes... __args) + _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { __callable_functor(**_Base::_M_get_pointer(__functor))( std::forward<_ArgTypes>(__args)...); @@ -2081,7 +2081,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) public: static _Res - _M_invoke(const _Any_data& __functor, _ArgTypes... __args) + _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)( std::forward<_ArgTypes>(__args)...); @@ -2121,7 +2121,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) } static void - _M_invoke(const _Any_data& __functor, _ArgTypes... __args) + _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) { std::mem_fn(_Base::_M_get_pointer(__functor)->__value)( std::forward<_ArgTypes>(__args)...); @@ -2275,8 +2275,8 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) if (_M_manager) { _M_manager(_M_functor, _M_functor, __destroy_functor); - _M_manager = 0; - _M_invoker = 0; + _M_manager = nullptr; + _M_invoker = nullptr; } return *this; } @@ -2395,7 +2395,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) #endif private: - typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...); + using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...); _Invoker_type _M_invoker; }; diff --git a/libstdc++-v3/testsuite/20_util/function/invoke/forwarding.cc b/libstdc++-v3/testsuite/20_util/function/invoke/forwarding.cc new file mode 100644 index 0000000..b6e1b47 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/function/invoke/forwarding.cc @@ -0,0 +1,56 @@ +// Copyright (C) 2014 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++11" } + +#include +#include + +struct Counted +{ + Counted() : count(0) { } + Counted(const Counted& c) : count(c.count + 1) { } + int count; +}; + +int func(Counted c) { return c.count; } + +std::function f = func; + +void +test01() +{ + Counted c; + int n = f(c); + // 1 copy invoking function::operator() and 1 copy invoking func + VERIFY( n == 2 ); +} + +void +test02() +{ + int n = f(Counted{}); + // copy elided when invoking function::operator(), 1 copy invoking func + VERIFY( n == 1 ); +} + +int +main() +{ + test01(); + test02(); +}