Skip to content

Commit

Permalink
eliminate more allocations using null logic instead of Optional<Node>
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=257516547
  • Loading branch information
nanaze authored and tjgq committed Jul 11, 2019
1 parent c86b74d commit 3c46073
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/com/google/javascript/jscomp/NodeUtil.java
Expand Up @@ -56,7 +56,6 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -4357,33 +4356,35 @@ public static Iterable<Node> preOrderIterable(Node root) {
private static final class PreOrderIterator extends AbstractIterator<Node> {

private final Predicate<Node> traverseNodePredicate;
private Optional<Node> current;

@Nullable private Node current;

public PreOrderIterator(Node root, Predicate<Node> traverseNodePredicate) {
Preconditions.checkNotNull(root);
this.traverseNodePredicate = traverseNodePredicate;
this.current = Optional.of(root);
this.current = root;
}

@Override
protected Node computeNext() {
if (!current.isPresent()) {
if (current == null) {
return endOfData();
}

Node returnValue = current.get();
Node returnValue = current;
current = calculateNextNode(returnValue);
return returnValue;
}

private Optional<Node> calculateNextNode(Node currentNode) {
@Nullable
private Node calculateNextNode(Node currentNode) {
Preconditions.checkNotNull(currentNode);

// If node does not match the predicate, do not descend into it.
if (traverseNodePredicate.apply(currentNode)) {

// In prefix order, the next node is the leftmost child.
if (currentNode.hasChildren()) {
return Optional.of(currentNode.getFirstChild());
return currentNode.getFirstChild();
}
}

Expand All @@ -4396,13 +4397,13 @@ private Optional<Node> calculateNextNode(Node currentNode) {
while (currentNode != null) {
Node next = currentNode.getNext();
if (next != null) {
return Optional.of(next);
return next;
}

currentNode = currentNode.getParent();
}

return Optional.empty();
return null;
}
}

Expand Down

0 comments on commit 3c46073

Please sign in to comment.