From b2d24044c2b001851b5aa88d92a0d5de57f25210 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 13 Nov 2023 19:53:18 -0800 Subject: [PATCH] QFactoryLoader: use the new UTF-8 QCborStreamReader API for performance That way we don't need to decode to QString in order to compare to "Keys". There's no avoiding the conversion to it when inserting in the map for top-level elements, but use of moc's -M option is extremely rare. Ideally, we'd simply ask QCborStreamReader to perform a comparison to a string of ours, so we didn't have to memcpy from the stream in the first place. But TinyCBOR has no API for that (it's been pending as https://github.com/intel/tinycbor/issues/223 for a while). Change-Id: I8bd6bb457b9c42218247fffd1797607f75b153f4 Reviewed-by: Eirik Aavitsland Reviewed-by: Thiago Macieira Reviewed-by: Edward Welbourne --- src/corelib/plugin/qfactoryloader.cpp | 29 +++++++-------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/src/corelib/plugin/qfactoryloader.cpp b/src/corelib/plugin/qfactoryloader.cpp index 55d5bd4f9c..b1fe671fb4 100644 --- a/src/corelib/plugin/qfactoryloader.cpp +++ b/src/corelib/plugin/qfactoryloader.cpp @@ -65,21 +65,6 @@ struct QFactoryLoaderIidSearch QFactoryLoaderIidSearch(QLatin1StringView iid) : iid(iid) { Q_ASSERT(!iid.isEmpty()); } - static QString readString(QCborStreamReader &reader) - { - QString result; - if (!reader.isLengthKnown()) - return result; - auto r = reader.readString(); - if (r.status != QCborStreamReader::Ok) - return result; - result = std::move(r.data); - r = reader.readString(); - if (r.status != QCborStreamReader::EndOfString) - result = QString(); - return result; - } - static IterationResult::Result skip(QCborStreamReader &reader) { // skip this, whatever it is @@ -91,10 +76,10 @@ struct QFactoryLoaderIidSearch { if (key != QtPluginMetaDataKeys::IID) return skip(reader); - matchesIid = (readString(reader) == iid); + matchesIid = (reader.toString() == iid); return IterationResult::FinishedSearch; } - IterationResult::Result operator()(const QString &, QCborStreamReader &reader) + IterationResult::Result operator()(QUtf8StringView, QCborStreamReader &reader) { return skip(reader); } @@ -124,8 +109,8 @@ struct QFactoryLoaderMetaDataKeysExtractor : QFactoryLoaderIidSearch return IterationResult::ParsingError; while (reader.isValid()) { // the metadata is JSON, so keys are all strings - QString key = reader.toString(); - if (key == "Keys"_L1) { + QByteArray key = reader.toUtf8String(); + if (key == "Keys") { if (!reader.isArray() || !reader.isLengthKnown()) return IterationResult::InvalidHeaderItem; keys = QCborValue::fromCbor(reader).toArray(); @@ -170,10 +155,10 @@ template static IterationResult iterateInPluginMetaData(QByteArrayV return reader.lastError(); r = f(key, reader); } else if (reader.isString()) { - QString key = readString(reader); + QByteArray key = reader.toUtf8String(); if (key.isNull()) return reader.lastError(); - r = f(key, reader); + r = f(QUtf8StringView(key), reader); } else { return IterationResult::InvalidTopLevelItem; } @@ -206,7 +191,7 @@ bool QPluginParsedMetaData::parse(QByteArrayView raw) if constexpr (std::is_enum_v>) map[int(key)] = item; else - map[key] = item; + map[QString::fromUtf8(key)] = item; return IterationResult::ContinueSearch; });