[libcxx-commits] [libcxx] [libc++] Improve code gen for string's operator== (PR #100926)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Jul 28 04:51:45 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Nikolas Klauser (philnik777)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/100926.diff
1 Files Affected:
- (modified) libcxx/include/string (+11-5)
``````````diff
diff --git a/libcxx/include/string b/libcxx/include/string
index 9fa979e3a5178..856e422d8c1e3 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -2246,6 +2246,10 @@ private:
friend constexpr basic_string operator+ <>(const basic_string&, type_identity_t<__self_view>);
friend constexpr basic_string operator+ <>(type_identity_t<__self_view>, const basic_string&);
#endif
+
+ template <class _CharT2, class _Traits2, class _Allocator2>
+ friend inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
+ operator==(const basic_string<_CharT2, _Traits2, _Allocator2>&, const _CharT2*) _NOEXCEPT;
};
// These declarations must appear before any functions are implicitly used
@@ -3855,16 +3859,18 @@ operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>&
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
-#if _LIBCPP_STD_VER >= 20
- return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
-#else
- typedef basic_string<_CharT, _Traits, _Allocator> _String;
_LIBCPP_ASSERT_NON_NULL(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
+
+ using _String = basic_string<_CharT, _Traits, _Allocator>;
+
size_t __rhs_len = _Traits::length(__rhs);
+ if (__builtin_constant_p(__rhs_len) && __rhs_len >= _String::__min_cap) {
+ if (!__lhs.__is_long())
+ return false;
+ }
if (__rhs_len != __lhs.size())
return false;
return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
-#endif
}
#if _LIBCPP_STD_VER >= 20
``````````
</details>
https://github.com/llvm/llvm-project/pull/100926
More information about the libcxx-commits
mailing list