I have observed a curious behaviour where processing in Unity Editor (TestRunner) takes 2.5s while the editor is in the foreground, but slows down to 15s when I immediately tab to another app, thus putting the editor to the background.
What's more: if I put the editor into the background but constantly move the mouse over the editor window, the processing completes in the same time as if the editor were in the foreground all the time.
I assume this to be some Windows (11?) optimization to purposefully slow down apps in the background. I could not find a way to disable this for a particular app or system-wide for me as an end-user. Does such an option exist for end-users or a workaround?
Is there anything an app developer can do to prevent this behaviour?
Because I have observed that the same issue does not exist when running Unity editor on Ubuntu 22 in VMware - the tests always complete fast regardless of whether I put the editor in the background within the VM, and not even when I put the entire VM to the background. This makes me hopeful that there are ways to prevent this behaviour.
FYI: The whole issue has been documented as a bug report to Jetbrains and in the Unity forum. I'm hoping I don't have to wait for a fix of either app, I'd rather wish to adjust some system setting and fix this highly undesirable Windows behaviour for good.
I have already tried changing the following settings to no avail:
- Unity Editor interaction Mode (Preferences => General) set to "No Throttling" and "8 ms" (120 Hz)
- using Task Manager to set the Unity.exe process to "Realtime"
- disabled "Hardware-Accelerated GPU Scheduling" (Settings: System>Display>Graphics>Default graphics settings)
- via Advanced System Settings: Performance Options => Advanced I set "Adjust for best performance of: Background services"
- turned off "Game Mode"
- set power plan to "Best Performance"
- wrote an autohotkey script that moves the mouse by 1px once every ms but this only speeds up execution by 50% (8s) and requires the cursor to be over the editor window
- changed SystemResponsiveness to 64 (100%) (see below) and added NoLazyMode=1 (in the same registry key)
- disabled Windows Defender realtime protection (see comment)
- disabled powerthrottling of unity.exe via powercfg
- manually adjusted Win32PrioritySeparation in registry
The last two tips were from this superuser post.
Temporary Workaround: This is an odd one but works reliably for me in Unity 2021.2 and newer. However it needs to be re-applied manually after every script compile / assembly reload.
- Open Preferences: General => Busy Progress Delay
- Click and drag the label to change the value (any value is fine as long as it is changed - IMPORTANT: it does not work by entering a value directly in the text field!)
- Run Unit Tests and put editor in background. Test continues to run fast.
I tried changing the corresponding EditorPrefs key "EditorBusyProgressDialogDelay" before running tests but this apparently does not trigger the code that makes the tests actually run faster. If anyone has any idea how to disassembly the editor code for the General Preferences that may help because then I could actually call that (possibly internal) method.
Additional note: setting Interaction Mode to No Throttling generally makes tests run 30% faster on my system!