Skip to content

Commit

Permalink
libstdc++: Check lengths first in operator== for basic_string [PR62187]
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jwakely committed Jun 14, 2022
1 parent 1b65779 commit 6abe341
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions libstdc++-v3/include/bits/basic_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
/**
Expand Down Expand Up @@ -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 !=
/**
Expand All @@ -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.
Expand Down

0 comments on commit 6abe341

Please sign in to comment.