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 r11-9111] libstdc++: Optimize std::function move constructor [PR101923]
Date: Tue, 12 Oct 2021 10:59:23 +0000 (GMT)	[thread overview]
Message-ID: <20211012105923.31A4F3857824@sourceware.org> (raw)

https://gcc.gnu.org/g:73b0f810a17a5f529fc8342a2df31276d3538851

commit r11-9111-g73b0f810a17a5f529fc8342a2df31276d3538851
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Aug 17 11:30:56 2021 +0100

    libstdc++: Optimize std::function move constructor [PR101923]
    
    PR 101923 points out that the unconditional swap in the std::function
    move constructor makes it slower than copying an empty std::function.
    The copy constructor has to check for the empty case before doing
    anything, and that makes it very fast for the empty case.
    
    Adding the same check to the move constructor avoids copying the
    _Any_data POD when we don't need to. We can also inline the effects of
    swap, by copying each member and then zeroing the pointer members.
    
    This makes moving an empty object at least as fast as copying an empty
    object.
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/101923
            * include/bits/std_function.h (function(function&&)): Check for
            non-empty parameter before doing any work.
    
    (cherry picked from commit 0808b0df9c4d31f4c362b9c85fb538b6aafcb517)

Diff:
---
 libstdc++-v3/include/bits/std_function.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/bits/std_function.h b/libstdc++-v3/include/bits/std_function.h
index 31eba2b822c..da15bd20e37 100644
--- a/libstdc++-v3/include/bits/std_function.h
+++ b/libstdc++-v3/include/bits/std_function.h
@@ -389,8 +389,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  (if it has one).
        */
       function(function&& __x) noexcept
-      : _Function_base()
-      { __x.swap(*this); }
+      : _Function_base(), _M_invoker(__x._M_invoker)
+      {
+	if (static_cast<bool>(__x))
+	  {
+	    _M_functor = __x._M_functor;
+	    _M_manager = __x._M_manager;
+	    __x._M_manager = nullptr;
+	    __x._M_invoker = nullptr;
+	  }
+      }
 
       /**
        *  @brief Builds a %function that targets a copy of the incoming


                 reply	other threads:[~2021-10-12 10:59 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=20211012105923.31A4F3857824@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).