public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Make move ctor noexcept for fully-dynamic string
Date: Fri, 1 Oct 2021 20:41:05 +0100	[thread overview]
Message-ID: <YVdkUUq9z68lKKiO@redhat.com> (raw)

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

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 <jwakely@redhat.com>

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.

Tested powerpc64le-linux. Committed to trunk.


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

commit 10b6d89baddd86139480ba902f491903fcb464a6
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Apr 30 15:04:34 2021

    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 <jwakely@redhat.com>
    
    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 --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
       }
+#else
+      : _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.

                 reply	other threads:[~2021-10-01 19:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YVdkUUq9z68lKKiO@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).