]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Optimize std::function move constructor [PR101923]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 17 Aug 2021 10:30:56 +0000 (11:30 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 12 Oct 2021 10:45:44 +0000 (11:45 +0100)
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)

libstdc++-v3/include/bits/std_function.h

index 31eba2b822c254771889ccd5b13791f7fa968d2d..da15bd20e37eeb83fa3bb3b90458c17258e2cf1a 100644 (file)
@@ -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