diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2011-12-07 12:28:44 +0100 |
---|---|---|
committer | Simons Kevin <kevin.simons@nokia.com> | 2011-12-07 13:17:46 +0100 |
commit | d02f9658c47599c4f07c269d18fbc503cafdffc2 (patch) | |
tree | f6ff37f4b866df3c65f1baf9064caeea774cd894 | |
parent | bb92eb093d289af7139fed33382bd0399ccadef8 (diff) |
Use 12-16 bytes less per string
Don't store a full QStringData, but simply
the length of the string.
Change-Id: I8b2b5a6eb7f065f2de6907955806340f5d99c0e8
Reviewed-by: Simons Kevin <kevin.simons@nokia.com>
-rw-r--r-- | src/qjson_p.h | 73 | ||||
-rw-r--r-- | src/qjsonobject.cpp | 5 | ||||
-rw-r--r-- | src/qjsonvalue.cpp | 4 | ||||
-rw-r--r-- | src/qjsonwriter.cpp | 8 |
4 files changed, 57 insertions, 33 deletions
diff --git a/src/qjson_p.h b/src/qjson_p.h index 491a409..e016493 100644 --- a/src/qjson_p.h +++ b/src/qjson_p.h @@ -66,19 +66,43 @@ static inline int alignedSize(int size) { return (size + 7) & ~7; } static inline int qStringSize(const QString &string) { - return sizeof(QStringData) + sizeof(ushort)*string.length(); + return (sizeof(ushort)*string.length() + 3) & ~3; } -static inline void createConstString(void *dest, const QString &s) +struct QStackString { - QStringData *data = static_cast<QStringData *>(dest); - data->ref.atomic.store(-1); - data->size = s.length(); - data->alloc = 0; - data->capacityReserved = false; - data->offset = 0; - memcpy((data + 1), s.constData(), sizeof(ushort)*s.length()); -} + QStackString(const ushort *utf16, int length) + : length(length), utf16(utf16) {} + int length; + const ushort *utf16; + + inline bool equals(const ushort *s, int slen) const + { + if (slen != length) + return false; + int l = length; + const ushort *a = utf16; + const ushort *b = s; + while (l-- && *a == *b) + a++,b++; + return (l == -1); + } + + bool operator ==(const QString &str) { + return equals((const ushort *)str.constData(), str.length()); + } + bool operator !=(const QString &str) { + return !equals((const ushort *)str.constData(), str.length()); + } + bool operator ==(const QStackString &str) { + return equals(str.utf16, str.length); + } + bool operator !=(const QStackString &str) { + return !equals(str.utf16, str.length); + } +}; + + struct Value { @@ -91,8 +115,8 @@ struct Value bool toBoolean() const; double toNumber() const; int toInt() const; - QString shallowString() const; - QString string() const; + QString toString() const; + QStackString shallowString() const; Array *array() const; Object *object() const; @@ -112,7 +136,8 @@ struct Value static inline Value *fromString(const QString &string) { Value *v = alloc(sizeof(Value) + qStringSize(string)); v->type = StringValue; - createConstString(v->data(), string); + v->val = string.length(); + memcpy(v->data(), string.constData(), sizeof(ushort)*string.length()); return v; } static inline Value *fromArray(const Array *a); @@ -123,15 +148,15 @@ struct Value struct Entry { uint size; offset valueOffset; - QStringData keyString; + uint keyLength; + ushort *keyData() const { return (ushort *)(this + 1); } + // keydata // value data follows key data - QString shallowKey() const { return *reinterpret_cast<const QConstStringData<1> *>(&keyString); } + QStackString shallowKey() const { return QStackString(keyData(), keyLength); } QString key() const { - QString str = shallowKey(); - str.detach(); - return str; + return QString((const QChar *)keyData(), keyLength); } Value *value() const { return reinterpret_cast<Value *>(((char *)this) + valueOffset); @@ -222,18 +247,16 @@ inline int Value::toInt() const return (int)*(double *)((char *)(this+1)); } -inline QString Value::shallowString() const +inline QString Value::toString() const { - Q_ASSERT(type == StringValue); - return *reinterpret_cast<const QConstStringData<1> *>((char *)(this+1)); + return QString((const QChar *)data(), val); } -inline QString Value::string() const +inline QStackString Value::shallowString() const { - QString str = shallowString(); - str.detach(); - return str; + return QStackString((ushort *)data(), val); } + inline Array *Value::array() const { Q_ASSERT(type == ArrayValue); diff --git a/src/qjsonobject.cpp b/src/qjsonobject.cpp index 79af761..da386b8 100644 --- a/src/qjsonobject.cpp +++ b/src/qjsonobject.cpp @@ -140,7 +140,7 @@ void JsonObject::insert(const QString &key, const JsonValue &value) int valueSize = v ? v->size : sizeof(Value); - int valueOffset = sizeof(Entry) + sizeof(QStringData) + key.length()*sizeof(ushort); + int valueOffset = sizeof(Entry) + qStringSize(key); int requiredSize = valueOffset + valueSize; detach(requiredSize + sizeof(offset)); // offset for the new index entry @@ -155,7 +155,8 @@ void JsonObject::insert(const QString &key, const JsonValue &value) o->reserveSpace(requiredSize, pos, 1); Entry *e = o->entryAt(pos); e->size = requiredSize; - createConstString(&e->keyString, key); + e->keyLength = key.length(); + memcpy(e->keyData(), key.constData(), sizeof(ushort)*key.length()); e->valueOffset = valueOffset; if (v) { memcpy(e->value(), v, v->size); diff --git a/src/qjsonvalue.cpp b/src/qjsonvalue.cpp index 2dc6008..0241482 100644 --- a/src/qjsonvalue.cpp +++ b/src/qjsonvalue.cpp @@ -144,7 +144,7 @@ QVariant JsonValue::toVariant() const case NumberValue: return v->toNumber(); case StringValue: - return v->string(); + return v->toString(); case ArrayValue: return JsonArray(d, v->array()).toVariantList(); case ObjectValue: @@ -236,7 +236,7 @@ QString JsonValue::toString() const if (v->type != StringValue) return QString(); - return v->string(); + return v->toString(); } JsonArray JsonValue::toArray() const diff --git a/src/qjsonwriter.cpp b/src/qjsonwriter.cpp index 133403d..3ebc192 100644 --- a/src/qjsonwriter.cpp +++ b/src/qjsonwriter.cpp @@ -26,16 +26,16 @@ static inline uchar hexdig(uint u) return (u < 0xa ? '0' + u : 'a' + u - 0xa); } -static QByteArray escapedString(const QString &s) +static QByteArray escapedString(const QStackString &s) { const uchar replacement = '?'; - QByteArray ba(s.length(), Qt::Uninitialized); + QByteArray ba(s.length, Qt::Uninitialized); uchar *cursor = (uchar *)ba.data(); const uchar *ba_end = cursor + ba.length(); - const QChar *ch = s.constData(); - const QChar *end = ch + s.length(); + const QChar *ch = (const QChar *)s.utf16; + const QChar *end = ch + s.length; int surrogate_high = -1; |