From 1270284f8dc753205034e7958c2a4063d1d15a7b Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Wed, 17 Aug 2022 19:55:21 +0800 Subject: [PATCH] QSystemLibrary: Cache the system directory path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The system directory path won't change during the lifetime of the application, so cache it to avoid querying it for multiple times. Change-Id: I302285794d491d581d74a93e7ba9affc6379c681 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- src/corelib/plugin/qsystemlibrary.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/corelib/plugin/qsystemlibrary.cpp b/src/corelib/plugin/qsystemlibrary.cpp index 503bb25206..f1475b4a67 100644 --- a/src/corelib/plugin/qsystemlibrary.cpp +++ b/src/corelib/plugin/qsystemlibrary.cpp @@ -44,15 +44,17 @@ extern QString qAppFileName(); static QString qSystemDirectory() { - QVarLengthArray fullPath; - - UINT retLen = ::GetSystemDirectory(fullPath.data(), MAX_PATH); - if (retLen > MAX_PATH) { - fullPath.resize(retLen); - retLen = ::GetSystemDirectory(fullPath.data(), retLen); - } - // in some rare cases retLen might be 0 - return QString::fromWCharArray(fullPath.constData(), int(retLen)); + static const QString result = []() -> QString { + QVarLengthArray fullPath = {}; + UINT retLen = ::GetSystemDirectoryW(fullPath.data(), MAX_PATH); + if (retLen > MAX_PATH) { + fullPath.resize(retLen); + retLen = ::GetSystemDirectoryW(fullPath.data(), retLen); + } + // in some rare cases retLen might be 0 + return QString::fromWCharArray(fullPath.constData(), int(retLen)); + }(); + return result; } HINSTANCE QSystemLibrary::load(const wchar_t *libraryName, bool onlySystemDirectory /* = true */)