[patch] optimize std::list move constructor

Jonathan Wakely jwakely@redhat.com
Thu Aug 7 11:01:00 GMT 2014


While preparing the initial commit for the std::list ABI transition I
noticed the move constructor does unnecessary work, initializing the
object to an empty state, then calling _List_node_base::swap which
checks whether it's empty (but we already know it is) and then
initializes it again. The _List_node_base::swap function is a call
into libstdc++.so so can't be inlined or otherwise optimized.

This patch changes the move constructor to avoid unnecessary checking
and redundant initializations. The code size increases for -Os, but on
my laptop the following silly test goes from 8s to 4.5s with -O3, and
from 10.5s to 8.5s with -Os, so I think it's worthwhile:

  #include <list>

  int main()
  {
    std::list<int> l1;
    for (long i = 0; i < 1000000000; ++i)
    {
      std::list<int> l2 = std::move(l1);
      l1 = std::move(l2);
    }
  }

I intend to commit this to trunk later today.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-patch
Size: 3814 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20140807/8436326b/attachment.bin>


More information about the Libstdc++ mailing list