msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

Improve MSBuild Performance by Adjusting CPU Priority

Open RezaRahmatzadeh opened this issue 1 year ago • 5 comments

Summary

I frequently build, run, and rebuild an MVC application on Windows 11 using my office laptop. To improve build performance, I’ve found that manually adjusting the CPU priority for MSBuild.exe to High (via Task Manager or PowerShell) significantly reduces build times.

Is there a built-in way to configure MSBuild to always run with High CPU priority—either globally, from within Visual Studio, or via an MSBuild setting?

I currently use a PowerShell script to set MSBuild’s priority to High:

while ($true) {
    $processes = @(
        'MSBuild',
        'devenv',
        'ServiceHub.Host.dotnet.x64',
        'ServiceHub.RoslynCodeAnalysisService'
    )
    
foreach ($processName in $processes) {
        Get-Process $processName -ErrorAction SilentlyContinue | ForEach-Object {
            $_. PriorityClass = 'High'
        }
    }
    Start-Sleep -Seconds 5
}

Background and Motivation

Building and rebuilding my MVC application frequently on Windows 11, I’ve noticed that manually setting MSBuild.exe to High CPU priority significantly reduces build times.

Proposed Feature

Provide a built-in option to configure MSBuild to always run with High CPU priority, either globally, within Visual Studio, or via an MSBuild setting.

Alternative Designs

A possible alternative is using an external script (e.g., PowerShell) to adjust MSBuild's process priority each time it runs, but this is not as seamless as a built-in solution.

RezaRahmatzadeh avatar Feb 10 '25 23:02 RezaRahmatzadeh

Hi @RezaRahmatzadeh,

After the team triage, we agreed to close the ticket since it doesn't fit a general windows model for handling the priorities. We would recommend you to run devenv.exe with bumped priority and all spawns processes would inherit it.

YuliiaKovalova avatar Feb 11 '25 15:02 YuliiaKovalova

Hi @RezaRahmatzadeh,

After the team triage, we agreed to close the ticket since it doesn't fit a general windows model for handling the priorities. We would recommend you to run devenv.exe with bumped priority and all spawns processes would inherit it.

Thanks for your suggestion @YuliiaKovalova , when I tried to set devenv.exe to high after that I realized msbuild is not getting high priority so I needed to set them to high separately, setting msbuild to high fasting build process very remarkable.

This matches my experience as well - MSBuild seems to set its own priority rather than inheriting from devenv.exe, which is why explicitly setting MSBuild to high priority gives such noticeable performance improvements during builds.

RezaRahmatzadeh avatar Feb 11 '25 16:02 RezaRahmatzadeh

Hi @RezaRahmatzadeh, After the team triage, we agreed to close the ticket since it doesn't fit a general windows model for handling the priorities. We would recommend you to run devenv.exe with bumped priority and all spawns processes would inherit it.

Thanks for your suggestion @YuliiaKovalova , when I tried to set devenv.exe to high after that I realized msbuild is not getting high priority so I needed to set them to high separately, setting msbuild to high fasting build process very remarkable.

This matches my experience as well - MSBuild seems to set its own priority rather than inheriting from devenv.exe, which is why explicitly setting MSBuild to high priority gives such noticeable performance improvements during builds.

let's ask @rainersigwald if he knows why it could happen

YuliiaKovalova avatar Feb 11 '25 16:02 YuliiaKovalova

https://learn.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities:

If the calling process is IDLE_PRIORITY_CLASS or BELOW_NORMAL_PRIORITY_CLASS, the new process will inherit this class.

This seems to mean HIGH_PRIORITY_CLASS is not inherited by child processes.

KalleOlaviNiemitalo avatar Feb 11 '25 20:02 KalleOlaviNiemitalo

Huh, TIL!

However, I don't expect to add this option--it doesn't reliably improve performance for builds when we've tried it in the past, and spawning a bunch of high-pri processes is a great way to make a machine feel laggy and unusable.

rainersigwald avatar Feb 11 '25 23:02 rainersigwald