Significantly speed up insertion into QJsonObject/Array
The code was only allocating memory for the next insertion leading to a reallocation of the whole data for every single insertion. The code now reserves some space and uses a decent growth strategy to avoid repeated reallocs. Change-Id: I48b0feab71ba8ca73e7037f8460080f198b2f009 Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
This commit is contained in:
parent
8dd18751e1
commit
349c0de091
@ -241,14 +241,6 @@ bool Entry::operator ==(const QString &key) const
|
|||||||
return (shallowKey() == key);
|
return (shallowKey() == key);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Entry::operator >=(const QString &key) const
|
|
||||||
{
|
|
||||||
if (value.latinKey)
|
|
||||||
return (shallowLatin1Key() >= key);
|
|
||||||
else
|
|
||||||
return (shallowKey() >= key);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Entry::operator ==(const Entry &other) const
|
bool Entry::operator ==(const Entry &other) const
|
||||||
{
|
{
|
||||||
if (value.latinKey) {
|
if (value.latinKey) {
|
||||||
|
@ -620,15 +620,24 @@ public:
|
|||||||
|
|
||||||
bool operator ==(const QString &key) const;
|
bool operator ==(const QString &key) const;
|
||||||
inline bool operator !=(const QString &key) const { return !operator ==(key); }
|
inline bool operator !=(const QString &key) const { return !operator ==(key); }
|
||||||
bool operator >=(const QString &key) const;
|
inline bool operator >=(const QString &key) const;
|
||||||
|
|
||||||
bool operator ==(const Entry &other) const;
|
bool operator ==(const Entry &other) const;
|
||||||
bool operator >=(const Entry &other) const;
|
bool operator >=(const Entry &other) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline bool Entry::operator >=(const QString &key) const
|
||||||
|
{
|
||||||
|
if (value.latinKey)
|
||||||
|
return (shallowLatin1Key() >= key);
|
||||||
|
else
|
||||||
|
return (shallowKey() >= key);
|
||||||
|
}
|
||||||
|
|
||||||
inline bool operator <(const QString &key, const Entry &e)
|
inline bool operator <(const QString &key, const Entry &e)
|
||||||
{ return e >= key; }
|
{ return e >= key; }
|
||||||
|
|
||||||
|
|
||||||
class Header {
|
class Header {
|
||||||
public:
|
public:
|
||||||
qle_uint tag; // 'qbjs'
|
qle_uint tag; // 'qbjs'
|
||||||
@ -735,7 +744,15 @@ public:
|
|||||||
|
|
||||||
Data *clone(Base *b, int reserve = 0)
|
Data *clone(Base *b, int reserve = 0)
|
||||||
{
|
{
|
||||||
int size = sizeof(Header) + b->size + reserve;
|
int size = sizeof(Header) + b->size;
|
||||||
|
if (ref.load() == 1 && alloc >= size + reserve)
|
||||||
|
return this;
|
||||||
|
|
||||||
|
if (reserve) {
|
||||||
|
if (reserve < 128)
|
||||||
|
reserve = 128;
|
||||||
|
size = qMax(size + reserve, size *2);
|
||||||
|
}
|
||||||
char *raw = (char *)malloc(size);
|
char *raw = (char *)malloc(size);
|
||||||
Q_CHECK_PTR(raw);
|
Q_CHECK_PTR(raw);
|
||||||
memcpy(raw + sizeof(Header), b, b->size);
|
memcpy(raw + sizeof(Header), b, b->size);
|
||||||
|
@ -58,8 +58,12 @@ private Q_SLOTS:
|
|||||||
void parseNumbers();
|
void parseNumbers();
|
||||||
void parseJson();
|
void parseJson();
|
||||||
void parseJsonToVariant();
|
void parseJsonToVariant();
|
||||||
|
|
||||||
void toByteArray();
|
void toByteArray();
|
||||||
void fromByteArray();
|
void fromByteArray();
|
||||||
|
|
||||||
|
void jsonObjectInsert();
|
||||||
|
void variantMapInsert();
|
||||||
};
|
};
|
||||||
|
|
||||||
BenchmarkQtBinaryJson::BenchmarkQtBinaryJson(QObject *parent) : QObject(parent)
|
BenchmarkQtBinaryJson::BenchmarkQtBinaryJson(QObject *parent) : QObject(parent)
|
||||||
@ -159,6 +163,30 @@ void BenchmarkQtBinaryJson::fromByteArray()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BenchmarkQtBinaryJson::jsonObjectInsert()
|
||||||
|
{
|
||||||
|
QJsonObject object;
|
||||||
|
QString test(QStringLiteral("testString"));
|
||||||
|
QJsonValue value(1.5);
|
||||||
|
|
||||||
|
QBENCHMARK {
|
||||||
|
for (int i = 0; i < 1000; i++)
|
||||||
|
object.insert("testkey_" + i, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BenchmarkQtBinaryJson::variantMapInsert()
|
||||||
|
{
|
||||||
|
QVariantMap object;
|
||||||
|
QString test(QStringLiteral("testString"));
|
||||||
|
QVariant variantValue(1.5);
|
||||||
|
|
||||||
|
QBENCHMARK {
|
||||||
|
for (int i = 0; i < 1000; i++)
|
||||||
|
object.insert("testkey_" + i, variantValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QTEST_MAIN(BenchmarkQtBinaryJson)
|
QTEST_MAIN(BenchmarkQtBinaryJson)
|
||||||
#include "tst_bench_qtbinaryjson.moc"
|
#include "tst_bench_qtbinaryjson.moc"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user