diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2012-01-04 12:17:57 +0100 |
---|---|---|
committer | Jamey Hicks <jamey.hicks@nokia.com> | 2012-01-04 14:54:52 +0100 |
commit | 0778b49ba94cecb7d8e212b0014d3385527a99c9 (patch) | |
tree | 9c8dd26ebc08359af3ecca75372480e0b3a134e5 /src | |
parent | 66e156b99086c496319d0521910c68096879cef0 (diff) |
Reuse the index in the object for existing keys
This makes the code slightly more performant when
inserting items into the object.
Change-Id: I316929b0f23d4f17f3934b9dd8eb58bb6236777f
Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/qjson.cpp | 29 | ||||
-rw-r--r-- | src/qjson_p.h | 3 | ||||
-rw-r--r-- | src/qjsonobject.cpp | 7 |
3 files changed, 29 insertions, 10 deletions
diff --git a/src/qjson.cpp b/src/qjson.cpp index 8261703..b99640d 100644 --- a/src/qjson.cpp +++ b/src/qjson.cpp @@ -165,17 +165,23 @@ int Object::indexOf(const QString &key) { for (int i = 0; i < (int)length; ++i) { Entry *e = entryAt(i); - if (e->value.latinKey) { - if (e->shallowLatin1Key() == key) - return i; - } else { - if (e->shallowKey() == key) - return i; - } + if (e->matchesKey(key)) + return i; } return -1; } +int Object::insertKey(const QString &key) +{ + // assumes we already have space for another entry in the index table + for (int i = 0; i < (int)length; ++i) { + Entry *e = entryAt(i); + if (e->matchesKey(key)) + return i; + } + return length; +} + bool Object::isValid() const { @@ -211,6 +217,15 @@ bool Array::isValid() const } +bool Entry::matchesKey(const QString &key) +{ + if (value.latinKey) + return (shallowLatin1Key() == key); + else + return (shallowKey() == key); +} + + int Value::usedStorage(const Base *b) const { int s = 0; diff --git a/src/qjson_p.h b/src/qjson_p.h index f0aa5a3..9a49948 100644 --- a/src/qjson_p.h +++ b/src/qjson_p.h @@ -322,6 +322,7 @@ struct Object : public Base return reinterpret_cast<Entry *>(((char *)this) + table()[i]); } int indexOf(const QString &key); + int insertKey(const QString &key); bool isValid() const; }; @@ -421,6 +422,8 @@ struct Entry { int length = *(int *) ((const char *)this + sizeof(Entry)); return QString((const QChar *)keyData(), length); } + + bool matchesKey(const QString &key); }; struct Header { diff --git a/src/qjsonobject.cpp b/src/qjsonobject.cpp index 5eefd1c..e75dccc 100644 --- a/src/qjsonobject.cpp +++ b/src/qjsonobject.cpp @@ -187,10 +187,11 @@ void JsonObject::insert(const QString &key, const JsonValue &value) if (!o->length) o->tableOffset = sizeof(Object); - // ### this is crude! - remove(key); - int pos = o->length; + int pos = o->insertKey(key); + if (pos < o->length) + ++d->compactionCounter; + o->reserveSpace(requiredSize, pos, 1); Entry *e = o->entryAt(pos); e->value.type = value.t; |