Skip to content

Commit

Permalink
Replace Hash(Map|Set) with LinkedHash(Map|Set) in several passes
Browse files Browse the repository at this point in the history
It is preferable to consistently use the Linked* version of these classes,
because that will force a deterministic iteration order for them
(order of insertion), instead of a non-deterministic order, which could
lead to non-deterministic compiler output.

PiperOrigin-RevId: 543464824
  • Loading branch information
brad4d authored and copybara-github committed Jun 26, 2023
1 parent 9d71f37 commit 8aaae26
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.rhino.Node;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -94,7 +94,7 @@ public void process(Node externs, Node root) {
/** Collects all references in a naive way. */
private static class CollectReferences {

private final Set<String> currFuncReferences = new HashSet<>();
private final Set<String> currFuncReferences = new LinkedHashSet<>();

void collect(Node fn) {
checkState(fn.isFunction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
Expand All @@ -56,9 +55,9 @@ public final class Es6RewriteBlockScopedDeclaration extends AbstractPostOrderCal
private final AbstractCompiler compiler;
private final AstFactory astFactory;
private final Table<Node, String, String> renameTable = HashBasedTable.create();
private final Set<Node> letConsts = new HashSet<>();
private final Set<String> undeclaredNames = new HashSet<>();
private final Set<String> externNames = new HashSet<>();
private final Set<Node> letConsts = new LinkedHashSet<>();
private final Set<String> undeclaredNames = new LinkedHashSet<>();
private final Set<String> externNames = new LinkedHashSet<>();
private static final FeatureSet transpiledFeatures =
FeatureSet.BARE_MINIMUM.with(Feature.LET_DECLARATIONS, Feature.CONST_DECLARATIONS);
private final Supplier<String> uniqueNameIdSupplier;
Expand Down
6 changes: 4 additions & 2 deletions src/com/google/javascript/jscomp/Es6RewriteGenerators.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import org.jspecify.nullness.Nullable;

/**
Expand Down Expand Up @@ -1122,13 +1124,13 @@ private boolean isEndOfBlockUnreachable(Node block) {

/** State machine context that is used during generator function transpilation. */
private class TranspilationContext {
final HashMap<String, LabelCases> namedLabels = new HashMap<>();
final HashMap<String, LabelCases> namedLabels = new LinkedHashMap<>();
final ArrayDeque<Case> breakCases = new ArrayDeque<>();
final ArrayDeque<Case> continueCases = new ArrayDeque<>();

final ArrayDeque<CatchCase> catchCases = new ArrayDeque<>();
final ArrayDeque<Case> finallyCases = new ArrayDeque<>();
final HashSet<String> catchNames = new HashSet<>();
final HashSet<String> catchNames = new LinkedHashSet<>();

/** All "case" sections that will be added to generator program. */
final ArrayList<Case> allCases = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -86,7 +86,7 @@ private static class Traversal implements NodeTraversal.Callback {
private final ListMultimap<Node, Node> hoistNodesToScope = ArrayListMultimap.create();

// Map for recording diagnostic information about what was marked for eager compilation.
private final Map<String, Long> chunkToEagerCompileFnCounts = new HashMap<>();
private final Map<String, Long> chunkToEagerCompileFnCounts = new LinkedHashMap<>();

public Traversal(Set<String> parenthesizeFunctionsInChunks) {
this.parenthesizeFunctionsInChunks = parenthesizeFunctionsInChunks;
Expand Down
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/VariableReferenceCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.QualifiedName;
import com.google.javascript.rhino.Token;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.jspecify.nullness.Nullable;
Expand Down Expand Up @@ -79,7 +79,7 @@ class VariableReferenceCheck implements CompilerPass {

// NOTE(nicksantos): It's a lot faster to use a shared Set that
// we clear after each method call, because the Set never gets too big.
private final Set<BasicBlock> blocksWithDeclarations = new HashSet<>();
private final Set<BasicBlock> blocksWithDeclarations = new LinkedHashSet<>();

// These types do not permit a block-scoped declaration inside them without an explicit block.
// e.g. if (b) let x;
Expand Down Expand Up @@ -112,7 +112,7 @@ private class ReferenceCheckingBehavior implements Behavior {
private final Set<String> varsInFunctionBody;

private ReferenceCheckingBehavior() {
varsInFunctionBody = new HashSet<>();
varsInFunctionBody = new LinkedHashSet<>();
}

@Override
Expand Down

0 comments on commit 8aaae26

Please sign in to comment.