8206122: Use Queue in place of ArrayList when need to remove first element

Reviewed-by: martin, jjg, vromero
This commit is contained in:
Ivan Gerasimov 2018-07-12 11:32:14 -07:00
parent 051dfe9fb4
commit 3496969f66
2 changed files with 40 additions and 41 deletions

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -27,10 +27,22 @@ package com.sun.tools.javac.api;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.Writer; import java.io.Writer;
import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashMap; 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.ClassTree;
import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.CompilationUnitTree;
@ -53,21 +65,8 @@ import com.sun.tools.javac.comp.Enter;
import com.sun.tools.javac.comp.Modules; import com.sun.tools.javac.comp.Modules;
import com.sun.tools.javac.main.Arguments; import com.sun.tools.javac.main.Arguments;
import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.main.JavaCompiler;
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.model.JavacElements;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.DefinedBy; import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api; import com.sun.tools.javac.util.DefinedBy.Api;
@ -101,9 +100,10 @@ import com.sun.tools.javac.util.Log;
public class JavacTaskPool { public class JavacTaskPool {
private static final JavacTool systemProvider = JavacTool.create(); private static final JavacTool systemProvider = JavacTool.create();
private static final Queue<ReusableContext> EMPTY_QUEUE = new ArrayDeque<>(0);
private final int maxPoolSize; 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 id;
private int statReused = 0; private int statReused = 0;
@ -159,14 +159,14 @@ public class JavacTaskPool {
ReusableContext ctx; ReusableContext ctx;
synchronized (this) { synchronized (this) {
List<ReusableContext> cached = Queue<ReusableContext> cached =
options2Contexts.getOrDefault(opts, Collections.emptyList()); options2Contexts.getOrDefault(opts, EMPTY_QUEUE);
if (cached.isEmpty()) { if (cached.isEmpty()) {
ctx = new ReusableContext(opts); ctx = new ReusableContext(opts);
statNew++; statNew++;
} else { } else {
ctx = cached.remove(0); ctx = cached.remove();
statReused++; statReused++;
} }
} }
@ -200,7 +200,7 @@ public class JavacTaskPool {
options2Contexts.get(toRemove.arguments).remove(toRemove); options2Contexts.get(toRemove.arguments).remove(toRemove);
statRemoved++; statRemoved++;
} }
options2Contexts.computeIfAbsent(ctx.arguments, x -> new ArrayList<>()).add(ctx); options2Contexts.computeIfAbsent(ctx.arguments, x -> new ArrayDeque<>()).add(ctx);
ctx.timeStamp = id++; ctx.timeStamp = id++;
} }
} }

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,11 +25,20 @@
package com.sun.tools.javac.comp; 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.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;
import com.sun.tools.javac.code.Source.Feature; 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.Type;
import com.sun.tools.javac.code.Types; import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext; import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
@ -48,7 +57,9 @@ import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
import com.sun.tools.javac.tree.JCTree.JCNewClass; import com.sun.tools.javac.tree.JCTree.JCNewClass;
import com.sun.tools.javac.tree.JCTree.JCStatement; import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCSwitch; 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.JCTypeApply;
import com.sun.tools.javac.tree.JCTree.JCUnary;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.JCTree.JCWhileLoop; import com.sun.tools.javac.tree.JCTree.JCWhileLoop;
import com.sun.tools.javac.tree.JCTree.Tag; import com.sun.tools.javac.tree.JCTree.Tag;
@ -56,9 +67,11 @@ import com.sun.tools.javac.tree.TreeCopier;
import com.sun.tools.javac.tree.TreeInfo; import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.tree.TreeScanner; 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.Context;
import com.sun.tools.javac.util.DefinedBy; import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api; 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.JCDiagnostic.DiagnosticType;
import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer; import com.sun.tools.javac.util.ListBuffer;
@ -66,20 +79,6 @@ import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Options; import com.sun.tools.javac.util.Options;
import com.sun.tools.javac.util.Position; 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.Flags.GENERATEDCONSTR;
import static com.sun.tools.javac.code.TypeTag.CLASS; import static com.sun.tools.javac.code.TypeTag.CLASS;
import static com.sun.tools.javac.tree.JCTree.Tag.APPLY; import static com.sun.tools.javac.tree.JCTree.Tag.APPLY;
@ -521,11 +520,11 @@ public class Analyzer {
*/ */
DeferredAnalysisHelper queueDeferredHelper = new DeferredAnalysisHelper() { DeferredAnalysisHelper queueDeferredHelper = new DeferredAnalysisHelper() {
Map<ClassSymbol, ArrayList<RewritingContext>> Q = new HashMap<>(); Map<ClassSymbol, Queue<RewritingContext>> Q = new HashMap<>();
@Override @Override
public void queue(RewritingContext rewriting) { 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); s.add(rewriting);
} }
@ -535,9 +534,9 @@ public class Analyzer {
DeferredAnalysisHelper prevHelper = deferredAnalysisHelper; DeferredAnalysisHelper prevHelper = deferredAnalysisHelper;
try { try {
deferredAnalysisHelper = flushDeferredHelper; 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()) { while (rewritings != null && !rewritings.isEmpty()) {
doAnalysis(rewritings.remove(0)); doAnalysis(rewritings.remove());
} }
} finally { } finally {
deferredAnalysisHelper = prevHelper; deferredAnalysisHelper = prevHelper;