diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs b/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs index 9282823dfaa2b..9f183de780782 100644 --- a/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs +++ b/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs @@ -1549,12 +1549,27 @@ private static ProjectDependencyGraph CreateDependencyGraph( private ImmutableDictionary CreateCompilationTrackerMap(ProjectId changedProjectId, ProjectDependencyGraph dependencyGraph) { - var builder = ImmutableDictionary.CreateBuilder(); + if (_projectIdToTrackerMap.Count == 0) + return _projectIdToTrackerMap; + using var _ = ArrayBuilder>.GetInstance(_projectIdToTrackerMap.Count, out var newTrackerInfo); + var allReused = true; foreach (var (id, tracker) in _projectIdToTrackerMap) - builder.Add(id, CanReuse(id) ? tracker : tracker.Fork(tracker.ProjectState, translate: null)); + { + var localTracker = tracker; + if (!CanReuse(id)) + { + localTracker = tracker.Fork(tracker.ProjectState, translate: null); + allReused = false; + } - return builder.ToImmutable(); + newTrackerInfo.Add(new KeyValuePair(id, localTracker)); + } + + if (allReused) + return _projectIdToTrackerMap; + + return ImmutableDictionary.CreateRange(newTrackerInfo); // Returns true if 'tracker' can be reused for project 'id' bool CanReuse(ProjectId id)