4

I recently switched to Qt5 and I noticed that initializing QApplication is taking noticeably longer (on the order of minutes) compared to Qt4.

Looking into it, the program seems to be spending its time here:

Qt5Guid.dll!QGlobalStatic<QFactoryLoader,&`anonymous namespace'::Q_QGS_directLoader::innerFunction,A0x95ca5a10::Q_QGS_directLoader::guard>::operator()() Line 129   C++
Qt5Guid.dll!QPlatformIntegrationFactory::create(const QString & platform, const QStringList & paramList, int & argc, char * * argv, const QString & platformPluginPath) Line 70 C++
Qt5Guid.dll!init_platform(const QString & pluginArgument, const QString & platformPluginPath, const QString & platformThemeName, int & argc, char * * argv) Line 1019   C++
Qt5Guid.dll!QGuiApplicationPrivate::createPlatformIntegration() Line 1176   C++
Qt5Guid.dll!QGuiApplicationPrivate::createEventDispatcher() Line 1196   C++
Qt5Widgetsd.dll!QApplicationPrivate::createEventDispatcher() Line 197   C++
Qt5Cored.dll!QCoreApplication::init() Line 769  C++
Qt5Cored.dll!QCoreApplication::QCoreApplication(QCoreApplicationPrivate & p) Line 689   C++
Qt5Guid.dll!QGuiApplication::QGuiApplication(QGuiApplicationPrivate & p) Line 570   C++
Qt5Widgetsd.dll!QApplication::QApplication(int & argc, char * * argv, int _internal) Line 569   C++

It's basically spending several minutes every startup initializing the windows platform plugin.

How can I reduce or remove this time and does anyone know what the cause might be?

I'm using Qt 5.5.1 with VS2012 64 bit.

Edit: Not sure if it's relevant, but I have QT_QPA_PLATFORM_PLUGIN_PATH="C:\Qt-5.5.1\plugins\platforms\" which is how it finds the platform plugin.

Edit2: Further investigation shows that the cause is that in the process of initializing the program, QFactoryLoader::update() scans through every file in some directories looking for a pattern. Because the directory includes the program directory it scans through the entirety of my program file (several gb).

The paths are determined by QCoreApplication::libraryPaths() which returns has 3 values:

  • C:/Qt-5.5.1/plugins/platforms
  • C:/Qt-5.5.1/plugins
  • C:/MyProject/x64/Debug <-- the problem

The update() function is called 7 times in the init process, but it's actually only the first two calls that are very slow.

I've also noticed that if I call QCoreApplication::libraryPaths() before the init, then only the first two paths are returned for the first 4 update() calls, which solves the problem.

It's great that I have a solution now, but I would like to understand why this works and find a less hacky one.

6
  • Maybe some Windows security feature increases loading time of the plugins?
    – Marek R
    Feb 22, 2016 at 22:19
  • If I were you I'd actually profile the startup. To see which function specifically it is that is using up the time (up to the presumably win32 functions it calls).
    – PeterT
    Feb 22, 2016 at 22:39
  • The app we developed for 2 years with Qt and binaries altogether around 25 mb in compressed installer archive it takes maybe second or two. We use Qt 5.3.2 and 5.5. The only thing that somewhat affects the load time is dynamic vs. static link but the difference is in percents. We have static link but started with dynamic. That should be debugger. Feb 22, 2016 at 23:36
  • Maybe the loading time can improve with static linking? Feb 23, 2016 at 0:32
  • 2
    @Whanhee Your last activity here said you were going to look at how to remove search paths to the Qt config to fix this problem. Did you ever figure that out? I've tried wading through the documentation but it always seems targeted at adding paths.
    – Bret Kuhns
    May 7, 2017 at 12:50

3 Answers 3

1

The answer without specific environment to quite a general question:

How can I improve the startup time of Qt5 programs?

... cannot really be given here. Most of Qt developers don't experience such delay. But we can always try to debug. So the answer is on how to debug. From what you posted the code is located at: qtbase/src/gui/kernel/qplatformintegrationfactory.cpp:

Qt5Guid.dll!QGlobalStatic<QFactoryLoader,&`anonymous namespace'::Q_QGS_directLoader::innerFunction,A0x95ca5a10::Q_QGS_directLoader::guard>::operator()() Line 129   C++
Qt5Guid.dll!QPlatformIntegrationFactory::create(const QString & platform, const QStringList & paramList, int & argc, char * * argv, const QString & platformPluginPath) Line 70 C++

It appears that the function called:

QPlatformIntegration *QPlatformIntegrationFactory::create(const QString &platform, const QStringList &paramList, int &argc, char **argv, const QString &platformPluginPath)
{
#ifndef QT_NO_LIBRARY
    // Try loading the plugin from platformPluginPath first:
    if (!platformPluginPath.isEmpty()) {
        QCoreApplication::addLibraryPath(platformPluginPath);
        if (QPlatformIntegration *ret = loadIntegration(directLoader(), platform, paramList, argc, argv))
            return ret;
    }
    if (QPlatformIntegration *ret = loadIntegration(loader(), platform, paramList, argc, argv))
        return ret;
#endif
    return 0;
}

You can either point to debug version of Qt or load symbols for Qt5Guid.dll and make the Qt source code available to debugger and/or build Qt yourself at the machine you use for debugging of your project and link the executable with it. Then put a breakpoint on line 70:

if (QPlatformIntegration *ret = loadIntegration(directLoader(), platform, paramList, argc, argv))

And then run the program and investigate what prevents the plugin from being loaded quickly. From this context it is not even clear what exact plugin it is, either it comes with Qt or a custom one.

5
  • Ok I've debugged it, with the breakpoint there and yes, platform is "windows" and platformPluginPath is "%QT_PATH%/plugins/platforms". (argc is also 0). Profiling also finds that it's spending all its time in Qt5Cored.dll!qt_find_pattern.
    – Www
    Feb 23, 2016 at 18:02
  • Ok I've investigated further and the cause is that QFactoryLoader::update is scanning through the executable directory (on top of others) for plugins. This involves scanning entire files for a pattern. The problem is that the executable and some libraries are several gb so it's looking through all the files for this plugin pattern.
    – Www
    Feb 23, 2016 at 19:02
  • That is good, you nailed it. Now need to look at ways to minimize it. doc.qt.io/qt-5/deployment-plugins.html Feb 23, 2016 at 19:26
  • I've added some more information in Edit2, do you have any more thoughts or suggestions?
    – Www
    Feb 23, 2016 at 21:29
  • 1
    Provide your own list of library paths. It looks like QCoreApplication::setLibraryPaths can do that. Maybe refactor files to more limited directories as well. Feb 23, 2016 at 22:14
0

I would not start with the debugging (because, I would venture, you might have hit highly contested lock somewhere and debugging won't help much)

I would advise to start with good profiling of your code, and I know none better than tools/docs from Bruce Dawson

Start here and proceed with profiling your app

3
  • I've profiled/debugged further and the cause is that it's scanning through every file in a few directories for being a plugin dll. This involves scanning through the entire files for a pattern. It scans through the executable and some of its libraries which are several gb, which is the cause of the slowness.
    – Www
    Feb 23, 2016 at 19:00
  • @Whanhee aha! good to hear. I guess you could set scanning dirs somewhere in config and be done with it. But I'll suggest to keep Bruce link handy, it is such a good tool to profile Windows apps Feb 23, 2016 at 19:11
  • Yes, I'm looking at how exactly the dirs are set. I'll keep the link in my bookmarks, this xperf profiling is actually insane! Thanks for the help.
    – Www
    Feb 23, 2016 at 19:21
0

Short answer: Deploy your binary file into a subfolder.

Long answer: I had the very same issue and looking up the Qt source code, it will append the application directory in order to look up for plugins. So there is no choice but to leave the application alone in a folder unless someone do a merge request (if I have spare time, I will try to do it myself).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.