From d9f7efba82eb47a028b343b6276250166ca1bc6e Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 13 Jun 2024 16:48:01 +0200 Subject: [PATCH] QUuid: rewrite compareThreeWay() in idiomatic form A lexicographical ordering chain with 3way-compare should call op<=> (or it's stand-in, compareThreeWay()) exactly once per element pair in the source ranges. First checking for != and then with <=> means we do the check twice. In this case, when comparing built-in types, the optimizer will probably fold everything for us, but code like this, at this time, has a high chance of being the source of a CnP operation, and the target may compare QStrings this way, so use the idiomatic form to let copy-pasters fall into the pit of success. Amends ef964c254c7a72bc05b1f4f0c6f270f9ad21fecd. Change-Id: Ib8344087f23435fc58740165afecd499722d1f00 Reviewed-by: Ivan Solovev (cherry picked from commit 1e6b7e08cf5f5a36fa3007e315a9282054c1a140) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/plugin/quuid.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/corelib/plugin/quuid.h b/src/corelib/plugin/quuid.h index 591d9b27c43..aa6e200ecb2 100644 --- a/src/corelib/plugin/quuid.h +++ b/src/corelib/plugin/quuid.h @@ -138,14 +138,14 @@ private: } friend Qt::strong_ordering compareThreeWay(const QUuid &lhs, const QUuid &rhs) noexcept { - if (lhs.variant() != rhs.variant()) - return Qt::compareThreeWay(lhs.variant(), rhs.variant()); - if (lhs.data1 != rhs.data1) - return Qt::compareThreeWay(lhs.data1, rhs.data1); - if (lhs.data2 != rhs.data2) - return Qt::compareThreeWay(lhs.data2, rhs.data2); - if (lhs.data3 != rhs.data3) - return Qt::compareThreeWay(lhs.data3, rhs.data3); + if (const auto c = Qt::compareThreeWay(lhs.variant(), rhs.variant()); !is_eq(c)) + return c; + if (const auto c = Qt::compareThreeWay(lhs.data1, rhs.data1); !is_eq(c)) + return c; + if (const auto c = Qt::compareThreeWay(lhs.data2, rhs.data2); !is_eq(c)) + return c; + if (const auto c = Qt::compareThreeWay(lhs.data3, rhs.data3); !is_eq(c)) + return c; int c = std::memcmp(lhs.data4, rhs.data4, sizeof(lhs.data4)); return Qt::compareThreeWay(c, 0);