From ed2eadaf56ae23e3d481fbbf028b345f30b583ee Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 2 Jul 2022 19:05:15 +0800 Subject: [PATCH] Work around application stuttering on Windows This is observed by unusual high page faults when the stuttering occurs. With this workaround, the high page faults still occurs but the GUI remains responsive. --- src/app/application.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/app/application.cpp b/src/app/application.cpp index 83aa0f071..45e6dc43a 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -847,6 +847,11 @@ void Application::applyMemoryPriority() const if (!setProcessInformation) // only available on Windows >= 8 return; + using SETTHREADINFORMATION = BOOL (WINAPI *)(HANDLE, THREAD_INFORMATION_CLASS, LPVOID, DWORD); + const auto setThreadInformation = Utils::Misc::loadWinAPI(u"Kernel32.dll"_qs, "SetThreadInformation"); + if (!setThreadInformation) // only available on Windows >= 8 + return; + #if (_WIN32_WINNT < _WIN32_WINNT_WIN8) // this dummy struct is required to compile successfully when targeting older Windows version struct MEMORY_PRIORITY_INFORMATION @@ -883,6 +888,11 @@ void Application::applyMemoryPriority() const break; } setProcessInformation(::GetCurrentProcess(), ProcessMemoryPriority, &prioInfo, sizeof(prioInfo)); + + // To avoid thrashing/sluggishness of the app, set "main event loop" thread to normal memory priority + // which is higher/equal than other threads + prioInfo.MemoryPriority = MEMORY_PRIORITY_NORMAL; + setThreadInformation(::GetCurrentThread(), ThreadMemoryPriority, &prioInfo, sizeof(prioInfo)); } void Application::adjustThreadPriority() const