From b5e320cbfa5f184911335c7b49a073ee0d15ea01 Mon Sep 17 00:00:00 2001 From: glisse Date: Thu, 26 Jul 2018 12:01:14 +0000 Subject: [PATCH] optimize std::vector move assignment 2018-07-26 Marc Glisse * include/bits/stl_vector.h (_Vector_impl_data::_M_copy_data): New. (_Vector_impl_data::_M_swap_data): Use _M_copy_data. (vector::_M_move_assign): Reorder the swaps. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@262998 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 6 ++++++ libstdc++-v3/include/bits/stl_vector.h | 19 +++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 050b027fbe5..9704fd60149 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,9 @@ +2018-07-26 Marc Glisse + + * include/bits/stl_vector.h (_Vector_impl_data::_M_copy_data): New. + (_Vector_impl_data::_M_swap_data): Use _M_copy_data. + (vector::_M_move_assign): Reorder the swaps. + 2018-07-26 Jonathan Wakely PR libstdc++/86676 diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index d2be98883b3..424971a02f2 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -103,11 +103,22 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER #endif void + _M_copy_data(_Vector_impl_data const& __x) _GLIBCXX_NOEXCEPT + { + _M_start = __x._M_start; + _M_finish = __x._M_finish; + _M_end_of_storage = __x._M_end_of_storage; + } + + void _M_swap_data(_Vector_impl_data& __x) _GLIBCXX_NOEXCEPT { - std::swap(_M_start, __x._M_start); - std::swap(_M_finish, __x._M_finish); - std::swap(_M_end_of_storage, __x._M_end_of_storage); + // Do not use std::swap(_M_start, __x._M_start), etc as it loses + // information used by TBAA. + _Vector_impl_data __tmp; + __tmp._M_copy_data(*this); + _M_copy_data(__x); + __x._M_copy_data(__tmp); } }; @@ -1731,8 +1742,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _M_move_assign(vector&& __x, true_type) noexcept { vector __tmp(get_allocator()); - this->_M_impl._M_swap_data(__tmp._M_impl); this->_M_impl._M_swap_data(__x._M_impl); + __tmp._M_impl._M_swap_data(__x._M_impl); std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator()); } -- 2.11.4.GIT