From 9890bb75011efe46b7928923d28c3ff6b21ef207 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 5 Jul 2022 14:21:17 +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. Original PR: #17311. --- src/base/bittorrent/session.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 94cb7c2b8..bd7b7c7fb 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -3061,6 +3061,11 @@ void Session::applyOSMemoryPriority() 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("Kernel32.dll", "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 @@ -3097,6 +3102,11 @@ void Session::applyOSMemoryPriority() 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)); } #endif