Skip to content

Commit af5e532

Browse files
committed
8342806: Desugar capturing lambda in StringNameTable
Reviewed-by: mcimadamore, liach
1 parent 01b681c commit af5e532

File tree

2 files changed

+155
-2
lines changed

2 files changed

+155
-2
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/util/StringNameTable.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -62,7 +62,15 @@ public StringNameTable(Names names, int initialCapacity, boolean intern) {
6262

6363
@Override
6464
public Name fromString(String string) {
65-
return this.nameMap.computeIfAbsent(string, s -> new NameImpl(this, intern ? s.intern() : s));
65+
Name name = nameMap.get(string);
66+
if (name == null) {
67+
if (intern) {
68+
string = string.intern();
69+
}
70+
name = new NameImpl(this, string);
71+
nameMap.put(string, name);
72+
}
73+
return name;
6674
}
6775

6876
@Override
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (c) 2024, 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+
package org.openjdk.bench.javax.tools;
24+
25+
import org.openjdk.jmh.annotations.Benchmark;
26+
import org.openjdk.jmh.annotations.BenchmarkMode;
27+
import org.openjdk.jmh.annotations.Fork;
28+
import org.openjdk.jmh.annotations.Measurement;
29+
import org.openjdk.jmh.annotations.Mode;
30+
import org.openjdk.jmh.annotations.OutputTimeUnit;
31+
import org.openjdk.jmh.annotations.Scope;
32+
import org.openjdk.jmh.annotations.Setup;
33+
import org.openjdk.jmh.annotations.State;
34+
import org.openjdk.jmh.annotations.TearDown;
35+
import org.openjdk.jmh.annotations.Warmup;
36+
37+
import javax.tools.JavaCompiler;
38+
import javax.tools.SimpleJavaFileObject;
39+
import javax.tools.StandardJavaFileManager;
40+
import javax.tools.StandardLocation;
41+
import javax.tools.ToolProvider;
42+
import java.io.IOException;
43+
import java.io.File;
44+
import java.net.URI;
45+
import java.nio.file.Files;
46+
import java.util.ArrayList;
47+
import java.util.Collections;
48+
import java.util.List;
49+
import java.util.concurrent.TimeUnit;
50+
51+
@BenchmarkMode(Mode.AverageTime)
52+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
53+
@State(Scope.Thread)
54+
@Warmup(iterations = 5, time = 3, timeUnit = TimeUnit.SECONDS)
55+
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
56+
@Fork(value = 2, jvmArgs = "-Xmx1g")
57+
public class JavacNameTable {
58+
59+
private List<JavaSourceFromString> compilationUnits;
60+
private JavaCompiler compiler;
61+
private StandardJavaFileManager fileManager;
62+
private File classDir;
63+
64+
@Setup
65+
public void prepare() throws IOException {
66+
67+
// Create a source file with lots of names
68+
StringBuilder buf = new StringBuilder();
69+
buf.append("class BigSource {\n");
70+
for (int i = 0; i < 20000; i++) {
71+
buf.append(String.format(
72+
//"final String name%05d = \"some text #%5d\";\n", i, i));
73+
"String name%05d;\n", i, i));
74+
}
75+
buf.append("}\n");
76+
String bigSource = buf.toString();
77+
78+
compiler = ToolProvider.getSystemJavaCompiler();
79+
80+
fileManager = compiler.getStandardFileManager(null, null, null);
81+
classDir = Files.createTempDirectory(
82+
JavacNameTable.class.getName()).toFile();
83+
fileManager.setLocation(StandardLocation.CLASS_OUTPUT,
84+
Collections.singleton(classDir));
85+
86+
compilationUnits = new ArrayList<>();
87+
compilationUnits.add(new JavaSourceFromString("BigSource", bigSource));
88+
}
89+
90+
@TearDown
91+
public void tearDown() {
92+
for (File f : classDir.listFiles()) {
93+
if (f.isFile()) {
94+
f.delete();
95+
} else {
96+
throw new IllegalStateException("Unexpected non-file: " + f);
97+
}
98+
}
99+
classDir.delete();
100+
}
101+
102+
@Benchmark
103+
public Boolean testSharedTable() throws Exception {
104+
return testCompile(null);
105+
}
106+
107+
@Benchmark
108+
public Boolean testUnsharedTable() throws Exception {
109+
return testCompile("-XDuseUnsharedTable=true");
110+
}
111+
112+
@Benchmark
113+
public Boolean testStringTable() throws Exception {
114+
return testCompile("-XDuseStringTable=true");
115+
}
116+
117+
@Benchmark
118+
public Boolean testInternStringTable() throws Exception {
119+
return testCompile("-XDinternStringTable=true");
120+
}
121+
122+
public Boolean testCompile(String flag) throws Exception {
123+
final List<String> options = flag != null ?
124+
Collections.singletonList(flag) : null;
125+
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager,
126+
null, options, null, compilationUnits);
127+
return task.call();
128+
}
129+
130+
private static class JavaSourceFromString extends SimpleJavaFileObject {
131+
132+
private final String code;
133+
134+
JavaSourceFromString(String name, String code) {
135+
super(URI.create("string:///"
136+
+ name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
137+
this.code = code;
138+
}
139+
140+
@Override
141+
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
142+
return code;
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)