[cfe-commits] [libcxx] r135893 - /libcxx/trunk/include/string
Howard Hinnant
hhinnant at apple.com
Mon Jul 25 11:28:05 PDT 2011
On Jul 25, 2011, at 2:21 PM, Benjamin Kramer wrote:
> On Sun, Jul 24, 2011 at 14:45, Howard Hinnant <hhinnant at apple.com> wrote:
>> Author: hhinnant
>> Date: Sun Jul 24 16:45:06 2011
>> New Revision: 135893
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=135893&view=rev
>> Log:
>> Optimization of string::operator< by M.E. O'Neill. Discussion in http://llvm.org/bugs/show_bug.cgi?id=10461
>>
>> Modified:
>> libcxx/trunk/include/string
>>
>> Modified: libcxx/trunk/include/string
>> URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/string?rev=135893&r1=135892&r2=135893&view=diff
>> ==============================================================================
>> --- libcxx/trunk/include/string (original)
>> +++ libcxx/trunk/include/string Sun Jul 24 16:45:06 2011
>> @@ -3392,7 +3392,17 @@
>> int
>> basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
>> {
>> - return compare(0, npos, __str.data(), __str.size());
>> + size_t __lhs_sz = size();
>> + size_t __rhs_sz = __str.size();
>> + int __result = traits_type::compare(data(), __str.data(),
>> + _VSTD::min(__lhs_sz, __rhs_sz));
>> + if (__result != 0)
>> + return __result;
>> + if (__lhs_sz < __rhs_sz)
>> + return -1;
>> + if (__lhs_sz > __rhs_sz)
>> + return 1;
>> + return 0;
>> }
>>
>> template <class _CharT, class _Traits, class _Allocator>
>
> Wouldn't it be faster to compare sizes first and do the data
> comparison only if sizes are equal? I suppose traits_type::compare is
> the expensive part of this function.
This function has a 3-way result:
1. r < 0 if *this < __str
2. r == 0 if *this == __str
3. r > 0 if *this > __str
For cases 1 and 3, traits_type::compare must be called even if size() != __str.size(). I'm unsure how to proceed with your suggestion.
Howard
More information about the cfe-commits
mailing list