Skip to content

Commit

Permalink
qmake: optimize container usage in the json handling.
Browse files Browse the repository at this point in the history
Iterate only once over QJsonObject, create key list by
existing loop instead of create by QJsonObject::keys(),
which contains internal loop.
In common case if loop's statement is lightweight,
then effect of optimization is significant, and vice versa.
Also make addJsonArray() and addJsonObject() functions
more homogeneous.

Use reserve to optimize memory allocation.

Change-Id: Id122cd1becfd34bb06640876b1c79e1d396d2a6b
Reviewed-by: Oswald Buddenhagen <[email protected]>
  • Loading branch information
Anton Kudryavtsev committed Feb 4, 2016
1 parent 20a2ba6 commit 69ab280
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions qmake/library/qmakebuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ static void insertJsonKeyValue(const QString &key, const QStringList &values, Pr
static void addJsonArray(const QJsonArray &array, const QString &keyPrefix, ProValueMap *map)
{
QStringList keys;
for (int i = 0; i < array.count(); ++i) {
const int size = array.count();
keys.reserve(size);
for (int i = 0; i < size; ++i) {
keys.append(QString::number(i));
addJsonValue(array.at(i), keyPrefix + QString::number(i), map);
}
Expand All @@ -304,10 +306,14 @@ static void addJsonArray(const QJsonArray &array, const QString &keyPrefix, ProV

static void addJsonObject(const QJsonObject &object, const QString &keyPrefix, ProValueMap *map)
{
for (auto it = object.begin(), end = object.end(); it != end; ++it)
addJsonValue(it.value(), keyPrefix + it.key(), map);

insertJsonKeyValue(keyPrefix + QLatin1String("_KEYS_"), object.keys(), map);
QStringList keys;
keys.reserve(object.size());
for (auto it = object.begin(), end = object.end(); it != end; ++it) {
const QString key = it.key();
keys.append(key);
addJsonValue(it.value(), keyPrefix + key, map);
}
insertJsonKeyValue(keyPrefix + QLatin1String("_KEYS_"), keys, map);
}

static void addJsonValue(const QJsonValue &value, const QString &keyPrefix, ProValueMap *map)
Expand Down

0 comments on commit 69ab280

Please sign in to comment.