Skip to content

Commit e2d19b9

Browse files
author
Allan Sandfeld Jensen
committed
Micro-optimize to ensure named return value optimization
Rewrite a few often used header functions to ensure they get return value optimization. Change-Id: I316fde21f63fedfd9cbae50855007234b0de9b70 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
1 parent a39047e commit e2d19b9

File tree

6 files changed

+53
-31
lines changed

6 files changed

+53
-31
lines changed

src/corelib/kernel/qproperty.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,11 @@ class QUntypedBindable
766766
// afterwards.
767767
if (!(iface->getBinding && iface->setBinding))
768768
return QUntypedPropertyBinding {};
769-
QUntypedPropertyBinding binding = iface->getBinding(data);
770-
iface->setBinding(data, QUntypedPropertyBinding{});
771-
return binding;
769+
return [&] {
770+
QUntypedPropertyBinding binding = iface->getBinding(data);
771+
iface->setBinding(data, QUntypedPropertyBinding{});
772+
return binding;
773+
}();
772774
}
773775

774776
void observe(QPropertyObserver *observer) const

src/corelib/kernel/qproperty_p.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,12 @@ class Q_CORE_EXPORT QPropertyBindingPrivate : public QtPrivate::RefCounted
387387
{
388388
if (!hasCustomVTable())
389389
return location;
390-
QPropertyBindingSourceLocation result;
391-
constexpr auto msg = "Custom location";
392-
result.fileName = msg;
393-
return result;
390+
return []() {
391+
constexpr auto msg = "Custom location";
392+
QPropertyBindingSourceLocation result;
393+
result.fileName = msg;
394+
return result;
395+
}();
394396
}
395397
QPropertyBindingError bindingError() const { return m_error; }
396398
QMetaType valueMetaType() const { return metaType; }

src/corelib/kernel/qvariant.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,15 @@ inline void swap(QVariant &value1, QVariant &value2) noexcept
762762

763763
#ifndef QT_MOC
764764

765+
namespace QtPrivate {
766+
template<typename T> inline T qvariant_cast_qmetatype_converted(const QVariant &v, QMetaType targetType)
767+
{
768+
T t{};
769+
QMetaType::convert(v.metaType(), v.constData(), targetType, &t);
770+
return t;
771+
}
772+
} // namespace QtPrivate
773+
765774
template<typename T> inline T qvariant_cast(const QVariant &v)
766775
{
767776
QMetaType targetType = QMetaType::fromType<T>();
@@ -774,9 +783,7 @@ template<typename T> inline T qvariant_cast(const QVariant &v)
774783
return v.d.get<nonConstT>();
775784
}
776785

777-
T t{};
778-
QMetaType::convert(v.metaType(), v.constData(), targetType, &t);
779-
return t;
786+
return QtPrivate::qvariant_cast_qmetatype_converted<T>(v, targetType);
780787
}
781788

782789
template<typename T> inline T qvariant_cast(QVariant &&v)
@@ -803,9 +810,7 @@ template<typename T> inline T qvariant_cast(QVariant &&v)
803810
return v.d.get<nonConstT>();
804811
}
805812

806-
T t{};
807-
QMetaType::convert(v.metaType(), v.constData(), targetType, &t);
808-
return t;
813+
return QtPrivate::qvariant_cast_qmetatype_converted<T>(v, targetType);
809814
}
810815

811816
# ifndef QT_NO_VARIANT

src/corelib/text/qstringbuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ class QStringBuilder : public QStringBuilderBase<QStringBuilder<A, B>,
100100
// so we're special casing this one out, QTBUG-114206
101101
return T();
102102
}
103-
104-
const qsizetype len = Concatenable::size(*this);
103+
return convertToImpl<T>(Concatenable::size(*this));
104+
}
105+
template <typename T> T convertToImpl(const qsizetype len) const
106+
{
105107
T s(len, Qt::Uninitialized);
106108

107109
// Using data_ptr() here (private API) so we can bypass the

src/corelib/text/qstringconverter.h

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,24 @@ class QStringEncoder : public QStringConverter
7070
FinalizeResult finalize() { return finalize(nullptr, 0); }
7171

7272
private:
73-
QByteArray encodeAsByteArray(QStringView in)
73+
QByteArray invalidateAndReturnNull()
74+
{
75+
// ensure that hasError returns true
76+
state.invalidChars = 1;
77+
return QByteArray();
78+
}
79+
QByteArray encodeAsByteArrayImpl(QStringView in)
7480
{
75-
if (!iface) {
76-
// ensure that hasError returns true
77-
state.invalidChars = 1;
78-
return {};
79-
}
8081
QByteArray result(iface->fromUtf16Len(in.size()), Qt::Uninitialized);
8182
char *out = result.data();
8283
out = iface->fromUtf16(out, in, &state);
8384
result.truncate(out - result.constData());
8485
return result;
8586
}
86-
87+
QByteArray encodeAsByteArray(QStringView in)
88+
{
89+
return iface ? encodeAsByteArrayImpl(in) : invalidateAndReturnNull();
90+
}
8791
};
8892

8993
class QStringDecoder : public QStringConverter
@@ -153,18 +157,23 @@ class QStringDecoder : public QStringConverter
153157
Q_CORE_EXPORT static QStringDecoder decoderForHtml(QByteArrayView data);
154158

155159
private:
156-
QString decodeAsString(QByteArrayView in)
160+
QString invalidateAndReturnNull()
161+
{
162+
// ensure that hasError returns true
163+
state.invalidChars = 1;
164+
return QString();
165+
}
166+
QString decodeAsStringImpl(QByteArrayView in)
157167
{
158-
if (!iface) {
159-
// ensure that hasError returns true
160-
state.invalidChars = 1;
161-
return {};
162-
}
163168
QString result(iface->toUtf16Len(in.size()), Qt::Uninitialized);
164169
const QChar *out = iface->toUtf16(result.data(), in, &state);
165170
result.truncate(out - result.constData());
166171
return result;
167172
}
173+
QString decodeAsString(QByteArrayView in)
174+
{
175+
return iface ? decodeAsStringImpl(in) : invalidateAndReturnNull();
176+
}
168177
};
169178

170179

src/corelib/tools/qhash.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,9 +1031,11 @@ class QHash
10311031

10321032
if (it.isUnused())
10331033
return T();
1034-
T value = it.node()->takeValue();
1035-
d->erase(it);
1036-
return value;
1034+
return [&] {
1035+
T value = it.node()->takeValue();
1036+
d->erase(it);
1037+
return value;
1038+
}();
10371039
}
10381040

10391041
public:

0 commit comments

Comments
 (0)