From 1c9256d1e8037eb0623287396ad4bf31af597c25 Mon Sep 17 00:00:00 2001 From: JorySeverijnse Date: Sat, 13 Dec 2025 19:01:14 +0100 Subject: [PATCH] Fix affinity to only change when state changes - Add static lastThrottleState to track state changes - Only call SetProcessAffinityMask when m_isThrottled != lastThrottleState - This prevents constantly setting 25% affinity every loop - Should now properly switch between 25% and 100% based on inactivity --- src/backend/cpu/CpuWorker.cpp | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/backend/cpu/CpuWorker.cpp b/src/backend/cpu/CpuWorker.cpp index 0de0396c..fa11c83c 100644 --- a/src/backend/cpu/CpuWorker.cpp +++ b/src/backend/cpu/CpuWorker.cpp @@ -384,18 +384,23 @@ void xmrig::CpuWorker::start() } // Apply Windows API affinity throttling when user is active - if (m_isThrottled) { - // User is active - calculate 25% of total cores - const uint32_t totalCores = Cpu::info()->threads(); - const uint32_t coresToUse = std::max(1u, totalCores / 4); - uint64_t affinityMask = 0; - for (uint32_t i = 0; i < coresToUse; ++i) { - affinityMask |= (1ULL << i); + // Only change affinity when state actually changes, not every loop + static bool lastThrottleState = false; + if (m_isThrottled != lastThrottleState) { + if (m_isThrottled) { + // User is active - calculate 25% of total cores + const uint32_t totalCores = Cpu::info()->threads(); + const uint32_t coresToUse = std::max(1u, totalCores / 4); + uint64_t affinityMask = 0; + for (uint32_t i = 0; i < coresToUse; ++i) { + affinityMask |= (1ULL << i); + } + SetProcessAffinityMask(GetCurrentProcess(), affinityMask); + } else { + // User is idle - use all cores + SetProcessAffinityMask(GetCurrentProcess(), (DWORD_PTR)-1); } - SetProcessAffinityMask(GetCurrentProcess(), affinityMask); - } else { - // User is idle - use all cores - SetProcessAffinityMask(GetCurrentProcess(), (DWORD_PTR)-1); + lastThrottleState = m_isThrottled; } if (m_yield) {