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>
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>
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits