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 C09203858D28 for ; Tue, 17 Jan 2023 19:18:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C09203858D28 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=1673983124; 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=BTkWSFsG9Pd7GXvoaM5efS2OeVaedfl2pCyEOL878yg=; b=E4vCmBvqKWfSPphYh8UlsZ6BzG5qaU8UHPiTgJe+gyFJLobxp8IfBaoXbSao3DbLSIj0Aj rH9eOlugn6wMC7Eu2obY01+3LJB3yieHcXZKnNGaQntAzgWuLDTuYND1vi/HWrOKTKzpha BfOMr9zQGGqtp85SEAgomn0Owl1HilY= Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-381-o4ciSyAGP1GFYsfdMcAgeQ-1; Tue, 17 Jan 2023 14:18:43 -0500 X-MC-Unique: o4ciSyAGP1GFYsfdMcAgeQ-1 Received: by mail-lf1-f70.google.com with SMTP id a20-20020ac25214000000b004b57756f937so11843236lfl.3 for ; Tue, 17 Jan 2023 11:18:42 -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=BTkWSFsG9Pd7GXvoaM5efS2OeVaedfl2pCyEOL878yg=; b=KELWSnoulg1pMbn1F6gK4Pk0R5y8+8W3BtpyhrFdx12ZbR3TFNYXjLci5xDcezs42y mXPHCsiwnzPgyZQlk+CAMmkzb8hqY20rcWP7NbpkEYPTJoWsnscP2lg5ReB6oUwbf70b G2e8rE4Mf9QNfjcE0WvQVucFGOhnwbYz0+q1mVuxUxxUEkxqP3RFsbfM1fskQ7/MR9O4 yKOgN3nFlg1kabHokX3opxGTjOwBCYHcKrks1b3/GC6/VQwKtJSRdWum+LuvbwXBMVUB v9syqwkgM734OCX4G1xMuG3quy7jVgR64YWPa5s//QmJD5QObKhv40RMQZWz4DOc9wsd piAw== X-Gm-Message-State: AFqh2ko8wnAWca9wHFiPdJRL2tvJIH6oOpUVCp0U/SBMl6G10aArgCwK 8bZqWPYBp77E+ReIaNZgL66nwLilcBPPffN7NpWKd13JAMu7cp4apuhsm+bs2cJe8nSsh4WlEie V+5dHJJ1rQiI75tNJ0cpOJ2AYpLCgz8E= X-Received: by 2002:ac2:558d:0:b0:4b5:828b:3fa4 with SMTP id v13-20020ac2558d000000b004b5828b3fa4mr201086lfg.460.1673983121616; Tue, 17 Jan 2023 11:18:41 -0800 (PST) X-Google-Smtp-Source: AMrXdXtvTnfmABPAtObBAEe7asAyKVBGFCeBBhUKUPWUH641LC0g2HE+PheH5PhIUjbMZ1pKM3Smrvq19UlJFFEv8vg= X-Received: by 2002:ac2:558d:0:b0:4b5:828b:3fa4 with SMTP id v13-20020ac2558d000000b004b5828b3fa4mr201083lfg.460.1673983121352; Tue, 17 Jan 2023 11:18:41 -0800 (PST) MIME-Version: 1.0 References: <52e5d904-da8a-14f1-6704-53f89dbd2d69@gmail.com> In-Reply-To: <52e5d904-da8a-14f1-6704-53f89dbd2d69@gmail.com> From: Jonathan Wakely Date: Tue, 17 Jan 2023 19:18:30 +0000 Message-ID: Subject: Re: minor optimization bug in basic_string move assignment To: =?UTF-8?Q?Fran=C3=A7ois_Dumont?= Cc: waffl3x , "libstdc++@gcc.gnu.org" 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.2 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, 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/incl= ude/bits/basic_string.h;hb=3DHEAD#l880 > > When move assigning to a basic_string, the allocated memory of the move= d 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 optimizatio= n is never taken. Unless there is some reason I can't think of that makes e= qual stateful allocators incompatible here, I believe the if statement on l= ine 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 le= ak 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.