Skip to content

Commit c40bfab

Browse files
perf: cache javac target to bytecode mapping
Extracts the static mapping in translateJavacTargetToTargetBytecode to a private static final field initialized once in a static block. This avoids repeated object allocation and map population on every method call. Co-authored-by: keeganwitt <64612+keeganwitt@users.noreply.github.com>
1 parent d5a3f18 commit c40bfab

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

src/main/java/org/codehaus/gmavenplus/util/GroovyCompiler.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.lang.reflect.InvocationTargetException;
1717
import java.lang.reflect.Method;
1818
import java.security.CodeSource;
19+
import java.util.Collections;
1920
import java.util.HashMap;
2021
import java.util.List;
2122
import java.util.Map;
@@ -250,6 +251,21 @@ public class GroovyCompiler {
250251
*/
251252
protected static final Version JAVA_12 = new Version(12);
252253

254+
/**
255+
* The mapping of javac target to Groovy target bytecode.
256+
*/
257+
private static final Map<String, String> JAVAC_TARGET_TO_TARGET_BYTECODE;
258+
259+
static {
260+
Map<String, String> javacTargetToTargetBytecode = new HashMap<>();
261+
javacTargetToTargetBytecode.put("5", "1.5");
262+
javacTargetToTargetBytecode.put("6", "1.6");
263+
javacTargetToTargetBytecode.put("7", "1.7");
264+
javacTargetToTargetBytecode.put("8", "1.8");
265+
javacTargetToTargetBytecode.put("1.9", "9");
266+
JAVAC_TARGET_TO_TARGET_BYTECODE = Collections.unmodifiableMap(javacTargetToTargetBytecode);
267+
}
268+
253269
private final ClassWrangler classWrangler;
254270
private final Log log;
255271

@@ -758,13 +774,7 @@ protected void verifyGroovyVersionSupportsTargetBytecode(String targetBytecode)
758774
}
759775

760776
public static String translateJavacTargetToTargetBytecode(String targetBytecode) {
761-
Map<String, String> javacTargetToTargetBytecode = new HashMap<>();
762-
javacTargetToTargetBytecode.put("5", "1.5");
763-
javacTargetToTargetBytecode.put("6", "1.6");
764-
javacTargetToTargetBytecode.put("7", "1.7");
765-
javacTargetToTargetBytecode.put("8", "1.8");
766-
javacTargetToTargetBytecode.put("1.9", "9");
767-
return javacTargetToTargetBytecode.getOrDefault(targetBytecode, targetBytecode);
777+
return JAVAC_TARGET_TO_TARGET_BYTECODE.getOrDefault(targetBytecode, targetBytecode);
768778
}
769779

770780
protected boolean isJavaSupportIndy() {

0 commit comments

Comments
 (0)