From 7b6e6718090119892412fea174df2f21cbd1d600 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Thu, 5 Jan 2017 13:29:30 +0100 Subject: [PATCH] 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 --- src/libs/utils/smallstring.h | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/libs/utils/smallstring.h b/src/libs/utils/smallstring.h index 147fba5fab9..8aa6e08d7c8 100644 --- a/src/libs/utils/smallstring.h +++ b/src/libs/utils/smallstring.h @@ -655,9 +655,30 @@ unitttest_public: 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; }