qmake: optimize container usage in the json handling

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 <oswald.buddenhagen@theqtcompany.com>
(cherry picked from qtbase/69ab28031549ff1c9ceecdcb29197900e9bc4328)
Reviewed-by: Jake Petroules <jake.petroules@qt.io>
Reviewed-by: Anton Kudryavtsev <a.kudryavtsev@netris.ru>
This commit is contained in:
Anton Kudryavtsev
2016-02-01 10:42:04 +03:00
committed by Oswald Buddenhagen
parent 3f4e40e3c1
commit f71bc5ad9e

View File

@@ -296,7 +296,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);
}
@@ -305,10 +307,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)