8

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.

  1. Open Preferences: General => Busy Progress Delay
  2. 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!)
  3. 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!

music2myear
  • 49,799
CodeSmile
  • 499

3 Answers3

2

No idea if this will help you, but I have this registry edit stored somewhere, which supposedly modifies the reserved CPU for "background" or "low priority" processes. I think that you're supposed to enter the hexadecimal value here, so "64" for "100%".

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile] "SystemResponsiveness"=dword:64 ;end

This key contains a REG_DWORD value named SystemResponsiveness that determines the percentage of CPU resources that should be guaranteed to low-priority tasks. For example, if this value is 20, then 20% of CPU resources are reserved for low-priority tasks. Note that values that are not evenly divisible by 10 are rounded up to the nearest multiple of 10. A value of 0 is also treated as 10.

GChuf
  • 1,327
2

If I understand right, this only happens with Unity.

As you have tried every setting that Windows has for increasing the performance of background tasks, this is probably not a Windows problem. You have even in Task Manager set the priority to Realtime, which ensures that a process will have absolute CPU priority, but that was not enough for this one process.

My conclusion is that the problem is with the process itself, and this is Unity bug. I don't think that there is a Windows solution to the problem.

One might be able to write a program that will send Windows API messages to the process that will convince it that it's running in foreground mode while in the background, but I don't know of a product that does that.

harrymc
  • 498,455
1

Pasting the solution OP wrote incorrectly as an edit to their question:

SOLUTION: The background test run slowdow described below no longer occurs in Unity 2023.1.0b14 (beta) and 2023.2.0a12 (alpha). I wrote a script that speeds up TestRunner by ~30% in general in my use case, and speeds up test runs from within Rider by a factor of 5 (but as of today only when using Unity 2023).

This is achieved by temporarily setting Editor Interaction Mode to No Throttling during a test run. Of course you can manually change this setting permanently - but keep in mind this is going to permanently and unnecessarily waste energy / drain the battery and increase heat & noise.

music2myear
  • 49,799