From patchwork Wed Jan 25 18:38:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Fran=C3=A7ois_Dumont?= X-Patchwork-Id: 63682 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB9363858409 for ; Wed, 25 Jan 2023 18:39:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB9363858409 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674671966; bh=ueafQ1Hurs5FdKF3mlsOnk8q55mznmg/TXoRyMix6iU=; h=Date:Subject:To:Cc:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=mMLvIBx1OUc3nTLJslrXYvjwUlLTHVhggxL0gtNysb3KVX0Cmw3JsNq3U+KnelZJU cewdAc0+TIXpg6aaLMhWkHm/ozAyT/uyDWXOPfOHFfINxO02E49oHctlz8Q7EtE7nq 5FcjxxU5HmQNqA2bEaSfrlKBdfMD8+ERfpQjra+k= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by sourceware.org (Postfix) with ESMTPS id CB0063858D28; Wed, 25 Jan 2023 18:38:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CB0063858D28 Received: by mail-ed1-x534.google.com with SMTP id w11so7474554edv.0; Wed, 25 Jan 2023 10:38:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=ueafQ1Hurs5FdKF3mlsOnk8q55mznmg/TXoRyMix6iU=; b=p3qnGFm/tzUlZpWg18mn6SexU3ZpdIz9izKtgw4XZ0xufYEGRVA+hLWYLngoGpCS7y Eq81TwizS3Qy+tx/5twYp3DwXBrSFYccZ/0zratO8QYcPiiQg/ekQX9cX492PUKY6riN /rqhlrbvoPk4M0ejPbxJUI/pKqMRuAEttkizeXQzk5Vq9rJkoL9v7pi+Hd3P0iwAe9bB VgpeZwa2tnIivMP+fir5jt0jIxZ1tKZvswU136VTZAE0Pv6+BT2vNRXefj78NBlqtFHw HaCGpseK5+RM6xyN/ICRaEqHoOvmXsZemYVLUvPPSF+11IgRsB4Wgw/8fe3ZnbUNrn51 rGiQ== X-Gm-Message-State: AFqh2kocH/orBFBwWNWz4mL1RjXwU/nKFsPgwK0gXj33Yrna5QXcXwZU 9f1y4tJAcjCru2EyIAS8EIs= X-Google-Smtp-Source: AMrXdXu3UTwy9wPmbWFEhU5MYkwYJ+HcuBPpQtakE123G6GkVrqAsslcRrg6PMbPp56TkFYkjVrJQg== X-Received: by 2002:aa7:cc81:0:b0:465:f6a9:cb7b with SMTP id p1-20020aa7cc81000000b00465f6a9cb7bmr33916028edt.12.1674671934145; Wed, 25 Jan 2023 10:38:54 -0800 (PST) Received: from [10.11.0.210] ([109.190.253.11]) by smtp.googlemail.com with ESMTPSA id et10-20020a056402378a00b00457b5ba968csm2678911edb.27.2023.01.25.10.38.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 25 Jan 2023 10:38:53 -0800 (PST) Message-ID: Date: Wed, 25 Jan 2023 19:38:47 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: [PATCH] minor optimization bug in basic_string move assignment To: Jonathan Wakely , "libstdc++@gcc.gnu.org" Cc: waffl3x , gcc-patches References: <52e5d904-da8a-14f1-6704-53f89dbd2d69@gmail.com> Content-Language: fr In-Reply-To: X-Spam-Status: No, score=-8.0 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Fran=C3=A7ois_Dumont_via_Gcc-patches?= From: =?utf-8?q?Fran=C3=A7ois_Dumont?= Reply-To: =?utf-8?q?Fran=C3=A7ois_Dumont?= Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 ? 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 equal     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 equal     allocators.     [1] https://cplusplus.github.io/LWG/issue2593     libstdc++-v3/ChangeLog:             * include/bits/basic_string.h (operator=(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 ? François On 17/01/23 20:18, Jonathan Wakely wrote: > On Wed, 4 Jan 2023 at 18:21, François 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=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. > diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index aa018262c98..c81dc0d425a 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(); diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/allocator/char/move_assign.cc b/libstdc++-v3/testsuite/21_strings/basic_string/allocator/char/move_assign.cc index cc58348e116..21e0b1cb4f4 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string/allocator/char/move_assign.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string/allocator/char/move_assign.cc @@ -28,6 +28,8 @@ const C c = 'a'; using traits = std::char_traits; using __gnu_test::propagating_allocator; +using __gnu_test::tracker_allocator_counter; +using __gnu_test::tracker_allocator; void test01() { @@ -146,10 +148,60 @@ void test03() VERIFY(7 == v8.get_allocator().get_personality()); } +void test04() +{ + typedef propagating_allocator> alloc_type; + typedef std::basic_string test_type; + + { + tracker_allocator_counter::reset(); + test_type v1(alloc_type(1)); + v1 = "abcdefghijklmnopqr10"; + auto ref_alloc_count = tracker_allocator_counter::get_allocation_count(); + + test_type v2(alloc_type(2)); + v2 = "abcdefghijklmnopqr20"; + v2 = std::move(v1); + VERIFY(1 == v1.get_allocator().get_personality()); + VERIFY(1 == v2.get_allocator().get_personality()); + + VERIFY( tracker_allocator_counter::get_allocation_count() == 2 * ref_alloc_count ); + VERIFY( tracker_allocator_counter::get_deallocation_count() == ref_alloc_count ); + + v1 = "abcdefghijklmnopqr11"; + + VERIFY( tracker_allocator_counter::get_allocation_count() == 3 * ref_alloc_count ); + } + + { + tracker_allocator_counter::reset(); + test_type v1(alloc_type(1)); + v1 = "abcdefghijklmnopqr10"; + auto ref_alloc_count = tracker_allocator_counter::get_allocation_count(); + + test_type v2(alloc_type(1)); + v2 = "abcdefghijklmnopqr20"; + v2 = std::move(v1); + VERIFY(1 == v1.get_allocator().get_personality()); + VERIFY(1 == v2.get_allocator().get_personality()); + + VERIFY( tracker_allocator_counter::get_allocation_count() == 2 * ref_alloc_count ); + VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 ); + + v1 = "abcdefghijklmnopqr11"; + + VERIFY( tracker_allocator_counter::get_allocation_count() == 2 * ref_alloc_count ); + } + + VERIFY( tracker_allocator_counter::get_allocation_count() == + tracker_allocator_counter::get_deallocation_count() ); +} + int main() { test01(); test02(); test03(); + test04(); return 0; }