dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.22k stars 1.35k forks source link

Use EvaluationContexts in graph creation #7002

Open rainersigwald opened 2 years ago

rainersigwald commented 2 years ago

Discussed offline with @AArnott and @cdmihai.

When building the static graph, I/O can be a bottleneck for evaluating the many projects that get put into the graph. The existing EvaluationContext allows sharing filesystem enumerations and other operations and would potentially speed up evaluation (especially of heavily multitargeted solutions).

This may not be trivial (from @cdmihai):

That’s a good point, static graph creation should use an evaluation context in order cache stuff from all the nodes’ evaluations, but it doesn’t. That would be one good perf optimization. Might be tricky to have it use an evaluation context because the graph builder provides a delegate to users, so the project evaluation happens in users’ code when they provide their own delegate. So we either change the delegate signature to receive an evaluation context (breaking change right?) or we change the ProjectCollection to carry an evaluation context (the delegate does receive a ProjectCollection).

Even if we can't make this fully generalizable, we should consider using an EvaluationContext for the default-behavior "don't specify your own delegate" case

            using ProjectCollection projectCollection = new(); 
            ProjectGraph graph = new(entrypoints, projectCollection);
cdmihai commented 2 years ago

The workaround here is that the custom delegate providing user can create and use their own EvaluationContext instance when their delegate gets called. That's how QB does it.

On the other hand it would be nice if static graph construction did this by default. I can't think of any scenario where you wouldn't want automatic caching (unless your memory is really constrained ...), and there's a high probability that potential static graph users might not know about EvaluationContexts.

AArnott commented 2 years ago

unless your memory is really constrained

If my memory were constrained, I would be more worried about the accumulation of ProjectInstance objects that the graph creates and never releases during the graph traversal.