Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Platform Negotiation in graph build #9343

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Build/Graph/GraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ internal class GraphBuilder

private readonly ProjectGraph.ProjectInstanceFactoryFunc _projectInstanceFactory;
private IReadOnlyDictionary<string, IReadOnlyCollection<string>> _solutionDependencies;
private ConcurrentDictionary<ConfigurationMetadata, Lazy<ProjectInstance>> _platformNegotiationInstancesCache = new();

public GraphBuilder(
IEnumerable<ProjectGraphEntryPoint> entryPoints,
Expand Down Expand Up @@ -92,6 +93,9 @@ public void BuildGraph()

RootNodes = GetGraphRoots(EntryPointNodes);
ProjectNodes = allParsedProjects.Values.Select(p => p.GraphNode).ToList();

// Clean and release some temporary used large memory objects.
_platformNegotiationInstancesCache.Clear();
}

private static IReadOnlyCollection<ProjectGraphNode> GetGraphRoots(IReadOnlyCollection<ProjectGraphNode> entryPointNodes)
Expand Down Expand Up @@ -576,7 +580,7 @@ private List<ProjectInterpretation.ReferenceInfo> ParseReferences(ProjectGraphNo
{
var referenceInfos = new List<ProjectInterpretation.ReferenceInfo>();

foreach (var referenceInfo in _projectInterpretation.GetReferences(parsedProject.ProjectInstance, _projectCollection, _projectInstanceFactory))
foreach (var referenceInfo in _projectInterpretation.GetReferences(parsedProject.ProjectInstance, _projectCollection, GetInstanceForPlatformNegotiationWithCaching))
{
if (FileUtilities.IsSolutionFilename(referenceInfo.ReferenceConfiguration.ProjectFullPath))
{
Expand All @@ -594,6 +598,16 @@ private List<ProjectInterpretation.ReferenceInfo> ParseReferences(ProjectGraphNo
return referenceInfos;
}

private ProjectInstance GetInstanceForPlatformNegotiationWithCaching(
string projectPath,
Dictionary<string, string> globalProperties,
ProjectCollection projectCollection)
{
return _platformNegotiationInstancesCache.GetOrAdd(
new ConfigurationMetadata(projectPath, CreatePropertyDictionary(globalProperties)),
new Lazy<ProjectInstance>(() => _projectInstanceFactory(projectPath, globalProperties, projectCollection))).Value;
}

internal static string FormatCircularDependencyError(List<string> projectsInCycle)
{
var errorMessage = new StringBuilder(projectsInCycle.Select(p => p.Length).Sum());
Expand Down