Skip to content

Commit

Permalink
Use LinkedHash(Map|Set) in more places for deterministic behavior
Browse files Browse the repository at this point in the history
It's important that the compiler always iterate over maps and sets
in a deterministic order.

PiperOrigin-RevId: 563512674
  • Loading branch information
brad4d authored and copybara-github committed Sep 7, 2023
1 parent 49501b2 commit d57b3c4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -107,7 +105,7 @@ public final class ConcretizeStaticInheritanceForInlining implements CompilerPas
static final DiagnosticType DUPLICATE_CLASS =
DiagnosticType.error("DUPLICATE_CLASS", "Multiple classes cannot share the same name: {0}");

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

// Property names that may cause issues if they are concretized.
private static final ImmutableSet<String> BANNED_PROP_NAMES =
Expand All @@ -117,7 +115,7 @@ private static class JavascriptClass {
// All static members to the class including get set properties.
private final Set<Node> staticMembers = new LinkedHashSet<>();
// Keep updated the set of static member names to avoid O(n^2) searches.
private final Set<String> staticMemberNames = new HashSet<>();
private final Set<String> staticMemberNames = new LinkedHashSet<>();
// Collect all the static field accesses to the class.
private final Set<Node> staticFieldAccess = new LinkedHashSet<>();
// Collect all get set properties as defined by Object.defineProperties(...)
Expand Down Expand Up @@ -203,7 +201,9 @@ private void copyDeclarations(
}

private void copyStaticMembers(
JavascriptClass superClass, JavascriptClass subClass, Node inheritsCall,
JavascriptClass superClass,
JavascriptClass subClass,
Node inheritsCall,
FindStaticMembers findStaticMembers) {
for (Node staticMember : superClass.staticMembers) {
checkState(staticMember.isAssign(), staticMember);
Expand Down Expand Up @@ -276,7 +276,7 @@ private class FindStaticMembers extends AbstractPostOrderCallback {
final List<Node> inheritsCalls = new ArrayList<>();
// Store the order we find class definitions and static fields. Copied statics must occur
// after both the namespace and the copied property are defined.
final Map<Node, Integer> nodeOrder = new HashMap<>();
final Map<Node, Integer> nodeOrder = new LinkedHashMap<>();

@Override
public void visit(NodeTraversal t, Node n, Node parent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -1299,7 +1298,7 @@ class CollapseProperties implements CompilerPass {
/** Maps names (e.g. "a.b.c") to nodes in the global namespace tree */
private Map<String, Name> nameMap;

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

@Override
public void process(Node externs, Node root) {
Expand Down Expand Up @@ -1365,7 +1364,8 @@ private boolean canEliminate(Name name) {
*/
private ImmutableSet<Name> checkNamespaces() {
ImmutableSet.Builder<Name> escaped = ImmutableSet.builder();
HashSet<String> dynamicallyImportedModuleRefs = new HashSet<>(dynamicallyImportedModules);
LinkedHashSet<String> dynamicallyImportedModuleRefs =
new LinkedHashSet<>(dynamicallyImportedModules);
if (!dynamicallyImportedModules.isEmpty()) {
// When the output chunk type is ES_MODULES, properties of the module namespace
// must not be collapsed as they are referenced off the namespace object. The namespace
Expand Down

0 comments on commit d57b3c4

Please sign in to comment.