From 4f3ea309222bf9af61f721b39265b651ab297914 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 6 Feb 2017 19:41:46 -0800 Subject: Avoid unnecessary creation of some Q_GLOBAL_STATIC If these lists weren't created in the first place, then they are empty. We don't need to create it in order to conclude that. Unlike most Q_GLOBAL_STATICS, these are almost never used and yet they were always created due to where they were checked. Since we're calling exists() before, there are two consequences: first, since the list already exists, we're not allocating memory so it cannot throw std::bad_alloc when being accessed. Second, since we've just checked it exists, we can use QGlobalStatic's operator*(), which is slightly faster than operator()(). The weird &(*list) syntax is only to avoid changing the rest of the code that used a pointer Change-Id: Ifaee7464122d402991b6fffd14a0e44f533dc3d9 Reviewed-by: Marc Mutz --- src/corelib/global/qglobal.cpp | 5 ++++- src/corelib/kernel/qcoreapplication.cpp | 17 ++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index bc6560ddae..54df8b1f61 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -4027,7 +4027,10 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters) { Q_ASSERT_X(cb >= 0, "QInternal::activateCallback()", "Callback id must be a valid id"); - QInternal_CallBackTable *cbt = global_callback_table(); + if (!global_callback_table.exists()) + return false; + + QInternal_CallBackTable *cbt = &(*global_callback_table); if (cbt && cb < cbt->callbacks.size()) { QList callbacks = cbt->callbacks[cb]; bool ret = false; diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 96167b4508..c5f2e71f8c 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -270,12 +270,13 @@ void qRemovePostRoutine(QtCleanUpFunction p) static void qt_call_pre_routines() { - QStartUpFuncList *list = preRList(); - if (!list) + if (!preRList.exists()) return; + #ifndef QT_NO_THREAD QMutexLocker locker(&globalPreRoutinesMutex); #endif + QVFuncList *list = &(*preRList); // Unlike qt_call_post_routines, we don't empty the list, because // Q_COREAPP_STARTUP_FUNCTION is a macro, so the user expects // the function to be executed every time QCoreApplication is created. @@ -285,16 +286,10 @@ static void qt_call_pre_routines() void Q_CORE_EXPORT qt_call_post_routines() { - QVFuncList *list = 0; - QT_TRY { - list = postRList(); - } QT_CATCH(const std::bad_alloc &) { - // ignore - if we can't allocate a post routine list, - // there's a high probability that there's no post - // routine to be executed :) - } - if (!list) + if (!postRList.exists()) return; + + QVFuncList *list = &(*postRList); while (!list->isEmpty()) (list->takeFirst())(); } -- cgit v1.2.3