Skip to content

Commit

Permalink
Optimize out empty ifs
Browse files Browse the repository at this point in the history
If an if statement has no true or false sub-statements, there is no point including the if statement in the LIR graph. So instead of constructing a sub-graph for the if statement — which would include an empty if vertex and nothing else — and returning it, we return an empty graph object.

Fixes elastic#9314
  • Loading branch information
ycombinator committed Apr 4, 2018
1 parent 7242572 commit 9859ec2
Showing 1 changed file with 6 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public Graph toGraph() throws InvalidIRException {
Graph trueGraph = getTrueStatement().toGraph();
Graph falseGraph = getFalseStatement().toGraph();

// If there is nothing in the true or false sections of this if statement,
// we can omit the if statement altogether!
if (trueGraph.isEmpty() && falseGraph.isEmpty()) {
return new Graph();
}

Graph.GraphCombinationResult combination = Graph.combine(trueGraph, falseGraph);
Graph newGraph = combination.graph;
Collection<Vertex> trueRoots = trueGraph.roots().map(combination.oldToNewVertices::get).collect(Collectors.toList());
Expand Down

0 comments on commit 9859ec2

Please sign in to comment.