From 79cbdb503aec471cfb1dfcc7b36867490e4ca7ee Mon Sep 17 00:00:00 2001 From: Thomas McGuire Date: Tue, 2 Oct 2012 09:40:42 +0200 Subject: Use QVarLengthArray when creating the connectNotify() argument This gets rid of the heap allocation of the QByteArray. This change is not needed in Qt5, as there, QMetaMethod is used as the argument, and therefore there is no need to construct a SIGNAL-compatible string in memory. Change-Id: Ie2023aeb99bc8f792d437ec604e9989a5efe456b Reviewed-by: Alan Alpert <416365416c@gmail.com> --- src/corelib/kernel/qobject.cpp | 8 ++++---- src/corelib/kernel/qobject_p.h | 21 ++++++++++++++------- src/declarative/qml/qdeclarativeboundsignal.cpp | 4 +++- src/declarative/qml/qdeclarativenotifier_p.h | 4 +++- src/declarative/qml/qdeclarativeproperty.cpp | 4 +++- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index 8961e34..9008fb8 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -54,7 +54,6 @@ #include #include #include -#include #include #include #include @@ -2692,7 +2691,8 @@ bool QObject::connect(const QObject *sender, const QMetaMethod &signal, return false; } - QByteArray signalSignature = QObjectPrivate::signalSignature(signal); + QVarLengthArray signalSignature; + QObjectPrivate::signalSignature(signal, &signalSignature); { QByteArray methodSignature; @@ -2981,9 +2981,9 @@ bool QObject::disconnect(const QObject *sender, const QMetaMethod &signal, } } - QByteArray signalSignature; + QVarLengthArray signalSignature; if (signal.mobj) - signalSignature = QObjectPrivate::signalSignature(signal); + QObjectPrivate::signalSignature(signal, &signalSignature); { QByteArray methodSignature; diff --git a/src/corelib/kernel/qobject_p.h b/src/corelib/kernel/qobject_p.h index 6521a4f..7c56f63 100644 --- a/src/corelib/kernel/qobject_p.h +++ b/src/corelib/kernel/qobject_p.h @@ -62,6 +62,7 @@ #include "QtCore/qreadwritelock.h" #include "QtCore/qvariant.h" #include "QtCore/qmetaobject.h" +#include "QtCore/qvarlengtharray.h" QT_BEGIN_NAMESPACE @@ -185,7 +186,8 @@ public: inline void connectNotify(const char *signal); inline void disconnectNotify(const char *signal); - static inline QByteArray signalSignature(const QMetaMethod &signal); + static inline void signalSignature(const QMetaMethod &signal, + QVarLengthArray *result); public: QString objectName; @@ -247,13 +249,18 @@ inline void QObjectPrivate::disconnectNotify(const char *signal) q_ptr->disconnectNotify(signal); } -inline QByteArray QObjectPrivate::signalSignature(const QMetaMethod &signal) +inline void QObjectPrivate::signalSignature(const QMetaMethod &signal, + QVarLengthArray *result) { - QByteArray result; - result.reserve(qstrlen(signal.signature())+1); - result.append((char)(QSIGNAL_CODE + '0')); - result.append(signal.signature()); - return result; + Q_ASSERT(result); + const int signatureLength = qstrlen(signal.signature()); + if (signatureLength == 0) { + result->append((char)0); + return; + } + result->reserve(signatureLength + 2); + result->append((char)(QSIGNAL_CODE + '0')); + result->append(signal.signature(), signatureLength + 1); } inline QObjectPrivate::Sender *QObjectPrivate::setCurrentSender(QObject *receiver, diff --git a/src/declarative/qml/qdeclarativeboundsignal.cpp b/src/declarative/qml/qdeclarativeboundsignal.cpp index d8aeef6..9bccb42 100644 --- a/src/declarative/qml/qdeclarativeboundsignal.cpp +++ b/src/declarative/qml/qdeclarativeboundsignal.cpp @@ -137,7 +137,9 @@ QDeclarativeBoundSignal::~QDeclarativeBoundSignal() void QDeclarativeBoundSignal::disconnect() { QObjectPrivate * const priv = QObjectPrivate::get(m_scope); - priv->disconnectNotify(QObjectPrivate::signalSignature(m_signal)); + QVarLengthArray signalSignature; + QObjectPrivate::signalSignature(m_signal, &signalSignature); + priv->disconnectNotify(signalSignature.constData()); } int QDeclarativeBoundSignal::index() const diff --git a/src/declarative/qml/qdeclarativenotifier_p.h b/src/declarative/qml/qdeclarativenotifier_p.h index 0e38ec1..f15efb6 100644 --- a/src/declarative/qml/qdeclarativenotifier_p.h +++ b/src/declarative/qml/qdeclarativenotifier_p.h @@ -217,7 +217,9 @@ void QDeclarativeNotifierEndpoint::disconnect() QMetaObject::disconnectOne(s->source, s->sourceSignal, target, targetMethod); QObjectPrivate * const priv = QObjectPrivate::get(s->source); const QMetaMethod signal = s->source->metaObject()->method(s->sourceSignal); - priv->disconnectNotify(QObjectPrivate::signalSignature(signal)); + QVarLengthArray signalSignature; + QObjectPrivate::signalSignature(signal, &signalSignature); + priv->disconnectNotify(signalSignature.constData()); s->source = 0; } } else if (type == NotifierType) { diff --git a/src/declarative/qml/qdeclarativeproperty.cpp b/src/declarative/qml/qdeclarativeproperty.cpp index e6b9775..ce4122a 100644 --- a/src/declarative/qml/qdeclarativeproperty.cpp +++ b/src/declarative/qml/qdeclarativeproperty.cpp @@ -1634,7 +1634,9 @@ bool QDeclarativePropertyPrivate::connect(QObject *sender, int signal_index, const QMetaMethod signal = sender->metaObject()->method(signal_index); QObjectPrivate * const senderPriv = QObjectPrivate::get(sender); - senderPriv->connectNotify(QObjectPrivate::signalSignature(signal)); + QVarLengthArray signalSignature; + QObjectPrivate::signalSignature(signal, &signalSignature); + senderPriv->connectNotify(signalSignature.constData()); return QMetaObject::connect(sender, signal_index, receiver, method_index, type, types); } -- cgit v0.12