QCoreApplication: avoid QFileInfo in applicationDirPath()

QFileInfo allocates a lot of memory, so it's not very good a tool for
manipulating just strings. As this function is called very early in the
application's lifetime (by the first qDebug() or so), we can avoid
creating the QFileInfoPrivate by going straight to QFileSystemEngine,
which is what QFileInfo::path() would have done:

QString QFileInfo::path() const
{
    Q_D(const QFileInfo);
    if (d->isDefaultConstructed)
        return ""_L1;
    return d->fileEntry.path();
}

Change-Id: Ie911c42577113c477aa9fffd5dcbb7e6f24af8f6
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
This commit is contained in:
Thiago Macieira 2025-02-09 12:32:28 -08:00
parent 87ba5348e9
commit d34a3536bf

View File

@ -18,6 +18,7 @@
#include <qdir.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <private/qfilesystementry_p.h>
#include <qmutex.h>
#include <private/qloggingregistry_p.h>
#include <qscopeguard.h>
@ -2382,7 +2383,8 @@ QString QCoreApplication::applicationDirPath()
return QString();
}
return QFileInfo(applicationFilePath()).path();
QFileSystemEntry appFilePath(applicationFilePath(), QFileSystemEntry::FromInternalPath{});
return appFilePath.isEmpty() ? QString() : appFilePath.path();
}
#if !defined(Q_OS_WIN) && !defined(Q_OS_DARWIN)