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

DYN-4228 Convert StrongConnectComponent to use List vs Dictionary input type #10355

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/Engine/ProtoAssociative/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ private GraphNode GetFinanlStatementOfSSA(GraphNode subNode, ref int index)
// http://en.wikipedia.org/wiki/Tarjan's_strongly_connected_components_algorithm
//
// Returns the first strongly connected component
private List<GraphNode> StrongConnectComponent(GraphNode node, ref int index, Dictionary<GraphNode, int> lowlinkMap, Dictionary<GraphNode, int> indexMap, Stack<GraphNode> S)
private List<GraphNode> StrongConnectComponent(GraphNode node, ref int index, Dictionary<int, int> lowlinkMap, int[] indexList, Stack<GraphNode> S)
{
indexMap[node] = index;
lowlinkMap[node] = index;
indexList[node.dependencyGraphListID] = index;
lowlinkMap[node.dependencyGraphListID] = index;
aparajit-pratap marked this conversation as resolved.
Show resolved Hide resolved
index++;
S.Push(node);

Expand Down Expand Up @@ -300,18 +300,18 @@ private List<GraphNode> StrongConnectComponent(GraphNode node, ref int index, Di

subNode = GetFinanlStatementOfSSA(subNode, ref n);

if (indexMap[subNode] == Constants.kInvalidIndex)
if (indexList[subNode.dependencyGraphListID] == Constants.kInvalidIndex)
{
StrongConnectComponent(subNode, ref index, lowlinkMap, indexMap, S);
lowlinkMap[node] = Math.Min(lowlinkMap[node], lowlinkMap[subNode]);
StrongConnectComponent(subNode, ref index, lowlinkMap, indexList, S);
lowlinkMap[node.dependencyGraphListID] = Math.Min(lowlinkMap[node.dependencyGraphListID], lowlinkMap[subNode.dependencyGraphListID]);
}
else if (S.Contains(subNode))
{
lowlinkMap[node] = Math.Min(lowlinkMap[node], indexMap[subNode]);
lowlinkMap[node.dependencyGraphListID] = Math.Min(lowlinkMap[node.dependencyGraphListID], indexList[subNode.dependencyGraphListID]);
}
}

if (lowlinkMap[node] == indexMap[node] && S.Count > 1)
if (lowlinkMap[node.dependencyGraphListID] == indexList[node.dependencyGraphListID] && S.Count > 1)
{
List<GraphNode> dependencyList = new List<GraphNode>();
while (true)
Expand All @@ -338,17 +338,17 @@ private bool CyclicDependencyTest(GraphNode node, ref SymbolNode cyclicSymbol1,
return true;
}

var indexMap = new Dictionary<GraphNode, int>();
var lowlinkMap = new Dictionary<GraphNode, int>();
var indexList = new int[codeBlock.instrStream.dependencyGraph.GraphList.Count];
Copy link
Contributor

@aparajit-pratap aparajit-pratap Feb 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the actual place where we're getting the perf improvement; since you are pre-allocating the size of the array in this case rather than initializing an empty dictionary that needs to be expanded every other time values are added to it?

var lowlinkMap = new Dictionary<int, int>();
var s = new Stack<GraphNode>();
int index = 0;

foreach (var subNode in codeBlock.instrStream.dependencyGraph.GraphList)
for (int n = 0; n < codeBlock.instrStream.dependencyGraph.GraphList.Count; ++n)
{
indexMap[subNode] = Constants.kInvalidIndex;
indexList[n] = Constants.kInvalidIndex;
}

var dependencyList = StrongConnectComponent(node, ref index, lowlinkMap, indexMap, s);
var dependencyList = StrongConnectComponent(node, ref index, lowlinkMap, indexList, s);
if (dependencyList == null)
{
return true;
Expand Down