From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 715EA3858C52 for ; Fri, 3 Feb 2023 14:50:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 715EA3858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675435845; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BJGscm8JkNfbNcoJoY7sTFGYlwsSixLyJSluv9rnr5k=; b=Sum0FoQamiyBIWysvrt8+tTDsSnJzzY/BGxjZxoy/grI76R9NE/t9qhqKmJTbyjCYhiqim lYYvGH3MqQN+kuVVQxBpd9npKgMPIxsQaVsGDdjjPrAbxftuCX5YPeCk6Lo4wDAh5wiVY9 DPfqanGX1qv8QHzpuoBXJJDc2lDsoF0= Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-601-NenyVatNM9qpeHVpoIdI6g-1; Fri, 03 Feb 2023 09:50:45 -0500 X-MC-Unique: NenyVatNM9qpeHVpoIdI6g-1 Received: by mail-lj1-f199.google.com with SMTP id b44-20020a05651c0b2c00b002905d4df023so1385716ljr.18 for ; Fri, 03 Feb 2023 06:50:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BJGscm8JkNfbNcoJoY7sTFGYlwsSixLyJSluv9rnr5k=; b=4VsHJYnrCVM6QPwGc7NGYtpZtLlEX6wzvXm4QPNJjJrHvxHawAhBZP+jvx4q6VH9ly VpeviYXPNpFWtSbIY0ybSgmL1FEQOyGpP5VYjE6OfS0ol1D323njY9w8EKcHI+ww0ezJ p8NLcRHvtwx0EXS/W1vbRjZ0sgDYXI2q7ErL4+FWBJBkmMp2lR5+OMo0zl+/zcRZQ4eu C5l3J0QSh2t+Z9uNwHulQjwTabo3O0W2fzNMmKuah/wd6Hzr/v4VkoR5dawbLsL9bEOW wRdRSubcnXwFqPnS4Ly9BcABDQcqpYz2DFVquIiFycbdrZmKf2NvlGmUN84tP7qsfzPT ZvfA== X-Gm-Message-State: AO0yUKU8X0WYotLX07He3437eFoU9vElnqDY4QEQGnTMkTZ9ilJOoQom FbxkYZip7rCz+eCR6tqspLOvI3IEMfu7r2DuZL99gZ8OMHckVJOsmLob77oCccPo4EU5OWgKvoe T/L4enx1HON/NWrGIPdDVvFo1yupfYTR9vw== X-Received: by 2002:ac2:5607:0:b0:4d8:6c48:511b with SMTP id v7-20020ac25607000000b004d86c48511bmr1868200lfd.86.1675435843686; Fri, 03 Feb 2023 06:50:43 -0800 (PST) X-Google-Smtp-Source: AK7set9yOBocLSLGTu9WA9LnCCRSARv8nXeo6BwCS3h6ZtTxO+SKIIC5EdH6G8Y6Vm0wUhpWqgBM/Hu4Zy12dqo1wic= X-Received: by 2002:ac2:5607:0:b0:4d8:6c48:511b with SMTP id v7-20020ac25607000000b004d86c48511bmr1868192lfd.86.1675435843331; Fri, 03 Feb 2023 06:50:43 -0800 (PST) MIME-Version: 1.0 References: <52e5d904-da8a-14f1-6704-53f89dbd2d69@gmail.com> In-Reply-To: From: Jonathan Wakely Date: Fri, 3 Feb 2023 14:50:32 +0000 Message-ID: Subject: Re: [PATCH] minor optimization bug in basic_string move assignment To: =?UTF-8?Q?Fran=C3=A7ois_Dumont?= Cc: "libstdc++@gcc.gnu.org" , waffl3x , gcc-patches X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-6.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, 25 Jan 2023 at 18:38, Fran=C3=A7ois Dumont w= rote: > > Let's submit a proper patch proposal then. > > The occasion for me to ask if there is any reason for cow string not > being C++11 allocator compliant ? Just lack of interest ? Mostly lack of interest, but also I don't really want to "encourage" the use of the old string by investing lots of maintenance effort into it. If you want new features like C++11 Allocators and resize_and_overwrite etc then you should use the new type. I don't remember if there were any actual blockers that made it difficult to support stateful allocators in the COW string. I might have written something about it in mails to the list when I was adding the SSO string, but I don't remember now. Anyway, for this patch ... > > I wanted to consider it to get rid of the __gnu_debug::_Safe_container > _IsCxx11AllocatorAware template parameter. > > libstdc++: Optimize basic_string move assignment > > Since resolution of Issue 2593 [1] we can consider that equal > allocators > before the propagate-on-move-assignment operations will still be equ= al > afterward. > > So we can extend the optimization of transfering the storage of the > move-to > instance to the move-from one that is currently limited to always eq= ual > allocators. > > [1] https://cplusplus.github.io/LWG/issue2593 > > libstdc++-v3/ChangeLog: > > * include/bits/basic_string.h (operator=3D(basic_string&&)): > Transfer move-to > storage to the move-from instance when allocators are equal. > * > testsuite/21_strings/basic_string/allocator/char/move_assign.cc (test04): > New test case. > > Tested under linux x86_64, ok to commit ? OK for trunk, thanks! +Reviewed-by: Jonathan Wakely > > On Wed, 4 Jan 2023 at 18:21, Fran=C3=A7ois Dumont via Libstdc++ > > wrote: > >> On 04/01/23 00:11, waffl3x via Libstdc++ wrote: > >>> Example: https://godbolt.org/z/sKhGqG1qK > >>> https://gcc.gnu.org/git/?p=3Dgcc.git;a=3Dblob;f=3Dlibstdc%2B%2B-v3/in= clude/bits/basic_string.h;hb=3DHEAD#l880 > >>> When move assigning to a basic_string, the allocated memory of the mo= ved 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 optimizat= ion 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 string= s allocator. The first condition in the function seems to indicate to me th= at this scenario was being considered and just forgotten about, as the memo= ry doesn't get deallocated immediately if the two allocators are equal. I'l= l 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 becom= e > >> 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. > >