Skip to content

Commit

Permalink
Fix "Array dimensions exceeded supported range" exception (#5343)
Browse files Browse the repository at this point in the history
Hitting this on a quickbuild repo with ~550 projects and ~19K edges (due to transitive edges).

The problem was using an intermediary list to aggregate intermediary results, which exploded exponentially in size due to duplicates. Fix is to use a hash set.

The failure can't really be tested (without stress tests) because it appears in an intermediary list which finally gets dumped into a set, so there's no observable trace of the problem at the end of graph construction.

This fix avoids the exception and takes graph construction down from 5 minutes to 30 seconds on that specific repo.
  • Loading branch information
cdmihai authored May 8, 2020
1 parent b861333 commit 26f6d1d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
16 changes: 11 additions & 5 deletions src/Build.UnitTests/Graph/GraphTestingUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
Expand Down Expand Up @@ -218,13 +219,18 @@ internal static TransientTestFile CreateProjectFile(

internal static IEnumerable<ProjectGraphNode> ComputeClosure(ProjectGraphNode node)
{
foreach (var reference in node.ProjectReferences)
{
yield return reference;
return ComputeClosureRecursive(node).ToHashSet();

foreach (var closureReference in ComputeClosure(reference))
IEnumerable<ProjectGraphNode> ComputeClosureRecursive(ProjectGraphNode projectGraphNode)
{
foreach (var reference in projectGraphNode.ProjectReferences)
{
yield return closureReference;
yield return reference;

foreach (var closureReference in ComputeClosureRecursive(reference))
{
yield return closureReference;
}
}
}
}
Expand Down
21 changes: 16 additions & 5 deletions src/Build.UnitTests/Graph/ProjectGraph_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,19 @@ public static IEnumerable<object[]> Graphs
}
};

yield return new object[]
{
new Dictionary<int, int[]>
{
{1, new []{2, 4, 3, 5} },
{2, new []{5} },
{3, new []{5} },
{4, new []{6} },
{5, new []{7} },
{6, new []{5} }
},
};

yield return new object[]
{
new Dictionary<int, int[]>
Expand Down Expand Up @@ -1996,11 +2009,9 @@ public void GraphShouldSupportTransitiveReferences(Dictionary<int, int[]> edges)

foreach (var node in graph.ProjectNodes)
{
foreach (var closureReference in ComputeClosure(node))
{
node.ProjectReferences.ShouldContain(closureReference);
closureReference.ReferencingProjects.ShouldContain(node);
}
var expectedClosure = ComputeClosure(node);

node.ProjectReferences.ShouldBeSameIgnoringOrder(expectedClosure);
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/Build/Graph/GraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private void AddEdges(Dictionary<ConfigurationMetadata, ParsedProject> allParsed

private void AddEdgesFromProjectReferenceItems(Dictionary<ConfigurationMetadata, ParsedProject> allParsedProjects, GraphEdges edges)
{
var transitiveReferenceCache = new Dictionary<ProjectGraphNode, List<ProjectGraphNode>>(allParsedProjects.Count);
var transitiveReferenceCache = new Dictionary<ProjectGraphNode, HashSet<ProjectGraphNode>>(allParsedProjects.Count);

foreach (var parsedProject in allParsedProjects)
{
Expand All @@ -143,7 +143,7 @@ private void AddEdgesFromProjectReferenceItems(Dictionary<ConfigurationMetadata,
// Add transitive references only if the project requires it.
if (requiresTransitiveProjectReferences)
{
foreach (var transitiveProjectReference in GetTransitiveProjectReferences(allParsedProjects[referenceInfo.ReferenceConfiguration]))
foreach (var transitiveProjectReference in GetTransitiveProjectReferencesExcludingSelf(allParsedProjects[referenceInfo.ReferenceConfiguration]))
{
currentNode.AddProjectReference(
transitiveProjectReference,
Expand All @@ -160,20 +160,24 @@ private void AddEdgesFromProjectReferenceItems(Dictionary<ConfigurationMetadata,
}
}

List<ProjectGraphNode> GetTransitiveProjectReferences(ParsedProject parsedProject)
HashSet<ProjectGraphNode> GetTransitiveProjectReferencesExcludingSelf(ParsedProject parsedProject)
{
if (transitiveReferenceCache.TryGetValue(parsedProject.GraphNode, out List<ProjectGraphNode> cachedTransitiveReferences))
if (transitiveReferenceCache.TryGetValue(parsedProject.GraphNode, out HashSet<ProjectGraphNode> cachedTransitiveReferences))
{
return cachedTransitiveReferences;
}
else
{
var transitiveReferences = new List<ProjectGraphNode>();
var transitiveReferences = new HashSet<ProjectGraphNode>();

foreach (var referenceInfo in parsedProject.ReferenceInfos)
{
transitiveReferences.Add(allParsedProjects[referenceInfo.ReferenceConfiguration].GraphNode);
transitiveReferences.AddRange(GetTransitiveProjectReferences(allParsedProjects[referenceInfo.ReferenceConfiguration]));

foreach (var transitiveReference in GetTransitiveProjectReferencesExcludingSelf(allParsedProjects[referenceInfo.ReferenceConfiguration]))
{
transitiveReferences.Add(transitiveReference);
}
}

transitiveReferenceCache.Add(parsedProject.GraphNode, transitiveReferences);
Expand Down

0 comments on commit 26f6d1d

Please sign in to comment.