diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2012-01-06 16:23:59 +0100 |
---|---|---|
committer | Jamey Hicks <jamey.hicks@nokia.com> | 2012-01-06 17:33:36 +0100 |
commit | b80bc3f04d650cf8a38429dfb5473daf38a0a4b8 (patch) | |
tree | 64845e532cf0d554d7e7494627d7d067e752c63f /src | |
parent | 09e23806546ebeea0f42aa89583f8e188cc06426 (diff) |
Reuse the table entry when inserting an existing key
This saves us some time and reallocations. Ideally,
we'd also reuse the entry where possible, but this
helps already.
Change-Id: I4140599483309cf0c1fa90e220cb3c890e7184a3
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/qjson.cpp | 12 | ||||
-rw-r--r-- | src/qjson_p.h | 1 | ||||
-rw-r--r-- | src/qjsonobject.cpp | 11 |
3 files changed, 7 insertions, 17 deletions
diff --git a/src/qjson.cpp b/src/qjson.cpp index b77dcf2..6d1aa70 100644 --- a/src/qjson.cpp +++ b/src/qjson.cpp @@ -171,18 +171,6 @@ int Object::indexOf(const QString &key) 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 { if (tableOffset + length*sizeof(offset) > size) diff --git a/src/qjson_p.h b/src/qjson_p.h index 5ddee90..06883fc 100644 --- a/src/qjson_p.h +++ b/src/qjson_p.h @@ -421,7 +421,6 @@ 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; }; diff --git a/src/qjsonobject.cpp b/src/qjsonobject.cpp index 2885277..243b1f2 100644 --- a/src/qjsonobject.cpp +++ b/src/qjsonobject.cpp @@ -188,13 +188,16 @@ void JsonObject::insert(const QString &key, const JsonValue &value) o->tableOffset = sizeof(Object); - int pos = o->insertKey(key); - if (pos < (int)o->length) { - o->removeItems(pos, 1); + int pos = o->indexOf(key); + int newIndex = 1; + if (pos >= 0) { ++d->compactionCounter; + newIndex = 0; + } else { + pos = o->length; } + o->reserveSpace(requiredSize, pos, newIndex); - o->reserveSpace(requiredSize, pos, 1); Entry *e = o->entryAt(pos); e->value.type = value.t; e->value.latinKey = latinKey; |