From 231893e87feb0165b9aa107e454e70c9794808c5 Mon Sep 17 00:00:00 2001 From: iHiD <286476+iHiD@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:49:44 +0900 Subject: [PATCH] Compile solutions with a native javac built at image build Each run spent most of a second loading javac into the JVM and warming it up in the JIT. The Docker build now compiles javac into a native executable with GraalVM Native Image (bin/build-native-javac.sh). The test runner starts it on the solution and tests as soon as it starts, and parses the test files while it runs. If the native javac fails for any reason, the test runner compiles the files again with the JVM's own javac, as before. That keeps compile error messages identical, and handles code that needs a Java release newer than the native javac's. This takes the median run over the track's 169 example solutions from 1.89s to 1.24s, with identical results. Runs with compile errors take about 0.2s longer. The image grows by about 46 MB. Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- Dockerfile | 11 ++++ bin/build-native-javac.sh | 43 ++++++++++++ src/main/java/com/exercism/TestRunner.java | 12 ++-- .../exercism/compiler/ExerciseCompiler.java | 66 +++++++++++++++++-- 4 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 bin/build-native-javac.sh diff --git a/Dockerfile b/Dockerfile index 4d8d947..036a0c7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,13 @@ WORKDIR /app COPY --chown=gradle:gradle . /app RUN gradle -i --stacktrace clean build +FROM ghcr.io/graalvm/native-image-community:25.0.2@sha256:0d936f32bb8acb5bc60c41b33e05f064d7a6aaf36b726538296c54949bd4a3c0 AS native-javac + +COPY --from=build /app/build/libs/java-test-runner.jar /tmp/java-test-runner.jar +COPY aot-training /tmp/aot-training +COPY bin/build-native-javac.sh /tmp/build-native-javac.sh +RUN sh /tmp/build-native-javac.sh + FROM eclipse-temurin:27_35-jdk@sha256:473d02d477a8152a05dc42fde10b965754f1863d45e586a97763ef298965c48b WORKDIR /opt/test-runner @@ -13,4 +20,8 @@ COPY bin/create-aot-cache.sh bin/create-aot-cache.sh COPY aot-training aot-training RUN sh bin/create-aot-cache.sh +# The native javac is added after the AOT cache is created. That way the training run +# compiles inside the JVM, and the cache holds javac's classes for runs that fall back to it. +COPY --from=native-javac /native-javac native-javac + ENTRYPOINT ["sh", "/opt/test-runner/bin/run.sh"] diff --git a/bin/build-native-javac.sh b/bin/build-native-javac.sh new file mode 100644 index 0000000..267fe0f --- /dev/null +++ b/bin/build-native-javac.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env sh + +# Synopsis: +# Build javac as a native executable with GraalVM Native Image. +# This runs once, in the native-javac stage of the Docker build. + +# Output: +# Writes /native-javac/javac and /native-javac/lib/ct.sym. +# The Dockerfile copies them to /opt/test-runner/native-javac. + +# A native javac starts in a few milliseconds and runs as compiled machine code from +# the start. The javac inside the JVM has to be loaded and warmed up by the JIT on +# every run, which takes most of a second. +# +# A native executable can't load JDK classes from lib/modules, so the native javac +# compiles against the class signatures in lib/ct.sym, selected with --release. +# ExerciseCompiler passes the same --release when it runs the native javac. + +set -e + +release=25 +config_folder="/tmp/native-javac-config" +classes_folder="/tmp/native-javac-classes" + +# javac looks up some classes and resources by name. GraalVM's tracing agent records +# them while javac compiles the sample exercise, so the native executable includes them. +mkdir -p "${classes_folder}" /native-javac +java -agentlib:native-image-agent=config-output-dir="${config_folder}" \ + -m jdk.compiler/com.sun.tools.javac.Main \ + --release "${release}" \ + -classpath /tmp/java-test-runner.jar \ + -d "${classes_folder}" \ + $(find /tmp/aot-training -name '*.java') + +native-image --no-fallback \ + -H:+UnlockExperimentalVMOptions \ + -H:ConfigurationFileDirectories="${config_folder}" \ + -H:IncludeResourceBundles=com.sun.tools.javac.resources.javac,com.sun.tools.javac.resources.compiler,com.sun.tools.javac.resources.ct \ + -m jdk.compiler/com.sun.tools.javac.Main \ + -o /native-javac/javac + +mkdir -p /native-javac/lib +cp "${JAVA_HOME}/lib/ct.sym" /native-javac/lib/ct.sym diff --git a/src/main/java/com/exercism/TestRunner.java b/src/main/java/com/exercism/TestRunner.java index 06c144c..0fdfc98 100644 --- a/src/main/java/com/exercism/TestRunner.java +++ b/src/main/java/com/exercism/TestRunner.java @@ -44,15 +44,15 @@ private void run() throws IOException { var sourceFiles = resolveSourceFiles(); var testFiles = resolveTestFiles(); - for (File testFile : testFiles) { - testParser.parse(testFile); - } - var filesToCompile = Stream.concat(sourceFiles.stream(), testFiles.stream()).toList(); Report report; - try (var compiler = new ExerciseCompiler(slug)) { - compiler.compile(filesToCompile); + try (var compiler = new ExerciseCompiler(slug, filesToCompile)) { + for (File testFile : testFiles) { + testParser.parse(testFile); + } + + compiler.compile(); testRunner.test(compiler.getClassLoader(), compiler.getClasspathRoots()); report = ReportGenerator.generate(testRunner.getTestDetails(), testParser.buildTestCodeMap()); } catch (ExerciseCompilationException e) { diff --git a/src/main/java/com/exercism/compiler/ExerciseCompiler.java b/src/main/java/com/exercism/compiler/ExerciseCompiler.java index 0e81553..7bbab32 100644 --- a/src/main/java/com/exercism/compiler/ExerciseCompiler.java +++ b/src/main/java/com/exercism/compiler/ExerciseCompiler.java @@ -9,25 +9,45 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; public class ExerciseCompiler implements Closeable, Flushable { + // The native javac is built when the Docker image is built (see bin/build-native-javac.sh). + // It compiles against the Java API of the release given here, read from lib/ct.sym. + private static final Path NATIVE_JAVAC_HOME = Path.of("/opt/test-runner/native-javac"); + private static final Path NATIVE_JAVAC = NATIVE_JAVAC_HOME.resolve("javac"); + private static final String NATIVE_JAVAC_RELEASE = "25"; + private final JavaCompiler compiler; private final StandardJavaFileManager fileManager; private final DiagnosticCollector diagnosticCollector; + private final Path outputDir; + private final Collection filesToCompile; + private final Process nativeCompilation; + + public ExerciseCompiler(String exerciseSlug, Collection filesToCompile) throws IOException { + this.filesToCompile = filesToCompile; + this.outputDir = Files.createTempDirectory(String.format("java-test-runner-%s-", exerciseSlug)); + this.nativeCompilation = startNativeCompilation(); - public ExerciseCompiler(String exerciseSlug) throws IOException { this.compiler = ToolProvider.getSystemJavaCompiler(); this.diagnosticCollector = new DiagnosticCollector<>(); this.fileManager = compiler.getStandardFileManager(this.diagnosticCollector, null, null); - - var outputDir = Files.createTempDirectory(String.format("java-test-runner-%s-", exerciseSlug)); this.fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(outputDir.toFile())); } - public void compile(final Collection filesToCompile) throws ExerciseCompilationException { + // The native javac starts compiling straight away, in its own process, while the + // test runner parses the test files. If it fails for any reason, compile() will + // compile the files again with the JVM's own javac. That keeps compile errors + // exactly as before, and handles code that needs a newer Java release. + public void compile() throws ExerciseCompilationException { + if (nativeCompilationSucceeded()) { + return; + } + var sources = fileManager.getJavaFileObjectsFromFiles(filesToCompile); var task = compiler.getTask(null, fileManager, diagnosticCollector, null, null, sources); if (task.call()) { @@ -41,6 +61,44 @@ public void compile(final Collection filesToCompile) throws ExerciseCompil throw new ExerciseCompilationException(messageBuilder.toString()); } + private Process startNativeCompilation() { + if (!Files.isExecutable(NATIVE_JAVAC)) { + return null; + } + + var command = new ArrayList<>(List.of( + NATIVE_JAVAC.toString(), + "-Djava.home=" + NATIVE_JAVAC_HOME, + "--release", NATIVE_JAVAC_RELEASE, + "-classpath", System.getProperty("java.class.path"), + "-d", outputDir.toString())); + for (var file : filesToCompile) { + command.add(file.getPath()); + } + + try { + return new ProcessBuilder(command) + .redirectErrorStream(true) + .redirectOutput(ProcessBuilder.Redirect.DISCARD) + .start(); + } catch (IOException e) { + return null; + } + } + + private boolean nativeCompilationSucceeded() { + if (nativeCompilation == null) { + return false; + } + + try { + return nativeCompilation.waitFor() == 0; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return false; + } + } + public ClassLoader getClassLoader() { return this.fileManager.getClassLoader(StandardLocation.CLASS_OUTPUT); }