Utils: Change of less operator for SmallString

Comparing file paths can be quite expensive because the start very likely
with the same string. Sorting for size and only compare the string is less
expensive. For many algorithm we need a sort order, so making the less
operator cheaper is quite desirable.

Change-Id: I33e7abc7a65264e80376f445f8b6dcada0625ab9
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2017-01-05 13:29:30 +01:00
parent 8088923f2f
commit 7b6e671809

View File

@@ -655,9 +655,30 @@ unitttest_public:
friend bool operator<(const BasicSmallString& first, const BasicSmallString& second) noexcept friend bool operator<(const BasicSmallString& first, const BasicSmallString& second) noexcept
{ {
size_type minimalSize = std::min(first.size(), second.size()); if (first.size() != second.size())
return first.size() < second.size();
const int comparison = std::memcmp(first.data(), second.data(), minimalSize + 1); const int comparison = std::memcmp(first.data(), second.data(), first.size() + 1);
return comparison < 0;
}
friend bool operator<(const BasicSmallString& first, SmallStringView second) noexcept
{
if (first.size() != second.size())
return first.size() < second.size();
const int comparison = std::memcmp(first.data(), second.data(), first.size());
return comparison < 0;
}
friend bool operator<(SmallStringView first, const BasicSmallString& second) noexcept
{
if (first.size() != second.size())
return first.size() < second.size();
const int comparison = std::memcmp(first.data(), second.data(), first.size());
return comparison < 0; return comparison < 0;
} }