Skip to content

Commit 3af26be

Browse files
authored
[libc++] Improve code gen for string's operator== (#100926)
If the string is too long for a short string, we can simply check for the long bit. If that's false we can do an early return. This improves the code gen slightly.
1 parent 289c049 commit 3af26be

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

libcxx/include/string

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,10 @@ private:
22462246
friend constexpr basic_string operator+ <>(const basic_string&, type_identity_t<__self_view>);
22472247
friend constexpr basic_string operator+ <>(type_identity_t<__self_view>, const basic_string&);
22482248
#endif
2249+
2250+
template <class _CharT2, class _Traits2, class _Allocator2>
2251+
friend inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
2252+
operator==(const basic_string<_CharT2, _Traits2, _Allocator2>&, const _CharT2*) _NOEXCEPT;
22492253
};
22502254

22512255
// These declarations must appear before any functions are implicitly used
@@ -3855,16 +3859,18 @@ operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>&
38553859
template <class _CharT, class _Traits, class _Allocator>
38563860
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
38573861
operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3858-
#if _LIBCPP_STD_VER >= 20
3859-
return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
3860-
#else
3861-
typedef basic_string<_CharT, _Traits, _Allocator> _String;
38623862
_LIBCPP_ASSERT_NON_NULL(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3863+
3864+
using _String = basic_string<_CharT, _Traits, _Allocator>;
3865+
38633866
size_t __rhs_len = _Traits::length(__rhs);
3867+
if (__builtin_constant_p(__rhs_len) && !_String::__fits_in_sso(__rhs_len)) {
3868+
if (!__lhs.__is_long())
3869+
return false;
3870+
}
38643871
if (__rhs_len != __lhs.size())
38653872
return false;
38663873
return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
3867-
#endif
38683874
}
38693875

38703876
#if _LIBCPP_STD_VER >= 20

0 commit comments

Comments
 (0)