From 4a4b377cd0912615477083d0f597907a51d025a9 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 4 May 2016 14:51:37 +0200 Subject: 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 --- src/corelib/json/qjsonvalue.cpp | 16 ++++++++++++++++ src/corelib/json/qjsonvalue.h | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'src/corelib/json') 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 @@ -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. 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; -- cgit v1.2.3