From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 6384D385781D; Fri, 1 Oct 2021 19:38:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6384D385781D MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-4065] libstdc++: Make move ctor noexcept for fully-dynamic string X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: ce709ad3dc0ed5d7ea48a116311d4441225446f0 X-Git-Newrev: 10b6d89baddd86139480ba902f491903fcb464a6 Message-Id: <20211001193839.6384D385781D@sourceware.org> Date: Fri, 1 Oct 2021 19:38:39 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Oct 2021 19:38:39 -0000 https://gcc.gnu.org/g:10b6d89baddd86139480ba902f491903fcb464a6 commit r12-4065-g10b6d89baddd86139480ba902f491903fcb464a6 Author: Jonathan Wakely Date: Fri Apr 30 15:04:34 2021 +0100 libstdc++: Make move ctor noexcept for fully-dynamic string The move constructor for the "fully-dynamic" COW string is not noexcept, because it allocates a new empty string rep for the moved-from string. However, there is no need to do that, because the moved-from string does not have to be left empty. Instead, implement move construction for the fully-dynamic case as a reference count increment, so the string is shared. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/cow_string.h [_GLIBCXX_FULLY_DYNAMIC_STRING] (basic_string(basic_string&&)): Add noexcept and avoid allocation, by sharing rep with the rvalue string. Diff: --- libstdc++-v3/include/bits/cow_string.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/include/bits/cow_string.h b/libstdc++-v3/include/bits/cow_string.h index 61edaa85484..ba4a8cc2e98 100644 --- a/libstdc++-v3/include/bits/cow_string.h +++ b/libstdc++-v3/include/bits/cow_string.h @@ -620,18 +620,25 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * The newly-created string contains the exact contents of @a __str. * @a __str is a valid, but unspecified string. */ - basic_string(basic_string&& __str) + basic_string(basic_string&& __str) noexcept #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 - noexcept // FIXME C++11: should always be noexcept. -#endif : _M_dataplus(std::move(__str._M_dataplus)) { -#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 __str._M_data(_S_empty_rep()._M_refdata()); + } #else - __str._M_data(_S_construct(size_type(), _CharT(), get_allocator())); -#endif + : _M_dataplus(__str._M_rep()) + { + // Rather than allocate an empty string for the rvalue string, + // just share ownership with it by incrementing the reference count. + // If the rvalue string was "leaked" then it was the unique owner, + // so need an extra increment to indicate shared ownership. + if (_M_rep()->_M_is_leaked()) + __gnu_cxx::__atomic_add_dispatch(&_M_rep()->_M_refcount, 2); + else + __gnu_cxx::__atomic_add_dispatch(&_M_rep()->_M_refcount, 1); } +#endif /** * @brief Construct string from an initializer %list.