Skip to content

Commit

Permalink
Cache Platform Negotiation in graph build (#9343)
Browse files Browse the repository at this point in the history
* Cache Platform Negotiation in graph build
* Clean cached data after build.
  • Loading branch information
rokonec committed Oct 27, 2023
1 parent 14f68e2 commit efd0158
Showing 1 changed file with 15 additions and 1 deletion.
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

0 comments on commit efd0158

Please sign in to comment.