Skip to content

Commit 787b913

Browse files
lauraharkercopybara-github
authored andcommitted
Decrease size of astPosition map in ControlFlowAnalysis
This change probably still adds some nodes into astPosition that aren't needed, but should decrease the # of such nodes. PiperOrigin-RevId: 507637302
1 parent 70f34d2 commit 787b913

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/com/google/javascript/jscomp/ControlFlowAnalysis.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.javascript.jscomp;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
1920
import static com.google.common.base.Preconditions.checkState;
2021

2122
import com.google.common.base.Preconditions;
@@ -71,7 +72,9 @@ public final class ControlFlowAnalysis implements NodeTraversal.Callback {
7172
// CFG nodes that come first lexically should be visited first, because
7273
// they will often be executed first in the source program.
7374
private final Comparator<DiGraphNode<Node, Branch>> priorityComparator =
74-
Comparator.comparingInt(digraphNode -> astPosition.get(digraphNode.getValue()));
75+
Comparator.comparingInt(
76+
digraphNode ->
77+
checkNotNull(astPosition.get(digraphNode.getValue()), digraphNode.getValue()));
7578

7679
private int astPositionCounter;
7780
private int priorityCounter;
@@ -257,7 +260,22 @@ private void prioritizeFromEntryNode(DiGraphNode<Node, Branch> entry) {
257260

258261
@Override
259262
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent) {
260-
astPosition.put(n, astPositionCounter++);
263+
if (shouldTraverseIntoChildren(n, parent)) {
264+
// Any AST node that will later have a corresponding CFG node must be in astPosition.
265+
// To avoid having astPosition grow too large, we exclude AST nodes that are not traversed
266+
// further, as they usually are not put in the CFG.
267+
astPosition.put(n, astPositionCounter++);
268+
return true;
269+
}
270+
return false;
271+
}
272+
273+
/**
274+
* Returns whether the children of this node should be traversed as part of this control-flow
275+
* analysis, i.e. whether the control-flow graph being built may require any edges into children
276+
* of this node.
277+
*/
278+
private boolean shouldTraverseIntoChildren(Node n, Node parent) {
261279
switch (n.getToken()) {
262280
case FUNCTION:
263281
if (shouldTraverseFunctions || n == cfg.getEntry().getValue()) {
@@ -295,7 +313,13 @@ public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent)
295313
case FOR_OF:
296314
case FOR_AWAIT_OF:
297315
// Only traverse the body of the for loop.
298-
return n == parent.getLastChild();
316+
boolean shouldTraverseForChild = n == parent.getLastChild();
317+
if (!shouldTraverseForChild) {
318+
// The control-flow graph contains edges from a FOR node to all its children, even
319+
// though only the body is actually traversed. So put the other children in astPosition.
320+
astPosition.put(n, astPositionCounter++);
321+
}
322+
return shouldTraverseForChild;
299323

300324
case DO:
301325
// Only traverse the body of the do-while.

0 commit comments

Comments
 (0)