Skip to content

Commit 2100515

Browse files
author
Howard Hinnant
committed
Modest performance improvement for std::string's operator==.
llvm-svn: 180072
1 parent dea46d7 commit 2100515

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

libcxx/include/string

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,11 @@ public:
14621462
int compare(size_type __pos1, size_type __n1, const_pointer __s, size_type __n2) const;
14631463

14641464
_LIBCPP_INLINE_VISIBILITY bool __invariants() const;
1465+
1466+
_LIBCPP_INLINE_VISIBILITY
1467+
bool __is_long() const _NOEXCEPT
1468+
{return bool(__r_.first().__s.__size_ & __short_mask);}
1469+
14651470
private:
14661471
_LIBCPP_INLINE_VISIBILITY
14671472
allocator_type& __alloc() _NOEXCEPT
@@ -1470,10 +1475,6 @@ private:
14701475
const allocator_type& __alloc() const _NOEXCEPT
14711476
{return __r_.second();}
14721477

1473-
_LIBCPP_INLINE_VISIBILITY
1474-
bool __is_long() const _NOEXCEPT
1475-
{return bool(__r_.first().__s.__size_ & __short_mask);}
1476-
14771478
_LIBCPP_INLINE_VISIBILITY
14781479
void __set_short_size(size_type __s) _NOEXCEPT
14791480
#if _LIBCPP_BIG_ENDIAN
@@ -3561,9 +3562,29 @@ bool
35613562
operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
35623563
const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
35633564
{
3564-
return __lhs.size() == __rhs.size() && _Traits::compare(__lhs.data(),
3565-
__rhs.data(),
3566-
__lhs.size()) == 0;
3565+
size_t __lhs_sz = __lhs.size();
3566+
return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3567+
__rhs.data(),
3568+
__lhs_sz) == 0;
3569+
}
3570+
3571+
template<class _Allocator>
3572+
_LIBCPP_INLINE_VISIBILITY inline
3573+
bool
3574+
operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3575+
const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3576+
{
3577+
size_t __lhs_sz = __lhs.size();
3578+
if (__lhs_sz != __rhs.size())
3579+
return false;
3580+
const char* __lp = __lhs.data();
3581+
const char* __rp = __rhs.data();
3582+
if (__lhs.__is_long())
3583+
return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3584+
for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3585+
if (*__lp != *__rp)
3586+
return false;
3587+
return true;
35673588
}
35683589

35693590
template<class _CharT, class _Traits, class _Allocator>

0 commit comments

Comments
 (0)