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.133.124]) by sourceware.org (Postfix) with ESMTPS id BE056385023F for ; Tue, 14 Jun 2022 20:20:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BE056385023F Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-552-Qhg6Vbv-PhumLmm_QWikAQ-1; Tue, 14 Jun 2022 16:20:01 -0400 X-MC-Unique: Qhg6Vbv-PhumLmm_QWikAQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D590029AA2EB; Tue, 14 Jun 2022 20:20:00 +0000 (UTC) Received: from localhost (unknown [10.33.36.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id 98D7140C1247; Tue, 14 Jun 2022 20:20:00 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Check lengths first in operator== for basic_string [PR62187] Date: Tue, 14 Jun 2022 21:20:00 +0100 Message-Id: <20220614202000.1030288-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Jun 2022 20:20:04 -0000 Tested powerpc64le-linux, pushed to trunk. -- >8 -- As confirmed by LWG 2852, the calls to traits_type::compare do not need to be obsvervable, so we can make operator== compare string lengths first and return immediately for non-equal lengths. This avoids doing a slow string comparison for "abc...xyz" == "abc...xy". Previously we only did this optimization for std::char_traits, but we can enable it unconditionally thanks to LWG 2852. For comparisons with a const char* we can call traits_type::length right away to do the same optimization. That strlen call can be folded away for constant arguments, making it very efficient. For the pre-C++20 operator== and operator!= overloads we can swap the order of the arguments to take advantage of the operator== improvements. libstdc++-v3/ChangeLog: PR libstdc++/62187 * include/bits/basic_string.h (operator==): Always compare lengths before checking string contents. [!__cpp_lib_three_way_comparison] (operator==, operator!=): Reorder arguments. --- libstdc++-v3/include/bits/basic_string.h | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index a34b3d9ed28..57247e306dc 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -3627,17 +3627,10 @@ _GLIBCXX_END_NAMESPACE_CXX11 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs) _GLIBCXX_NOEXCEPT - { return __lhs.compare(__rhs) == 0; } - - template - _GLIBCXX20_CONSTEXPR - inline - typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type - operator==(const basic_string<_CharT>& __lhs, - const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT - { return (__lhs.size() == __rhs.size() - && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(), - __lhs.size())); } + { + return __lhs.size() == __rhs.size() + && !_Traits::compare(__lhs.data(), __rhs.data(), __lhs.size()); + } /** * @brief Test equivalence of string and C string. @@ -3650,7 +3643,10 @@ _GLIBCXX_END_NAMESPACE_CXX11 inline bool operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, const _CharT* __rhs) - { return __lhs.compare(__rhs) == 0; } + { + return __lhs.size() == _Traits::length(__rhs) + && !_Traits::compare(__lhs.data(), __rhs, __lhs.size()); + } #if __cpp_lib_three_way_comparison /** @@ -3691,7 +3687,7 @@ _GLIBCXX_END_NAMESPACE_CXX11 inline bool operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs) - { return __rhs.compare(__lhs) == 0; } + { return __rhs == __lhs; } // operator != /** @@ -3717,7 +3713,7 @@ _GLIBCXX_END_NAMESPACE_CXX11 inline bool operator!=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs) - { return !(__lhs == __rhs); } + { return !(__rhs == __lhs); } /** * @brief Test difference of string and C string. -- 2.34.3