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 <eirik.aavitsland@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
bb10
Thiago Macieira 2023-11-13 19:53:18 -08:00
parent 878e3342e1
commit b2d24044c2
1 changed files with 7 additions and 22 deletions

View File

@ -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 <typename F> 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<std::decay_t<decltype(key)>>)
map[int(key)] = item;
else
map[key] = item;
map[QString::fromUtf8(key)] = item;
return IterationResult::ContinueSearch;
});