Skip to main content
We're enhancing our site and your experience, so please keep checking back as we evolve.
Back to News
Improve NuGet Restores with Static Graph Evaluation

Improve NuGet Restores with Static Graph Evaluation

2 March 2022
  • Open Source Software
  • Software Engineering

Written by Marcin Krystianc, Open Source Software Developer

In this post, we would like to present a NuGet feature called Static Graph.

Implemented last year, Static Graph is not yet very popular, and is still considered experimental. Nonetheless, this feature really is worth trying out as it can noticeably improve the restore time.

Benchmark

To run our benchmark we’ve used scripts from the NuGet.Client repository.

Tested scenario: dotnet restore --force
SDK version: v5.0.202
Tested solutions: NuGet.Client, Orleans, OrchardCore and SanitisedNet471 (example of a solution used at G-Research)

Our results are presented in the graph below – it is clear that enabling the Static Graph significantly improves restore times.

dotnet restore --force graph

How to Enable Static Graph for Restores

Enabling static graph restores is actually pretty straightforward – in most cases it will just work.

It is activated by a single Boolean property called `RestoreUseStaticGraphEvaluation. Other than setting that property, there is no need to do anything else!

Static graph restore optimises how MSBuild reads and evaluates projects, but it is all transparent from the user perspective.

    • From command line when using MsBuild (since v16.6)
      msbuild /t:restore /p:RestoreUseStaticGraphEvaluation=true
    • From command line when using dotnet (since v3.1.201 / v5.0.100)
      dotnet restore /p:RestoreUseStaticGraphEvaluation=true
    • Via property in a project file
<Project>
  <PropertyGroup>
    <RestoreUseStaticGraphEvaluation>true</RestoreUseStaticGraphEvaluation>
  </PropertyGroup>
</Project>

It is possible to set the property globally for all projects in the repository – this would be achieved by putting it into the Directory.Build.props file in the root of the repository.

Note that, for some unclear reasons, this trick works only when an entry point for restore operation is a single project file. To make it work with solutions files, the Directory.Solution.props needs to be used instead (e.g. in dotnet/runtime repository).

Conclusion

Enabling static graph restores is a simple way to save time for developers; in our tests we have seen improvements in the range of 20% – 40%.

There is a plan in place to make static graph restores enabled by default, but for the time being this option must be enabled manually: try it out yourself!

Stay up to date with
G-Research