8206122: Use Queue in place of ArrayList when need to remove first element
authorigerasim
Thu, 12 Jul 2018 11:32:14 -0700
changeset 51045 215d1a5b097a
parent 51044 68646e6522ca
child 51046 69634e97740c
8206122: Use Queue in place of ArrayList when need to remove first element Reviewed-by: martin, jjg, vromero
src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java	Thu Jul 12 11:18:01 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java	Thu Jul 12 11:32:14 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -27,10 +27,22 @@
 
 import java.io.PrintStream;
 import java.io.Writer;
+import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+
+import javax.tools.Diagnostic;
+import javax.tools.DiagnosticListener;
+import javax.tools.JavaFileManager;
+import javax.tools.JavaFileObject;
 
 import com.sun.source.tree.ClassTree;
 import com.sun.source.tree.CompilationUnitTree;
@@ -53,21 +65,8 @@
 import com.sun.tools.javac.comp.Modules;
 import com.sun.tools.javac.main.Arguments;
 import com.sun.tools.javac.main.JavaCompiler;
+import com.sun.tools.javac.model.JavacElements;
 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
-
-import javax.tools.Diagnostic;
-import javax.tools.DiagnosticListener;
-import javax.tools.JavaFileManager;
-import javax.tools.JavaFileObject;
-
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.stream.Collectors;
-import java.util.stream.StreamSupport;
-
-import com.sun.tools.javac.model.JavacElements;
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.DefinedBy;
 import com.sun.tools.javac.util.DefinedBy.Api;
@@ -101,9 +100,10 @@
 public class JavacTaskPool {
 
     private static final JavacTool systemProvider = JavacTool.create();
+    private static final Queue<ReusableContext> EMPTY_QUEUE = new ArrayDeque<>(0);
 
     private final int maxPoolSize;
-    private final Map<List<String>, List<ReusableContext>> options2Contexts = new HashMap<>();
+    private final Map<List<String>, Queue<ReusableContext>> options2Contexts = new HashMap<>();
     private int id;
 
     private int statReused = 0;
@@ -159,14 +159,14 @@
         ReusableContext ctx;
 
         synchronized (this) {
-            List<ReusableContext> cached =
-                    options2Contexts.getOrDefault(opts, Collections.emptyList());
+            Queue<ReusableContext> cached =
+                    options2Contexts.getOrDefault(opts, EMPTY_QUEUE);
 
             if (cached.isEmpty()) {
                 ctx = new ReusableContext(opts);
                 statNew++;
             } else {
-                ctx = cached.remove(0);
+                ctx = cached.remove();
                 statReused++;
             }
         }
@@ -200,7 +200,7 @@
                     options2Contexts.get(toRemove.arguments).remove(toRemove);
                     statRemoved++;
                 }
-                options2Contexts.computeIfAbsent(ctx.arguments, x -> new ArrayList<>()).add(ctx);
+                options2Contexts.computeIfAbsent(ctx.arguments, x -> new ArrayDeque<>()).add(ctx);
                 ctx.timeStamp = id++;
             }
         }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java	Thu Jul 12 11:18:01 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java	Thu Jul 12 11:32:14 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,11 +25,20 @@
 
 package com.sun.tools.javac.comp;
 
-import java.util.ArrayList;
+import java.util.ArrayDeque;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Queue;
+import java.util.function.Predicate;
 
 import com.sun.source.tree.LambdaExpressionTree;
+import com.sun.source.tree.NewClassTree;
+import com.sun.tools.javac.code.Flags;
+import com.sun.tools.javac.code.Kinds.Kind;
 import com.sun.tools.javac.code.Source;
 import com.sun.tools.javac.code.Source.Feature;
+import com.sun.tools.javac.code.Symbol.ClassSymbol;
 import com.sun.tools.javac.code.Type;
 import com.sun.tools.javac.code.Types;
 import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
@@ -48,7 +57,9 @@
 import com.sun.tools.javac.tree.JCTree.JCNewClass;
 import com.sun.tools.javac.tree.JCTree.JCStatement;
 import com.sun.tools.javac.tree.JCTree.JCSwitch;
+import com.sun.tools.javac.tree.JCTree.JCTry;
 import com.sun.tools.javac.tree.JCTree.JCTypeApply;
+import com.sun.tools.javac.tree.JCTree.JCUnary;
 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
 import com.sun.tools.javac.tree.JCTree.JCWhileLoop;
 import com.sun.tools.javac.tree.JCTree.Tag;
@@ -56,9 +67,11 @@
 import com.sun.tools.javac.tree.TreeInfo;
 import com.sun.tools.javac.tree.TreeMaker;
 import com.sun.tools.javac.tree.TreeScanner;
+import com.sun.tools.javac.util.Assert;
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.DefinedBy;
 import com.sun.tools.javac.util.DefinedBy.Api;
+import com.sun.tools.javac.util.DiagnosticSource;
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
 import com.sun.tools.javac.util.List;
 import com.sun.tools.javac.util.ListBuffer;
@@ -66,20 +79,6 @@
 import com.sun.tools.javac.util.Options;
 import com.sun.tools.javac.util.Position;
 
-import java.util.EnumSet;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.function.Predicate;
-
-import com.sun.source.tree.NewClassTree;
-import com.sun.tools.javac.code.Flags;
-import com.sun.tools.javac.code.Kinds.Kind;
-import com.sun.tools.javac.code.Symbol.ClassSymbol;
-import com.sun.tools.javac.tree.JCTree.JCTry;
-import com.sun.tools.javac.tree.JCTree.JCUnary;
-import com.sun.tools.javac.util.Assert;
-import com.sun.tools.javac.util.DiagnosticSource;
-
 import static com.sun.tools.javac.code.Flags.GENERATEDCONSTR;
 import static com.sun.tools.javac.code.TypeTag.CLASS;
 import static com.sun.tools.javac.tree.JCTree.Tag.APPLY;
@@ -521,11 +520,11 @@
      */
     DeferredAnalysisHelper queueDeferredHelper = new DeferredAnalysisHelper() {
 
-        Map<ClassSymbol, ArrayList<RewritingContext>> Q = new HashMap<>();
+        Map<ClassSymbol, Queue<RewritingContext>> Q = new HashMap<>();
 
         @Override
         public void queue(RewritingContext rewriting) {
-            ArrayList<RewritingContext> s = Q.computeIfAbsent(rewriting.env.enclClass.sym.outermostClass(), k -> new ArrayList<>());
+            Queue<RewritingContext> s = Q.computeIfAbsent(rewriting.env.enclClass.sym.outermostClass(), k -> new ArrayDeque<>());
             s.add(rewriting);
         }
 
@@ -535,9 +534,9 @@
                 DeferredAnalysisHelper prevHelper = deferredAnalysisHelper;
                 try {
                     deferredAnalysisHelper = flushDeferredHelper;
-                    ArrayList<RewritingContext> rewritings = Q.get(flushEnv.enclClass.sym.outermostClass());
+                    Queue<RewritingContext> rewritings = Q.get(flushEnv.enclClass.sym.outermostClass());
                     while (rewritings != null && !rewritings.isEmpty()) {
-                        doAnalysis(rewritings.remove(0));
+                        doAnalysis(rewritings.remove());
                     }
                 } finally {
                     deferredAnalysisHelper = prevHelper;