public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: "François Dumont" <frs.dumont@gmail.com>
To: waffl3x <waffl3x@protonmail.com>,
	"libstdc++@gcc.gnu.org" <libstdc++@gcc.gnu.org>
Subject: Re: minor optimization bug in basic_string move assignment
Date: Wed, 4 Jan 2023 19:20:43 +0100	[thread overview]
Message-ID: <52e5d904-da8a-14f1-6704-53f89dbd2d69@gmail.com> (raw)
In-Reply-To: <G9uH4h4S1h6p8JjtjxWOwtv0Z9UpcZGEsH_6Fw-Z6yM-Io23_8UuINXojQh3EfgNMqzHMytO5gGql_xOom_UouW4nzs0tC3h2T73H2_aIKM=@protonmail.com>

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

On 04/01/23 00:11, waffl3x via Libstdc++ wrote:
> Example: https://godbolt.org/z/sKhGqG1qK
> https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/bits/basic_string.h;hb=HEAD#l880
> When move assigning to a basic_string, the allocated memory of the moved into string is stored into the source string instead of deallocating it, a good optimization when everything is compatible. However in the case of a stateful allocator (is_always_true() evaluating as false) this optimization is never taken. Unless there is some reason I can't think of that makes equal stateful allocators incompatible here, I believe the if statement on line 880 of basic_string.h should also compare the equality of each strings allocator. The first condition in the function seems to indicate to me that this scenario was being considered and just forgotten about, as the memory doesn't get deallocated immediately if the two allocators are equal. I'll note that because of how everything is handled, this doesn't result in a leak so this bug is still only a minor missed optimization.
>
> mailto:libstdc++@gcc.gnu.org

Hmmm, I don't know, at least it is not as simple as you present it.

You cannot add a check on allocator equality as you are proposing 
because it is too late. __str allocator might have already been 
propagated to *this on the previous call to std::__alloc_on_move. Note 
that current check is done only if 
!_Alloc_traits::_S_propagate_on_move_assign().

This patch might do the job but I wonder if equal allocators can become 
un-equal after the propagate-on-move-assignment ?



[-- Attachment #2: basic_string.h.patch --]
[-- Type: text/x-patch, Size: 1450 bytes --]

diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index 109915653af..2627631e102 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -844,9 +844,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       operator=(basic_string&& __str)
       noexcept(_Alloc_traits::_S_nothrow_move())
       {
+	const bool __equal_allocs = _Alloc_traits::_S_always_equal()
+	    || _M_get_allocator() == __str._M_get_allocator()
 	if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
-	    && !_Alloc_traits::_S_always_equal()
-	    && _M_get_allocator() != __str._M_get_allocator())
+	    && !__equal_allocs)
 	  {
 	    // Destroy existing storage before replacing allocator.
 	    _M_destroy(_M_allocated_capacity);
@@ -868,16 +869,14 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
 		_M_set_length(__str.size());
 	      }
 	  }
-	else if (_Alloc_traits::_S_propagate_on_move_assign()
-	    || _Alloc_traits::_S_always_equal()
-	    || _M_get_allocator() == __str._M_get_allocator())
+	else if (_Alloc_traits::_S_propagate_on_move_assign() || __equal_allocs)
 	  {
 	    // Just move the allocated pointer, our allocator can free it.
 	    pointer __data = nullptr;
 	    size_type __capacity;
 	    if (!_M_is_local())
 	      {
-		if (_Alloc_traits::_S_always_equal())
+		if (__equal_allocs)
 		  {
 		    // __str can reuse our existing storage.
 		    __data = _M_data();

  reply	other threads:[~2023-01-04 18:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-03 23:11 waffl3x
2023-01-04 18:20 ` François Dumont [this message]
2023-01-17 19:18   ` Jonathan Wakely
2023-01-25 18:38     ` [PATCH] " François Dumont
2023-02-03 14:50       ` Jonathan Wakely
2023-02-04 13:11         ` François Dumont
2023-02-04 13:30           ` Jonathan Wakely

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=52e5d904-da8a-14f1-6704-53f89dbd2d69@gmail.com \
    --to=frs.dumont@gmail.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=waffl3x@protonmail.com \
    /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).