public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-1096] libstdc++: Check lengths first in operator== for basic_string [PR62187] Date: Tue, 14 Jun 2022 20:19:36 +0000 (GMT) [thread overview] Message-ID: <20220614201936.C4869384D191@sourceware.org> (raw) https://gcc.gnu.org/g:6abe341558abec40c9c44d76e7fb4fb3978e894b commit r13-1096-g6abe341558abec40c9c44d76e7fb4fb3978e894b Author: Jonathan Wakely <jwakely@redhat.com> Date: Tue Jun 14 16:19:32 2022 +0100 libstdc++: Check lengths first in operator== for basic_string [PR62187] 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<char>, 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. Diff: --- 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<typename _CharT> - _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.
reply other threads:[~2022-06-14 20:19 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20220614201936.C4869384D191@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@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: linkBe 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).