Skip to content

Commit

Permalink
QFactoryLoader: use the new UTF-8 QCborStreamReader API for performance
Browse files Browse the repository at this point in the history
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
intel/tinycbor#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>
  • Loading branch information
thiagomacieira committed Nov 28, 2023
1 parent 878e334 commit b2d2404
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions src/corelib/plugin/qfactoryloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
});

Expand Down

0 comments on commit b2d2404

Please sign in to comment.