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

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 25 23:54:49 PDT 2015


EricWF created this revision.
EricWF added a reviewer: mclow.lists.
EricWF added a subscriber: cfe-commits.

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

http://reviews.llvm.org/D12355

Files:
  include/string

Index: include/string
===================================================================
--- include/string
+++ include/string
@@ -1123,6 +1123,27 @@
     return __murmur2_or_cityhash<size_t>()(__p, (__e-__p)*sizeof(value_type));
 }
 
+
+template <class _LHS, class _RHS>
+inline _LIBCPP_INLINE_VISIBILITY
+int __do_string_compare(_LHS const& __lhs, _RHS const& __rhs) {
+    return __lhs.compare(0, _LHS::npos, __rhs, _LHS::traits_type::length(__rhs));
+}
+
+template <class _Allocator>
+inline _LIBCPP_INLINE_VISIBILITY
+int __do_string_compare(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
+                        const char* __rhs) {
+    return _VSTD::strcmp(__lhs.data(), __rhs);
+}
+
+template <class _Allocator>
+inline _LIBCPP_INLINE_VISIBILITY
+int __do_string_compare(const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs,
+                        const wchar_t* __rhs) {
+    return _VSTD::wcscmp(__lhs.data(), __rhs);
+}
+
 // basic_string
 
 template<class _CharT, class _Traits, class _Allocator>
@@ -3702,7 +3723,7 @@
 basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
 {
     _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
-    return compare(0, npos, __s, traits_type::length(__s));
+    return __do_string_compare(*this, __s);
 }
 
 template <class _CharT, class _Traits, class _Allocator>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12355.33183.patch
Type: text/x-patch
Size: 1408 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150826/83b15bbf/attachment.bin>


More information about the cfe-commits mailing list