Skip to content

Commit 8468001

Browse files
committed
8263452: Javac slow compilation due to algorithmic complexity
Reviewed-by: vromero, jfranck
1 parent 67cb22a commit 8468001

File tree

4 files changed

+107
-6
lines changed

4 files changed

+107
-6
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ public static EnumSet<Flag> asFlagSet(long flags) {
176176

177177
/** Flag for synthesized default constructors of anonymous classes.
178178
*/
179-
public static final int ANONCONSTR = 1<<29;
179+
public static final int ANONCONSTR = 1<<29; //non-class members
180+
181+
/**
182+
* Flag to indicate the super classes of this ClassSymbol has been attributed.
183+
*/
184+
public static final int SUPER_OWNER_ATTRIBUTED = 1<<29; //ClassSymbols
180185

181186
/** Flag for class symbols to indicate it has been checked and found
182187
* acyclic.
@@ -380,6 +385,7 @@ public static EnumSet<Flag> asFlagSet(long flags) {
380385
*/
381386
public static final long NON_SEALED = 1L<<63; // ClassSymbols
382387

388+
383389
/** Modifier masks.
384390
*/
385391
public static final int

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5098,14 +5098,17 @@ void attribClass(ClassSymbol c) throws CompletionFailure {
50985098
chk.checkNonCyclic(null, c.type);
50995099

51005100
Type st = types.supertype(c.type);
5101-
if ((c.flags_field & Flags.COMPOUND) == 0) {
5101+
if ((c.flags_field & Flags.COMPOUND) == 0 &&
5102+
(c.flags_field & Flags.SUPER_OWNER_ATTRIBUTED) == 0) {
51025103
// First, attribute superclass.
51035104
if (st.hasTag(CLASS))
51045105
attribClass((ClassSymbol)st.tsym);
51055106

51065107
// Next attribute owner, if it is a class.
51075108
if (c.owner.kind == TYP && c.owner.type.hasTag(CLASS))
51085109
attribClass((ClassSymbol)c.owner);
5110+
5111+
c.flags_field |= Flags.SUPER_OWNER_ATTRIBUTED;
51095112
}
51105113

51115114
// The previous operations might have attributed the current class

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,7 +2247,7 @@ void checkNonCyclicDecl(JCClassDecl tree) {
22472247

22482248
class CycleChecker extends TreeScanner {
22492249

2250-
List<Symbol> seenClasses = List.nil();
2250+
Set<Symbol> seenClasses = new HashSet<>();
22512251
boolean errorFound = false;
22522252
boolean partialCheck = false;
22532253

@@ -2266,7 +2266,7 @@ private void checkSymbol(DiagnosticPosition pos, Symbol sym) {
22662266
} else if (sym.kind == TYP) {
22672267
checkClass(pos, sym, List.nil());
22682268
}
2269-
} else {
2269+
} else if (sym == null || sym.kind != PCK) {
22702270
//not completed yet
22712271
partialCheck = true;
22722272
}
@@ -2315,7 +2315,7 @@ void checkClass(DiagnosticPosition pos, Symbol c, List<JCTree> supertypes) {
23152315
noteCyclic(pos, (ClassSymbol)c);
23162316
} else if (!c.type.isErroneous()) {
23172317
try {
2318-
seenClasses = seenClasses.prepend(c);
2318+
seenClasses.add(c);
23192319
if (c.type.hasTag(CLASS)) {
23202320
if (supertypes.nonEmpty()) {
23212321
scan(supertypes);
@@ -2338,7 +2338,7 @@ void checkClass(DiagnosticPosition pos, Symbol c, List<JCTree> supertypes) {
23382338
}
23392339
}
23402340
} finally {
2341-
seenClasses = seenClasses.tail;
2341+
seenClasses.remove(c);
23422342
}
23432343
}
23442344
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8263452
27+
* @summary Verify javac does not need a long time to process sources with deep class nesting
28+
* and deep inheritance hierarchies.
29+
* @modules jdk.compiler
30+
*/
31+
32+
import java.util.Arrays;
33+
import javax.tools.JavaCompiler;
34+
import javax.tools.ToolProvider;
35+
36+
import com.sun.source.util.JavacTask;
37+
import java.io.IOException;
38+
import java.net.URI;
39+
import javax.tools.JavaFileObject;
40+
import javax.tools.SimpleJavaFileObject;
41+
42+
public class SuperClassAndNesting {
43+
44+
private static final int SIZE = 100;
45+
46+
public static void main(String... args) throws IOException {
47+
new SuperClassAndNesting().run();
48+
}
49+
50+
void run() throws IOException {
51+
compileTestClass(generateTestClass(SIZE));
52+
}
53+
54+
String generateTestClass(int depth) {
55+
StringBuilder clazz = new StringBuilder();
56+
clazz.append("""
57+
class Test {
58+
class T0 extends java.util.ArrayList {
59+
""");
60+
for (int i = 1; i < depth; i++) {
61+
clazz.append("class T" + i + " extends T" + (i - 1) + " {\n");
62+
}
63+
for (int i = 0; i < depth; i++) {
64+
clazz.append("}\n");
65+
}
66+
clazz.append("}\n");
67+
return clazz.toString();
68+
}
69+
70+
void compileTestClass(String code) throws IOException {
71+
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
72+
assert tool != null;
73+
74+
JavacTask ct = (JavacTask) tool.getTask(null, null, null,
75+
null, null, Arrays.asList(new MyFileObject(code)));
76+
ct.analyze();
77+
}
78+
79+
static class MyFileObject extends SimpleJavaFileObject {
80+
private final String text;
81+
82+
public MyFileObject(String text) {
83+
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
84+
this.text = text;
85+
}
86+
87+
@Override
88+
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
89+
return text;
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)