QMetaEnum: write "proper code"

Rewrite keysToValue() as suggested by a comment:

- Don't use the moral equivalent of
     QString::fromLatin1(keys).split().front().toLatin1(),
  use QStringTokenizer over QLatin1Strings, removing lots of
  allocations.

- Use QL1S instead of raw char* and strcmp(), because that made the
  old code rely on NUL-terminated tokens after splitting, which is no
  longer the case when using QStringTokenizer.

- Use the new stringDataView() instead of stringData() to avoid
  QByteArray dtors littering the code.

- Extract Method parse_scope(), using high-level API operating on QL1S
  to see what it's actually doing, instead of previous low-level
  bit-pushing with char* and ints that did a good job of obfuscating
  the purpose of all the scanning.

- Extract Method lookup() as a lambda to make the main loop of the
  algorithm more readable.

- Extract Method className() to delay looking up the class name until
  it is required (and help with readability). This could be further
  optimized by memoizing the result, but I'm convinced that's not
  worth the effort.

The code now no longer allocates, but we still can't mark the function
as noexcept, because stringDataView() contains a Q_ASSERT, so the
function has preconditions.

Results show that the new code is up to 2x faster than the old, and
never slower (within measurement uncertainty):

 PASS   : tst_QMetaEnum::keysToValue(0 bits set)
-     0.00042 msecs per iteration (total: 56, iterations: 131072)
+     0.00012 msecs per iteration (total: 68, iterations: 524288)
 PASS   : tst_QMetaEnum::keysToValue(1 bits set)
-     0.00079 msecs per iteration (total: 52, iterations: 65536)
+     0.00024 msecs per iteration (total: 63, iterations: 262144)
 PASS   : tst_QMetaEnum::keysToValue(2 bits set)
-     0.0010 msecs per iteration (total: 71, iterations: 65536)
+     0.00040 msecs per iteration (total: 53, iterations: 131072)
 PASS   : tst_QMetaEnum::keysToValue(3 bits set)
-     0.0014 msecs per iteration (total: 98, iterations: 65536)
+     0.00054 msecs per iteration (total: 72, iterations: 131072)
 PASS   : tst_QMetaEnum::keysToValue(4 bits set)
-     0.0017 msecs per iteration (total: 57, iterations: 32768)
+     0.00074 msecs per iteration (total: 98, iterations: 131072)
 PASS   : tst_QMetaEnum::keysToValue(5 bits set)
-     0.0019 msecs per iteration (total: 65, iterations: 32768)
+     0.00088 msecs per iteration (total: 58, iterations: 65536)
 PASS   : tst_QMetaEnum::keysToValue(6 bits set)
-     0.0022 msecs per iteration (total: 74, iterations: 32768)
+     0.0010 msecs per iteration (total: 72, iterations: 65536)
 PASS   : tst_QMetaEnum::keysToValue(7 bits set)
-     0.0025 msecs per iteration (total: 85, iterations: 32768)
+     0.0012 msecs per iteration (total: 79, iterations: 65536)
 PASS   : tst_QMetaEnum::keysToValue(8 bits set)
-     0.0027 msecs per iteration (total: 91, iterations: 32768)
+     0.0012 msecs per iteration (total: 85, iterations: 65536)
 PASS   : tst_QMetaEnum::keysToValue(9 bits set)
-     0.0029 msecs per iteration (total: 98, iterations: 32768)
+     0.0014 msecs per iteration (total: 97, iterations: 65536)
 PASS   : tst_QMetaEnum::keysToValue(10 bits set)
-     0.0033 msecs per iteration (total: 55, iterations: 16384)
+     0.0018 msecs per iteration (total: 62, iterations: 32768)
 PASS   : tst_QMetaEnum::keysToValue(11 bits set)
-     0.0036 msecs per iteration (total: 60, iterations: 16384)
+     0.0022 msecs per iteration (total: 73, iterations: 32768)
 PASS   : tst_QMetaEnum::keysToValue(12 bits set)
-     0.0036 msecs per iteration (total: 60, iterations: 16384)
+     0.0018 msecs per iteration (total: 62, iterations: 32768)
 PASS   : tst_QMetaEnum::keysToValue(13 bits set)
-     0.0039 msecs per iteration (total: 64, iterations: 16384)
+     0.0021 msecs per iteration (total: 70, iterations: 32768)
 PASS   : tst_QMetaEnum::keysToValue(14 bits set)
-     0.0040 msecs per iteration (total: 67, iterations: 16384)
+     0.0023 msecs per iteration (total: 77, iterations: 32768)
 PASS   : tst_QMetaEnum::keysToValue(15 bits set)
-     0.0042 msecs per iteration (total: 70, iterations: 16384)
+     0.0025 msecs per iteration (total: 82, iterations: 32768)
 PASS   : tst_QMetaEnum::keysToValue(16 bits set)
-     0.0053 msecs per iteration (total: 88, iterations: 16384)
+     0.0028 msecs per iteration (total: 92, iterations: 32768)
 PASS   : tst_QMetaEnum::keysToValue(17 bits set)
-     0.0048 msecs per iteration (total: 80, iterations: 16384)
+     0.0029 msecs per iteration (total: 97, iterations: 32768)
 PASS   : tst_QMetaEnum::keysToValue(18 bits set)
-     0.0050 msecs per iteration (total: 83, iterations: 16384)
+     0.0031 msecs per iteration (total: 51, iterations: 16384)
 PASS   : tst_QMetaEnum::keysToValue(19 bits set)
-     0.0051 msecs per iteration (total: 85, iterations: 16384)
+     0.0037 msecs per iteration (total: 62, iterations: 16384)
 PASS   : tst_QMetaEnum::keysToValue(20 bits set)
-     0.0053 msecs per iteration (total: 88, iterations: 16384)
+     0.0041 msecs per iteration (total: 68, iterations: 16384)
 PASS   : tst_QMetaEnum::keysToValue(21 bits set)
-     0.0056 msecs per iteration (total: 92, iterations: 16384)
+     0.0042 msecs per iteration (total: 69, iterations: 16384)
 PASS   : tst_QMetaEnum::keysToValue(22 bits set)
-     0.0056 msecs per iteration (total: 93, iterations: 16384)
+     0.0044 msecs per iteration (total: 73, iterations: 16384)
 PASS   : tst_QMetaEnum::keysToValue(23 bits set)
-     0.0057 msecs per iteration (total: 95, iterations: 16384)
+     0.0044 msecs per iteration (total: 73, iterations: 16384)
 PASS   : tst_QMetaEnum::keysToValue(24 bits set)
-     0.0060 msecs per iteration (total: 99, iterations: 16384)
+     0.0062 msecs per iteration (total: 51, iterations: 8192)
 PASS   : tst_QMetaEnum::keysToValue(25 bits set)
-     0.0063 msecs per iteration (total: 52, iterations: 8192)
+     0.0048 msecs per iteration (total: 80, iterations: 16384)
 PASS   : tst_QMetaEnum::keysToValue(26 bits set)
-     0.000381 msecs per iteration (total: 100, iterations: 262144)
+     0.00014 msecs per iteration (total: 75, iterations: 524288)
 PASS   : tst_QMetaEnum::keysToValue(27 bits set)
-     0.00616 msecs per iteration (total: 101, iterations: 16384)
+     0.0050 msecs per iteration (total: 82, iterations: 16384)
 PASS   : tst_QMetaEnum::keysToValue(28 bits set)
-     0.0062 msecs per iteration (total: 51, iterations: 8192)
+     0.0051 msecs per iteration (total: 85, iterations: 16384)
 PASS   : tst_QMetaEnum::keysToValue(29 bits set)
-     0.0064 msecs per iteration (total: 53, iterations: 8192)
+     0.0050 msecs per iteration (total: 82, iterations: 16384)
 PASS   : tst_QMetaEnum::keysToValue(30 bits set)
-     0.0062 msecs per iteration (total: 51, iterations: 8192)
+     0.0050 msecs per iteration (total: 83, iterations: 16384)

Change-Id: Idff1ef7633862beb318901352516ebb0dde3c058
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2021-08-12 09:19:15 +02:00
parent c3308b13ca
commit 17357856b5

View File

@ -2830,6 +2830,19 @@ const char *QMetaEnum::valueToKey(int value) const
return nullptr;
}
static auto parse_scope(QLatin1String qualifiedKey) noexcept
{
struct R {
std::optional<QLatin1String> scope;
QLatin1String key;
};
const auto scopePos = qualifiedKey.lastIndexOf(QLatin1String("::"));
if (scopePos < 0)
return R{std::nullopt, qualifiedKey};
else
return R{qualifiedKey.first(scopePos), qualifiedKey.sliced(scopePos + 2)};
}
/*!
Returns the value derived from combining together the values of
the \a keys using the OR operator, or -1 if \a keys is not
@ -2846,34 +2859,25 @@ int QMetaEnum::keysToValue(const char *keys, bool *ok) const
*ok = false;
if (!mobj || !keys)
return -1;
const QString keysString = QString::fromLatin1(keys);
const auto splitKeys = QStringView{keysString}.split(QLatin1Char('|'));
// ### TODO write proper code: do not allocate memory, so we can go nothrow
auto lookup = [&] (QLatin1String key) -> std::optional<int> {
for (int i = data.keyCount() - 1; i >= 0; --i) {
if (key == stringDataView(mobj, mobj->d.data[data.data() + 2*i]))
return mobj->d.data[data.data() + 2*i + 1];
}
return std::nullopt;
};
auto className = [&] { return stringDataView(mobj, priv(mobj->d.data)->className); };
int value = 0;
for (QStringView untrimmed : splitKeys) {
const QStringView trimmed = untrimmed.trimmed();
QByteArray qualified_key = trimmed.toLatin1();
const char *key = qualified_key.constData();
uint scope = 0;
const char *s = key + qstrlen(key);
while (s > key && *s != ':')
--s;
if (s > key && *(s - 1) == ':') {
scope = s - key - 1;
key += scope + 2;
}
int i;
for (i = data.keyCount() - 1; i >= 0; --i) {
const QByteArray className = stringData(mobj, priv(mobj->d.data)->className);
if ((!scope || (className.size() == int(scope) && strncmp(qualified_key.constData(), className.constData(), scope) == 0))
&& strcmp(key, rawStringData(mobj, mobj->d.data[data.data() + 2*i])) == 0) {
value |= mobj->d.data[data.data() + 2*i + 1];
break;
}
}
if (i < 0) {
return -1;
}
for (const QLatin1String untrimmed : qTokenize(QLatin1String{keys}, QLatin1Char{'|'})) {
const auto parsed = parse_scope(untrimmed.trimmed());
if (parsed.scope && *parsed.scope != className())
return -1; // wrong type name in qualified name
if (auto thisValue = lookup(parsed.key))
value |= *thisValue;
else
return -1; // no such enumerator
}
if (ok != nullptr)
*ok = true;