Optimize QJsonObject::operator==

The old code iterated through one QJsonObject and looked up the key
in the other, comparing the values. It had linearithmic complexity,
and created one QString 'key' per element.

Since the entries in a QJsonObject are lexicographically ordered,
we can, however, just walk through the two objects in lock-step and
compare corresponding entries (at the same index) with each other.

Doing so saves O(N) QString creations and QJsonObject::value()
calls, and makes operator== linear in the number of elements.

Change-Id: Ib46ee0c1008b7f114454e282b6bd2bfcdbe59e2a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2016-05-20 23:56:19 +02:00
parent f9c60038bf
commit 862fa24179
2 changed files with 4 additions and 2 deletions

View File

@ -670,6 +670,8 @@ public:
bool operator >=(const Entry &other) const;
};
inline bool operator!=(const Entry &lhs, const Entry &rhs) { return !(lhs == rhs); }
inline bool Entry::operator >=(const QString &key) const
{
if (value.latinKey)

View File

@ -545,8 +545,8 @@ bool QJsonObject::operator==(const QJsonObject &other) const
for (uint i = 0; i < o->length; ++i) {
QJsonPrivate::Entry *e = o->entryAt(i);
QJsonValue v(d, o, e->value);
if (other.value(e->key()) != v)
QJsonPrivate::Entry *oe = other.o->entryAt(i);
if (*e != *oe || QJsonValue(d, o, e->value) != QJsonValue(other.d, other.o, oe->value))
return false;
}