DBus: use QStringRef to optimize memory allocation

Replace substring functions that return QString with
corresponding functions that return QStringRef where
it's possible.

Create QString from QStringRef only where necessary.

Add overloaded functions with QStringRef arg
in QDBusUtil:
- isValidUniqueConnectionName()
- isValidMemberName()
- isValidPartOfObjectPath()

Change-Id: I4a24a298702728ba7d3a65c39e25c3a9c759e07f
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Anton Kudryavtsev 2016-03-31 15:03:12 +03:00
parent 6ebe7e8f37
commit c8e4d15004
5 changed files with 60 additions and 39 deletions

View File

@ -44,6 +44,7 @@
#include <qdebug.h>
#include <qcoreapplication.h>
#include <qstringlist.h>
#include <qvector.h>
#include <qtimer.h>
#include <qthread.h>
@ -909,8 +910,8 @@ bool QDBusConnection::registerObject(const QString &path, const QString &interfa
if (!d || !d->connection || !object || !options || !QDBusUtil::isValidObjectPath(path))
return false;
QStringList pathComponents = path.split(QLatin1Char('/'));
if (pathComponents.last().isEmpty())
auto pathComponents = path.splitRef(QLatin1Char('/'));
if (pathComponents.constLast().isEmpty())
pathComponents.removeLast();
QDBusWriteLocker locker(RegisterObjectAction, d);
@ -965,7 +966,7 @@ bool QDBusConnection::registerObject(const QString &path, const QString &interfa
}
} else {
// add entry
node = node->children.insert(it, pathComponents.at(i));
node = node->children.insert(it, pathComponents.at(i).toString());
}
// iterate
@ -1017,8 +1018,8 @@ QObject *QDBusConnection::objectRegisteredAt(const QString &path) const
if (!d || !d->connection || !QDBusUtil::isValidObjectPath(path))
return 0;
QStringList pathComponents = path.split(QLatin1Char('/'));
if (pathComponents.last().isEmpty())
auto pathComponents = path.splitRef(QLatin1Char('/'));
if (pathComponents.constLast().isEmpty())
pathComponents.removeLast();
// lower-bound search for where this object should enter in the tree

View File

@ -586,7 +586,7 @@ static void huntAndDestroy(QObject *needle, QDBusConnectionPrivate::ObjectTreeNo
}
}
static void huntAndUnregister(const QStringList &pathComponents, int i, QDBusConnection::UnregisterMode mode,
static void huntAndUnregister(const QVector<QStringRef> &pathComponents, int i, QDBusConnection::UnregisterMode mode,
QDBusConnectionPrivate::ObjectTreeNode *node)
{
if (pathComponents.count() == i) {
@ -2335,12 +2335,12 @@ void QDBusConnectionPrivate::registerObject(const ObjectTreeNode *node)
void QDBusConnectionPrivate::unregisterObject(const QString &path, QDBusConnection::UnregisterMode mode)
{
QDBusConnectionPrivate::ObjectTreeNode *node = &rootNode;
QStringList pathComponents;
QVector<QStringRef> pathComponents;
int i;
if (path == QLatin1String("/")) {
i = 0;
} else {
pathComponents = path.split(QLatin1Char('/'));
pathComponents = path.splitRef(QLatin1Char('/'));
i = 1;
}

View File

@ -94,14 +94,15 @@ QString qDBusInterfaceFromMetaObject(const QMetaObject *mo)
interface.prepend(QLatin1String("local."));
} else {
interface.prepend(QLatin1Char('.')).prepend(QCoreApplication::instance()->applicationName());
QStringList domainName =
QCoreApplication::instance()->organizationDomain().split(QLatin1Char('.'),
QString::SkipEmptyParts);
if (domainName.isEmpty())
const QString organizationDomain = QCoreApplication::instance()->organizationDomain();
const auto domainName = organizationDomain.splitRef(QLatin1Char('.'), QString::SkipEmptyParts);
if (domainName.isEmpty()) {
interface.prepend(QLatin1String("local."));
else
for (int i = 0; i < domainName.count(); ++i)
interface.prepend(QLatin1Char('.')).prepend(domainName.at(i));
} else {
interface.reserve(interface.size() + organizationDomain.size());
for (const QStringRef &x : domainName)
interface.prepend(QLatin1Char('.')).prepend(x);
}
}
}

View File

@ -42,6 +42,7 @@
#include "qdbus_symbols_p.h"
#include <QtCore/qstringlist.h>
#include <QtCore/qvector.h>
#include "qdbusargument.h"
#include "qdbusunixfiledescriptor.h"
@ -330,10 +331,10 @@ namespace QDBusUtil
/*!
\internal
\fn bool QDBusUtil::isValidPartOfObjectPath(const QString &part)
\fn bool QDBusUtil::isValidPartOfObjectPath(const QStringRef &part)
See QDBusUtil::isValidObjectPath
*/
bool isValidPartOfObjectPath(const QString &part)
bool isValidPartOfObjectPath(const QStringRef &part)
{
if (part.isEmpty())
return false; // can't be valid if it's empty
@ -346,6 +347,13 @@ namespace QDBusUtil
return true;
}
/*!
\internal
\fn bool QDBusUtil::isValidPartOfObjectPath(const QString &part)
\overload
*/
/*!
\fn bool QDBusUtil::isValidInterfaceName(const QString &ifaceName)
Returns \c true if this is \a ifaceName is a valid interface name.
@ -364,36 +372,35 @@ namespace QDBusUtil
if (ifaceName.isEmpty() || ifaceName.length() > DBUS_MAXIMUM_NAME_LENGTH)
return false;
QStringList parts = ifaceName.split(QLatin1Char('.'));
const auto parts = ifaceName.splitRef(QLatin1Char('.'));
if (parts.count() < 2)
return false; // at least two parts
for (int i = 0; i < parts.count(); ++i)
if (!isValidMemberName(parts.at(i)))
for (const QStringRef &part : parts)
if (!isValidMemberName(part))
return false;
return true;
}
/*!
\fn bool QDBusUtil::isValidUniqueConnectionName(const QString &connName)
\fn bool QDBusUtil::isValidUniqueConnectionName(const QStringRef &connName)
Returns \c true if \a connName is a valid unique connection name.
Unique connection names start with a colon (":") and are followed by a list of dot-separated
components composed of ASCII letters, digits, the hyphen or the underscore ("_") character.
*/
bool isValidUniqueConnectionName(const QString &connName)
bool isValidUniqueConnectionName(const QStringRef &connName)
{
if (connName.isEmpty() || connName.length() > DBUS_MAXIMUM_NAME_LENGTH ||
!connName.startsWith(QLatin1Char(':')))
return false;
QStringList parts = connName.mid(1).split(QLatin1Char('.'));
const auto parts = connName.mid(1).split(QLatin1Char('.'));
if (parts.count() < 1)
return false;
for (int i = 0; i < parts.count(); ++i) {
const QString &part = parts.at(i);
for (const QStringRef &part : parts) {
if (part.isEmpty())
return false;
@ -406,6 +413,12 @@ namespace QDBusUtil
return true;
}
/*!
\fn bool QDBusUtil::isValidUniqueConnectionName(const QString &connName)
\overload
*/
/*!
\fn bool QDBusUtil::isValidBusName(const QString &busName)
Returns \c true if \a busName is a valid bus name.
@ -429,12 +442,11 @@ namespace QDBusUtil
if (busName.startsWith(QLatin1Char(':')))
return isValidUniqueConnectionName(busName);
QStringList parts = busName.split(QLatin1Char('.'));
const auto parts = busName.splitRef(QLatin1Char('.'));
if (parts.count() < 1)
return false;
for (int i = 0; i < parts.count(); ++i) {
const QString &part = parts.at(i);
for (const QStringRef &part : parts) {
if (part.isEmpty())
return false;
@ -450,12 +462,12 @@ namespace QDBusUtil
}
/*!
\fn bool QDBusUtil::isValidMemberName(const QString &memberName)
\fn bool QDBusUtil::isValidMemberName(const QStringRef &memberName)
Returns \c true if \a memberName is a valid member name. A valid member name does not exceed
255 characters in length, is not empty, is composed only of ASCII letters, digits and
underscores, but does not start with a digit.
*/
bool isValidMemberName(const QString &memberName)
bool isValidMemberName(const QStringRef &memberName)
{
if (memberName.isEmpty() || memberName.length() > DBUS_MAXIMUM_NAME_LENGTH)
return false;
@ -469,6 +481,12 @@ namespace QDBusUtil
return true;
}
/*!
\fn bool QDBusUtil::isValidMemberName(const QString &memberName)
\overload
*/
/*!
\fn bool QDBusUtil::isValidErrorName(const QString &errorName)
Returns \c true if \a errorName is a valid error name. Valid error names are valid interface
@ -501,12 +519,10 @@ namespace QDBusUtil
path.endsWith(QLatin1Char('/')))
return false;
QStringList parts = path.split(QLatin1Char('/'));
Q_ASSERT(parts.count() >= 1);
parts.removeFirst(); // it starts with /, so we get an empty first part
for (int i = 0; i < parts.count(); ++i)
if (!isValidPartOfObjectPath(parts.at(i)))
// it starts with /, so we skip the empty first part
const auto parts = path.midRef(1).split(QLatin1Char('/'));
for (const QStringRef &part : parts)
if (!isValidPartOfObjectPath(part))
return false;
return true;

View File

@ -68,15 +68,18 @@ namespace QDBusUtil
{
Q_DBUS_EXPORT bool isValidInterfaceName(const QString &ifaceName);
Q_DBUS_EXPORT bool isValidUniqueConnectionName(const QString &busName);
Q_DBUS_EXPORT bool isValidUniqueConnectionName(const QStringRef &busName);
bool inline isValidUniqueConnectionName(const QString &busName) { return isValidUniqueConnectionName(QStringRef(&busName)); }
Q_DBUS_EXPORT bool isValidBusName(const QString &busName);
Q_DBUS_EXPORT bool isValidMemberName(const QString &memberName);
Q_DBUS_EXPORT bool isValidMemberName(const QStringRef &memberName);
bool inline isValidMemberName(const QString &memberName) { return isValidMemberName(QStringRef(&memberName)); }
Q_DBUS_EXPORT bool isValidErrorName(const QString &errorName);
Q_DBUS_EXPORT bool isValidPartOfObjectPath(const QString &path);
Q_DBUS_EXPORT bool isValidPartOfObjectPath(const QStringRef &path);
bool inline isValidPartOfObjectPath(const QString &path) { return isValidPartOfObjectPath(QStringRef(&path)); }
Q_DBUS_EXPORT bool isValidObjectPath(const QString &path);