src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java
author jlahoda
Mon, 21 Oct 2019 15:38:26 +0200
changeset 58713 ad69fd32778e
parent 51045 215d1a5b097a
permissions -rw-r--r--
8226585: Improve javac messages for using a preview API Summary: Avoiding deprecation for removal for APIs associated with preview features, the features are marked with an annotation, and errors/warnings are produced for them based on the annotation. Reviewed-by: erikj, mcimadamore, alanb Contributed-by: joe.darcy@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     1
/*
51045
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     4
 *
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
48373
f9152f462cbc 8193758: Update copyright headers of files in src tree that are missing Classpath exception
alanb
parents: 47350
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f9152f462cbc 8193758: Update copyright headers of files in src tree that are missing Classpath exception
alanb
parents: 47350
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f9152f462cbc 8193758: Update copyright headers of files in src tree that are missing Classpath exception
alanb
parents: 47350
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    10
 *
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    15
 * accompanied this code).
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    16
 *
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    20
 *
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    23
 * questions.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    24
 */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    25
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    26
package com.sun.tools.javac.api;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    27
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    28
import java.io.PrintStream;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    29
import java.io.Writer;
51045
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    30
import java.util.ArrayDeque;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    31
import java.util.ArrayList;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    32
import java.util.Collection;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    33
import java.util.HashMap;
51045
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    34
import java.util.HashSet;
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    35
import java.util.List;
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    36
import java.util.Map;
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    37
import java.util.Queue;
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    38
import java.util.Set;
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    39
import java.util.stream.Collectors;
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    40
import java.util.stream.StreamSupport;
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    41
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    42
import javax.tools.Diagnostic;
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    43
import javax.tools.DiagnosticListener;
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    44
import javax.tools.JavaFileManager;
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    45
import javax.tools.JavaFileObject;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    46
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    47
import com.sun.source.tree.ClassTree;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    48
import com.sun.source.tree.CompilationUnitTree;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    49
import com.sun.source.util.JavacTask;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    50
import com.sun.source.util.TaskEvent;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    51
import com.sun.source.util.TaskEvent.Kind;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    52
import com.sun.source.util.TaskListener;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    53
import com.sun.source.util.TreeScanner;
32547
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
    54
import com.sun.tools.javac.code.Kinds;
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 51045
diff changeset
    55
import com.sun.tools.javac.code.Preview;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    56
import com.sun.tools.javac.code.Symbol;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    57
import com.sun.tools.javac.code.Symtab;
32547
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
    58
import com.sun.tools.javac.code.Type;
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
    59
import com.sun.tools.javac.code.Type.ClassType;
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
    60
import com.sun.tools.javac.code.TypeTag;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    61
import com.sun.tools.javac.code.Types;
43269
12f989542165 8171098: NPE when --add-modules java.corba is used
jlahoda
parents: 40232
diff changeset
    62
import com.sun.tools.javac.comp.Annotate;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    63
import com.sun.tools.javac.comp.Check;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    64
import com.sun.tools.javac.comp.CompileStates;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    65
import com.sun.tools.javac.comp.Enter;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 36526
diff changeset
    66
import com.sun.tools.javac.comp.Modules;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    67
import com.sun.tools.javac.main.Arguments;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    68
import com.sun.tools.javac.main.JavaCompiler;
51045
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
    69
import com.sun.tools.javac.model.JavacElements;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    70
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    71
import com.sun.tools.javac.util.Context;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    72
import com.sun.tools.javac.util.DefinedBy;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    73
import com.sun.tools.javac.util.DefinedBy.Api;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    74
import com.sun.tools.javac.util.Log;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    75
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    76
/**
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    77
 * A pool of reusable JavacTasks. When a task is no valid anymore, it is returned to the pool,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    78
 * and its Context may be reused for future processing in some cases. The reuse is achieved
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    79
 * by replacing some components (most notably JavaCompiler and Log) with reusable counterparts,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    80
 * and by cleaning up leftovers from previous compilation.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    81
 * <p>
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    82
 * For each combination of options, a separate task/context is created and kept, as most option
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    83
 * values are cached inside components themselves.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    84
 * <p>
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    85
 * When the compilation redefines sensitive classes (e.g. classes in the the java.* packages), the
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    86
 * task/context is not reused.
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    87
 * <p>
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    88
 * When the task is reused, then packages that were already listed won't be listed again.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    89
 * <p>
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    90
 * Care must be taken to only return tasks that won't be used by the original caller.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    91
 * <p>
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    92
 * Care must also be taken when custom components are installed, as those are not cleaned when the
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    93
 * task/context is reused, and subsequent getTask may return a task based on a context with these
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    94
 * custom components.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    95
 *
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    96
 * <p><b>This is NOT part of any supported API.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    97
 * If you write code that depends on this, you do so at your own risk.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    98
 * This code and its internal interfaces are subject to change or
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
    99
 * deletion without notice.</b>
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   100
 */
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   101
public class JavacTaskPool {
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   102
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   103
    private static final JavacTool systemProvider = JavacTool.create();
51045
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
   104
    private static final Queue<ReusableContext> EMPTY_QUEUE = new ArrayDeque<>(0);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   105
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   106
    private final int maxPoolSize;
51045
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
   107
    private final Map<List<String>, Queue<ReusableContext>> options2Contexts = new HashMap<>();
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   108
    private int id;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   109
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   110
    private int statReused = 0;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   111
    private int statNew = 0;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   112
    private int statPolluted = 0;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   113
    private int statRemoved = 0;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   114
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   115
    /**Creates the pool.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   116
     *
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   117
     * @param maxPoolSize maximum number of tasks/context that will be kept in the pool.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   118
     */
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   119
    public JavacTaskPool(int maxPoolSize) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   120
        this.maxPoolSize = maxPoolSize;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   121
    }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   122
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   123
    /**Creates a new task as if by {@link javax.tools.JavaCompiler#getTask} and runs the provided
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   124
     * worker with it. The task is only valid while the worker is running. The internal structures
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   125
     * may be reused from some previous compilation.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   126
     *
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   127
     * @param out a Writer for additional output from the compiler;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   128
     * use {@code System.err} if {@code null}
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   129
     * @param fileManager a file manager; if {@code null} use the
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   130
     * compiler's standard filemanager
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   131
     * @param diagnosticListener a diagnostic listener; if {@code
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   132
     * null} use the compiler's default method for reporting
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   133
     * diagnostics
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   134
     * @param options compiler options, {@code null} means no options
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   135
     * @param classes names of classes to be processed by annotation
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   136
     * processing, {@code null} means no class names
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   137
     * @param compilationUnits the compilation units to compile, {@code
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   138
     * null} means no compilation units
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   139
     * @param worker that should be run with the task
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   140
     * @return an object representing the compilation
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   141
     * @throws RuntimeException if an unrecoverable error
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   142
     * occurred in a user supplied component.  The
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   143
     * {@linkplain Throwable#getCause() cause} will be the error in
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   144
     * user code.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   145
     * @throws IllegalArgumentException if any of the options are invalid,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   146
     * or if any of the given compilation units are of other kind than
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   147
     * {@linkplain JavaFileObject.Kind#SOURCE source}
32547
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
   148
     */
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   149
    public <Z> Z getTask(Writer out,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   150
                         JavaFileManager fileManager,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   151
                         DiagnosticListener<? super JavaFileObject> diagnosticListener,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   152
                         Iterable<String> options,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   153
                         Iterable<String> classes,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   154
                         Iterable<? extends JavaFileObject> compilationUnits,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   155
                         Worker<Z> worker) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   156
        List<String> opts =
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   157
                StreamSupport.stream(options.spliterator(), false)
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   158
                             .collect(Collectors.toCollection(ArrayList::new));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   159
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   160
        ReusableContext ctx;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   161
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   162
        synchronized (this) {
51045
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
   163
            Queue<ReusableContext> cached =
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
   164
                    options2Contexts.getOrDefault(opts, EMPTY_QUEUE);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   165
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   166
            if (cached.isEmpty()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   167
                ctx = new ReusableContext(opts);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   168
                statNew++;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   169
            } else {
51045
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
   170
                ctx = cached.remove();
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   171
                statReused++;
32547
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
   172
            }
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
   173
        }
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
   174
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   175
        ctx.useCount++;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   176
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   177
        JavacTaskImpl task =
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   178
                (JavacTaskImpl) systemProvider.getTask(out, fileManager, diagnosticListener,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   179
                                                       opts, classes, compilationUnits, ctx);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   180
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   181
        task.addTaskListener(ctx);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   182
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   183
        Z result = worker.withTask(task);
32547
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
   184
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   185
        //not returning the context to the pool if task crashes with an exception
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   186
        //the task/context may be in a broken state
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   187
        ctx.clear();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   188
        if (ctx.polluted) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   189
            statPolluted++;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   190
        } else {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   191
            task.cleanup();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   192
            synchronized (this) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   193
                while (cacheSize() + 1 > maxPoolSize) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   194
                    ReusableContext toRemove =
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   195
                            options2Contexts.values()
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   196
                                            .stream()
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   197
                                            .flatMap(Collection::stream)
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   198
                                            .sorted((c1, c2) -> c1.timeStamp < c2.timeStamp ? -1 : 1)
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   199
                                            .findFirst()
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   200
                                            .get();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   201
                    options2Contexts.get(toRemove.arguments).remove(toRemove);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   202
                    statRemoved++;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   203
                }
51045
215d1a5b097a 8206122: Use Queue in place of ArrayList when need to remove first element
igerasim
parents: 48373
diff changeset
   204
                options2Contexts.computeIfAbsent(ctx.arguments, x -> new ArrayDeque<>()).add(ctx);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   205
                ctx.timeStamp = id++;
32547
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
   206
            }
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
   207
        }
dea8ac70d849 8135246: CheckAttributedTree silently generates spurious compiler error
mcimadamore
parents: 32454
diff changeset
   208
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   209
        return result;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   210
    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   211
    //where:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   212
        private long cacheSize() {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   213
            return options2Contexts.values().stream().flatMap(Collection::stream).count();
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   214
        }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   215
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   216
    public void printStatistics(PrintStream out) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   217
        out.println(statReused + " reused Contexts");
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   218
        out.println(statNew + " newly created Contexts");
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   219
        out.println(statPolluted + " polluted Contexts");
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   220
        out.println(statRemoved + " removed Contexts");
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   221
    }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   222
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   223
    public interface Worker<Z> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   224
        public Z withTask(JavacTask task);
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   225
    }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   226
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   227
    static class ReusableContext extends Context implements TaskListener {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   228
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   229
        Set<CompilationUnitTree> roots = new HashSet<>();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   230
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   231
        List<String> arguments;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   232
        boolean polluted = false;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   233
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   234
        int useCount;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   235
        long timeStamp;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   236
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   237
        ReusableContext(List<String> arguments) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   238
            super();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   239
            this.arguments = arguments;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   240
            put(Log.logKey, ReusableLog.factory);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   241
            put(JavaCompiler.compilerKey, ReusableJavaCompiler.factory);
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   242
        }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   243
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   244
        void clear() {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   245
            drop(Arguments.argsKey);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   246
            drop(DiagnosticListener.class);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   247
            drop(Log.outKey);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   248
            drop(Log.errKey);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   249
            drop(JavaFileManager.class);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   250
            drop(JavacTask.class);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   251
            drop(JavacTrees.class);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   252
            drop(JavacElements.class);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   253
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   254
            if (ht.get(Log.logKey) instanceof ReusableLog) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   255
                //log already inited - not first round
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   256
                ((ReusableLog)Log.instance(this)).clear();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   257
                Enter.instance(this).newRound();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   258
                ((ReusableJavaCompiler)ReusableJavaCompiler.instance(this)).clear();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   259
                Types.instance(this).newRound();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   260
                Check.instance(this).newRound();
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 51045
diff changeset
   261
                Check.instance(this).clear(); //clear mandatory warning handlers
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 51045
diff changeset
   262
                Preview.instance(this).clear(); //clear mandatory warning handlers
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   263
                Modules.instance(this).newRound();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   264
                Annotate.instance(this).newRound();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   265
                CompileStates.instance(this).clear();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   266
                MultiTaskListener.instance(this).clear();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   267
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   268
                //find if any of the roots have redefined java.* classes
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   269
                Symtab syms = Symtab.instance(this);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   270
                pollutionScanner.scan(roots, syms);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   271
                roots.clear();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   272
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   273
        }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   274
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   275
        /**
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   276
         * This scanner detects as to whether the shared context has been polluted. This happens
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   277
         * whenever a compiled program redefines a core class (in 'java.*' package) or when
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   278
         * (typically because of cyclic inheritance) the symbol kind of a core class has been touched.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   279
         */
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   280
        TreeScanner<Void, Symtab> pollutionScanner = new TreeScanner<Void, Symtab>() {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   281
            @Override @DefinedBy(Api.COMPILER_TREE)
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   282
            public Void visitClass(ClassTree node, Symtab syms) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   283
                Symbol sym = ((JCClassDecl)node).sym;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   284
                if (sym != null) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   285
                    syms.removeClass(sym.packge().modle, sym.flatName());
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   286
                    Type sup = supertype(sym);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   287
                    if (isCoreClass(sym) ||
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   288
                            (sup != null && isCoreClass(sup.tsym) && sup.tsym.kind != Kinds.Kind.TYP)) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   289
                        polluted = true;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   290
                    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   291
                }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   292
                return super.visitClass(node, syms);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   293
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   294
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   295
            private boolean isCoreClass(Symbol s) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   296
                return s.flatName().toString().startsWith("java.");
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   297
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   298
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   299
            private Type supertype(Symbol s) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   300
                if (s.type == null ||
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   301
                        !s.type.hasTag(TypeTag.CLASS)) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   302
                    return null;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   303
                } else {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   304
                    ClassType ct = (ClassType)s.type;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   305
                    return ct.supertype_field;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   306
                }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   307
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   308
        };
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   309
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   310
        @Override @DefinedBy(Api.COMPILER_TREE)
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   311
        public void finished(TaskEvent e) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   312
            if (e.getKind() == Kind.PARSE) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   313
                roots.add(e.getCompilationUnit());
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   314
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   315
        }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   316
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   317
        @Override @DefinedBy(Api.COMPILER_TREE)
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   318
        public void started(TaskEvent e) {
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   319
            //do nothing
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   320
        }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   321
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   322
        <T> void drop(Key<T> k) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   323
            ht.remove(k);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   324
        }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   325
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   326
        <T> void drop(Class<T> c) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   327
            ht.remove(key(c));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   328
        }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   329
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   330
        /**
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   331
         * Reusable JavaCompiler; exposes a method to clean up the component from leftovers associated with
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   332
         * previous compilations.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   333
         */
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   334
        static class ReusableJavaCompiler extends JavaCompiler {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   335
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   336
            final static Factory<JavaCompiler> factory = ReusableJavaCompiler::new;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   337
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   338
            ReusableJavaCompiler(Context context) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   339
                super(context);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   340
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   341
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   342
            @Override
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   343
            public void close() {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   344
                //do nothing
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   345
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   346
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   347
            void clear() {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   348
                newRound();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   349
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   351
            @Override
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   352
            protected void checkReusable() {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   353
                //do nothing - it's ok to reuse the compiler
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   354
            }
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   355
        }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   356
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   357
        /**
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   358
         * Reusable Log; exposes a method to clean up the component from leftovers associated with
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   359
         * previous compilations.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   360
         */
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   361
        static class ReusableLog extends Log {
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   362
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   363
            final static Factory<Log> factory = ReusableLog::new;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   364
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   365
            Context context;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   366
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   367
            ReusableLog(Context context) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   368
                super(context);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   369
                this.context = context;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   370
            }
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   371
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   372
            void clear() {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   373
                recorded.clear();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   374
                sourceMap.clear();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   375
                nerrors = 0;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   376
                nwarnings = 0;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   377
                //Set a fake listener that will lazily lookup the context for the 'real' listener. Since
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   378
                //this field is never updated when a new task is created, we cannot simply reset the field
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   379
                //or keep old value. This is a hack to workaround the limitations in the current infrastructure.
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   380
                diagListener = new DiagnosticListener<JavaFileObject>() {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   381
                    DiagnosticListener<JavaFileObject> cachedListener;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   382
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   383
                    @Override  @DefinedBy(Api.COMPILER)
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   384
                    @SuppressWarnings("unchecked")
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   385
                    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   386
                        if (cachedListener == null) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   387
                            cachedListener = context.get(DiagnosticListener.class);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   388
                        }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   389
                        cachedListener.report(diagnostic);
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   390
                    }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   391
                };
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47216
diff changeset
   392
            }
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   393
        }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   394
    }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   395
}