[libcxx] r246266 - [libcxx] Optimize away unneeded length calculation in basic_string::compare(const char*)

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 27 20:02:38 PDT 2015


Author: ericwf
Date: Thu Aug 27 22:02:37 2015
New Revision: 246266

URL: http://llvm.org/viewvc/llvm-project?rev=246266&view=rev
Log:
[libcxx] Optimize away unneeded length calculation in basic_string::compare(const char*)

Summary:
This patch optimizes basic_string::compare to use strcmp when the default char_traits has been given.
See PR19900 for more information. https://llvm.org/bugs/show_bug.cgi?id=19900

Reviewers: mclow.lists

Subscribers: bkramer, cfe-commits

Differential Revision: http://reviews.llvm.org/D12355

Modified:
    libcxx/trunk/include/string

Modified: libcxx/trunk/include/string
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/string?rev=246266&r1=246265&r2=246266&view=diff
==============================================================================
--- libcxx/trunk/include/string (original)
+++ libcxx/trunk/include/string Thu Aug 27 22:02:37 2015
@@ -3795,7 +3795,11 @@ bool
 operator==(const _CharT* __lhs,
            const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
 {
-    return __rhs.compare(__lhs) == 0;
+    typedef basic_string<_CharT, _Traits, _Allocator> _String;
+    _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
+    size_t __lhs_len = _Traits::length(__lhs);
+    if (__lhs_len != __rhs.size()) return false;
+    return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
 }
 
 template<class _CharT, class _Traits, class _Allocator>
@@ -3804,7 +3808,11 @@ bool
 operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
            const _CharT* __rhs) _NOEXCEPT
 {
-    return __lhs.compare(__rhs) == 0;
+    typedef basic_string<_CharT, _Traits, _Allocator> _String;
+    _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
+    size_t __rhs_len = _Traits::length(__rhs);
+    if (__rhs_len != __lhs.size()) return false;
+    return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
 }
 
 // operator!=




More information about the cfe-commits mailing list