Optimize non-const overload of QJsonObject::operator[]

Refactored parts of insert() into a new private method insertAt(), which
can also be called by operator[]() to avoid a redundant key lookup.

This is in preparation for overloading QJsonObject's non-const methods
on QLatin1String.

As a bonus, this also avoids a redundant key lookup in
QJsonValueRef::operator=().

Change-Id: Ic481981d838e50bc55fb8e7844536749781899ce
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Anton Kudryavtsev <antkudr@mail.ru>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Mat Sutcliffe 2019-07-02 02:16:41 +01:00
parent ce86c3373e
commit 8010e906d3
2 changed files with 14 additions and 6 deletions

View File

@ -446,11 +446,10 @@ QJsonValue QJsonObject::operator [](const QString &key) const
*/
QJsonValueRef QJsonObject::operator [](const QString &key)
{
// ### somewhat inefficient, as we lookup the key twice if it doesn't yet exist
bool keyExists = false;
int index = o ? o->indexOf(key, &keyExists) : -1;
int index = o ? o->indexOf(key, &keyExists) : 0;
if (!keyExists) {
iterator i = insert(key, QJsonValue());
iterator i = insertAt(index, key, QJsonValue(), false);
index = i.i;
}
return QJsonValueRef(this, index);
@ -485,6 +484,16 @@ QJsonObject::iterator QJsonObject::insert(const QString &key, const QJsonValue &
remove(key);
return end();
}
bool keyExists = false;
int pos = o ? o->indexOf(key, &keyExists) : 0;
return insertAt(pos, key, value, keyExists);
}
/*!
\internal
*/
QJsonObject::iterator QJsonObject::insertAt(int pos, const QString &key, const QJsonValue &value, bool keyExists)
{
QJsonValue val = value;
bool latinOrIntValue;
@ -500,8 +509,6 @@ QJsonObject::iterator QJsonObject::insert(const QString &key, const QJsonValue &
if (!o->length)
o->tableOffset = sizeof(QJsonPrivate::Object);
bool keyExists = false;
int pos = o->indexOf(key, &keyExists);
if (keyExists)
++d->compactionCounter;
@ -1289,7 +1296,7 @@ void QJsonObject::setValueAt(int i, const QJsonValue &val)
Q_ASSERT(o && i >= 0 && i < (int)o->length);
QJsonPrivate::Entry *e = o->entryAt(i);
insert(e->key(), val);
insertAt(i, e->key(), val, true);
}
uint qHash(const QJsonObject &object, uint seed)

View File

@ -251,6 +251,7 @@ private:
QString keyAt(int i) const;
QJsonValue valueAt(int i) const;
void setValueAt(int i, const QJsonValue &val);
iterator insertAt(int i, const QString &key, const QJsonValue &val, bool exists);
QJsonPrivate::Data *d;
QJsonPrivate::Object *o;