[libcxx-commits] [PATCH] D59999: Make it easier for the compiler to optimize `operator==(string, char*)`.

Samuel Benzaquen via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 29 08:46:44 PDT 2019


sbenza created this revision.
sbenza added a reviewer: EricWF.
Herald added subscribers: libcxx-commits, jdoerfert, christof.
Herald added a project: libc++.

Make it easier for the compiler to optimize `operator==(string,char*)`
by removing one layer of indirection.
This generates better code in general by calling `memcmp`/`bcmp`
directly instead of going through string's `compare()` function.
It also allows better optimization when one of the operands is a compile
time array, like a literal string.
It dramatically simplifies the generated code for things like `x == ""`
and `x == "012345678"`.

Improves the BM_StringRelationalLiteral_Eq_Small_* benchmarks.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D59999

Files:
  libcxx/include/string


Index: libcxx/include/string
===================================================================
--- libcxx/include/string
+++ libcxx/include/string
@@ -3875,11 +3875,10 @@
 operator==(const _CharT* __lhs,
            const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
 {
-    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;
+    return _Traits::compare(__lhs, __rhs.data(), __lhs_len) == 0;
 }
 
 template<class _CharT, class _Traits, class _Allocator>
@@ -3888,11 +3887,10 @@
 operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
            const _CharT* __rhs) _NOEXCEPT
 {
-    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;
+    return _Traits::compare(__lhs.data(), __rhs, __rhs_len) == 0;
 }
 
 template<class _CharT, class _Traits, class _Allocator>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59999.192837.patch
Type: text/x-patch
Size: 1325 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190329/6a7f3ed1/attachment-0001.bin>


More information about the libcxx-commits mailing list