public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Optimize std::function move constructor [PR101923]
@ 2021-08-17 14:22 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2021-08-17 14:22 UTC (permalink / raw)
  To: libstdc++, gcc-patches

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

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.

Tested powerpc64le-linux. Committed to trunk.


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

commit 0808b0df9c4d31f4c362b9c85fb538b6aafcb517
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Aug 17 11:30:56 2021

    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.

diff --git a/libstdc++-v3/include/bits/std_function.h b/libstdc++-v3/include/bits/std_function.h
index c08484465c9..fb86ff1c5f8 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

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

only message in thread, other threads:[~2021-08-17 14:22 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-17 14:22 [committed] libstdc++: Optimize std::function move constructor [PR101923] 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).