public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: "François Dumont" <frs.dumont@gmail.com>
Cc: waffl3x <waffl3x@protonmail.com>,
	"libstdc++@gcc.gnu.org" <libstdc++@gcc.gnu.org>
Subject: Re: minor optimization bug in basic_string move assignment
Date: Tue, 17 Jan 2023 19:18:30 +0000	[thread overview]
Message-ID: <CACb0b4mHxXAWZ69d+9ioBeWG9gAjDAvWyf6x0dZyfHC7RWqwpg@mail.gmail.com> (raw)
In-Reply-To: <52e5d904-da8a-14f1-6704-53f89dbd2d69@gmail.com>

On Wed, 4 Jan 2023 at 18:21, François Dumont via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> 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 ?

Since https://cplusplus.github.io/LWG/issue2593 they can't. But I
think when I wrote that code, they could do, which is probably why the
optimization wasn't done.


  reply	other threads:[~2023-01-17 19:18 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
2023-01-17 19:18   ` Jonathan Wakely [this message]
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=CACb0b4mHxXAWZ69d+9ioBeWG9gAjDAvWyf6x0dZyfHC7RWqwpg@mail.gmail.com \
    --to=jwakely@redhat.com \
    --cc=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).