public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: waffl3x <waffl3x@protonmail.com>
To: "libstdc++@gcc.gnu.org" <libstdc++@gcc.gnu.org>
Subject: minor optimization bug in basic_string move assignment
Date: Tue, 03 Jan 2023 23:11:53 +0000	[thread overview]
Message-ID: <G9uH4h4S1h6p8JjtjxWOwtv0Z9UpcZGEsH_6Fw-Z6yM-Io23_8UuINXojQh3EfgNMqzHMytO5gGql_xOom_UouW4nzs0tC3h2T73H2_aIKM=@protonmail.com> (raw)

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

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

             reply	other threads:[~2023-01-03 23:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-03 23:11 waffl3x [this message]
2023-01-04 18:20 ` François Dumont
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='G9uH4h4S1h6p8JjtjxWOwtv0Z9UpcZGEsH_6Fw-Z6yM-Io23_8UuINXojQh3EfgNMqzHMytO5gGql_xOom_UouW4nzs0tC3h2T73H2_aIKM=@protonmail.com' \
    --to=waffl3x@protonmail.com \
    --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).