diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2016-05-04 14:51:37 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2016-05-09 08:10:13 +0000 |
commit | 4a4b377cd0912615477083d0f597907a51d025a9 (patch) | |
tree | 46def6af310bd47b60191af504f2befd088b14ed /src | |
parent | 0b1b06ffc0a334263c01e32e5bfb50b4d96ba70d (diff) |
QJsonValue: don't create a temporary QString on every toString() invocation
The vast majority of users call toString() without the optional
defaultValue. So do it like the toArray() and toObject() methods
and split toString() into two overloads, so the common case no
longer needs to pass a temporaray QString.
Saves ~1.4 and ~1KiB in QtCore and QtGui text size, resp., on
optimized GCC 6.0 Linux AMD64 builds, even though we added a new
function to QtCore, too.
Change-Id: Ibe02397ca49ce11fdb58f5c5fc69e909bf94c1c6
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/json/qjsonvalue.cpp | 16 | ||||
-rw-r--r-- | src/corelib/json/qjsonvalue.h | 3 |
2 files changed, 18 insertions, 1 deletions
diff --git a/src/corelib/json/qjsonvalue.cpp b/src/corelib/json/qjsonvalue.cpp index 36df146332..b21f59ae35 100644 --- a/src/corelib/json/qjsonvalue.cpp +++ b/src/corelib/json/qjsonvalue.cpp @@ -564,6 +564,22 @@ QString QJsonValue::toString(const QString &defaultValue) const } /*! + Converts the value to a QString and returns it. + + If type() is not String, a null QString will be returned. + + \sa QString::isNull() + */ +QString QJsonValue::toString() const +{ + if (t != String) + return QString(); + stringData->ref.ref(); // the constructor below doesn't add a ref. + QStringDataPtr holder = { stringData }; + return QString(holder); +} + +/*! Converts the value to an array and returns it. If type() is not Array, the \a defaultValue will be returned. diff --git a/src/corelib/json/qjsonvalue.h b/src/corelib/json/qjsonvalue.h index 8b6221ea4c..1ce7f745e0 100644 --- a/src/corelib/json/qjsonvalue.h +++ b/src/corelib/json/qjsonvalue.h @@ -107,7 +107,8 @@ public: bool toBool(bool defaultValue = false) const; int toInt(int defaultValue = 0) const; double toDouble(double defaultValue = 0) const; - QString toString(const QString &defaultValue = QString()) const; + QString toString() const; + QString toString(const QString &defaultValue) const; QJsonArray toArray() const; QJsonArray toArray(const QJsonArray &defaultValue) const; QJsonObject toObject() const; |