summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2016-05-20 23:56:19 +0200
committerMarc Mutz <marc.mutz@kdab.com>2016-06-08 04:38:20 +0000
commit862fa24179505ef725ff78bb64bdabd54bd00c95 (patch)
tree3f6012291d492a461b6742eb93b2208649a24df2 /src/corelib
parentf9c60038bf098015172998c2a216dd02672cd96b (diff)
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>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/json/qjson_p.h2
-rw-r--r--src/corelib/json/qjsonobject.cpp4
2 files changed, 4 insertions, 2 deletions
diff --git a/src/corelib/json/qjson_p.h b/src/corelib/json/qjson_p.h
index c52a37ba2b..b7de24d165 100644
--- a/src/corelib/json/qjson_p.h
+++ b/src/corelib/json/qjson_p.h
@@ -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)
diff --git a/src/corelib/json/qjsonobject.cpp b/src/corelib/json/qjsonobject.cpp
index b83c8dd19a..f5fd76eac0 100644
--- a/src/corelib/json/qjsonobject.cpp
+++ b/src/corelib/json/qjsonobject.cpp
@@ -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;
}