From 3ef51efbe75bfb9f1dfbe7df073e9eb745a72ad8 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 15 Aug 2012 18:03:50 +0200 Subject: [PATCH] Avoid an expensive call to toLocal8Bit upon thread creation QString::toLocal8Bit() will need to call QTextCodec::codecForLocale(), which isn't the cheapest of the functions, at least the first time it's run. So avoid calling it when in most scenarios, the name of the QObject isn't set, and the information is purely for debugging. Additionally, avoid allocating memory when setting the thread name to the class name. The class name coming from the meta object is a static constant string and we can use it directly. Change-Id: Ief643bad87a51487b1d41c0a2f323e80bb53e8a7 Reviewed-by: Olivier Goffart Reviewed-by: Simon Hausmann --- src/corelib/thread/qthread_unix.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index 0250ea1053b..47d6f48ed99 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -270,6 +270,19 @@ void QThreadPrivate::createEventDispatcher(QThreadData *data) #ifndef QT_NO_THREAD +#if (defined(Q_OS_LINUX) || defined(Q_OS_MAC) || defined(Q_OS_QNX)) +static void setCurrentThreadName(const char *name) +{ +# if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) + prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); +# elif defined(Q_OS_MAC) + pthread_setname_np(name); +# elif defined(Q_OS_QNX) + pthread_setname_np(thr->d_func()->thread_id, name); +# endif +} +#endif + void *QThreadPrivate::start(void *arg) { #if !defined(Q_OS_LINUX_ANDROID) @@ -301,18 +314,13 @@ void *QThreadPrivate::start(void *arg) #if (defined(Q_OS_LINUX) || defined(Q_OS_MAC) || defined(Q_OS_QNX)) // sets the name of the current thread. - QByteArray objectName = thr->objectName().toLocal8Bit(); + QString objectName = thr->objectName(); - if (objectName.isEmpty()) - objectName = thr->metaObject()->className(); + if (Q_LIKELY(objectName.isEmpty())) + setCurrentThreadName(thr->metaObject()->className()); + else + setCurrentThreadName(objectName.toLocal8Bit()); -#if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) - prctl(PR_SET_NAME, (unsigned long)objectName.constData(), 0, 0, 0); -#elif defined(Q_OS_MAC) - pthread_setname_np(objectName.constData()); -#elif defined(Q_OS_QNX) - pthread_setname_np(thr->d_func()->thread_id, objectName.constData()); -#endif #endif emit thr->started();