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>
bb10
Marc Mutz 2016-05-04 14:51:37 +02:00
parent 0b1b06ffc0
commit 4a4b377cd0
2 changed files with 18 additions and 1 deletions

View File

@ -563,6 +563,22 @@ QString QJsonValue::toString(const QString &defaultValue) const
return QString(holder);
}
/*!
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.

View File

@ -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;