forked from typetools/checker-framework
-
Notifications
You must be signed in to change notification settings - Fork 29
investigation: repeated NewClassTree visits in Nullness checker #1345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ac5e60f
perf: add tree processing cache to avoid redundant computations
zyf265600 6caaa38
Add fast-path in NullnessPropagationTreeAnnotator.visitNewClass: retu…
zyf265600 e8671e9
Merge branch 'master' into performance-optimize-001
zyf265600 da837a3
performance comparison
zyf265600 5bf54a6
remove redundant changes and files
zyf265600 588ed47
refine code with Formatter
zyf265600 4bb4e8d
Merge branch 'master' into performance-optimize-001
wmdietl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,285 @@ | ||
| /* | ||
| * @test | ||
| * @summary Measure impact of skipping hasEffectiveAnnotation(NONNULL) fast-path on many `new` sites. | ||
| * | ||
| * @run main/timeout=600 NewClassPerf | ||
| */ | ||
|
|
||
| import java.io.BufferedWriter; | ||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.Formatter; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
|
|
||
| public class NewClassPerf { | ||
|
|
||
| private static final int RUNS = Integer.getInteger("perf.runs", 10); | ||
| private static final int GROUPS = Integer.getInteger("perf.groups", 400); // ~2k new-exprs total (5 per group) | ||
| // Protocol: AB | BA | BOTH | SEPARATE. Default BOTH runs interleaved AB and BA to cancel order bias. | ||
| private static final String PROTOCOL = System.getProperty("perf.protocol", "BOTH"); | ||
| private static final int WARMUP = Integer.getInteger("perf.warmupPerVariant", 1); | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| Path workDir = Paths.get(".").toAbsolutePath().normalize(); | ||
| Path src = workDir.resolve("ManyNew.java"); | ||
| writeManyNewSource(src, GROUPS); | ||
|
|
||
| switch (PROTOCOL.toUpperCase()) { | ||
| case "AB": { | ||
| Result[] ab = timeInterleaved(src, "AB"); | ||
| printResults("Interleaved AB", ab[0], ab[1]); | ||
| break; | ||
| } | ||
| case "BA": { | ||
| Result[] ba = timeInterleaved(src, "BA"); | ||
| printResults("Interleaved BA", ba[0], ba[1]); | ||
| break; | ||
| } | ||
| case "SEPARATE": { | ||
| Result[] sep = timeSeparate(src, WARMUP); | ||
| printResults("Separate (warmup=" + WARMUP + ")", sep[0], sep[1]); | ||
| break; | ||
| } | ||
| case "BOTH": | ||
| default: { | ||
| Result[] ab = timeInterleaved(src, "AB"); | ||
| Result[] ba = timeInterleaved(src, "BA"); | ||
| System.out.println("==== Interleaved AB ===="); | ||
| printResults("AB", ab[0], ab[1]); | ||
| System.out.println(); | ||
| System.out.println("==== Interleaved BA ===="); | ||
| printResults("BA", ba[0], ba[1]); | ||
| // Consistency check | ||
| double sign1 = Math.signum((ab[1].median() - ab[0].median())); | ||
| double sign2 = Math.signum((ba[1].median() - ba[0].median())); | ||
| System.out.println(); | ||
| if (sign1 == 0 || sign2 == 0 || Math.signum(sign1) != Math.signum(sign2)) { | ||
| System.out.println("Direction: ORDER-SENSITIVE (results differ between AB and BA)"); | ||
| } else { | ||
| System.out.println("Direction: CONSISTENT across AB and BA"); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void writeManyNewSource(Path file, int groups) throws IOException { | ||
| StringBuilder sb = new StringBuilder(1024 * 1024); | ||
| try (Formatter fmt = new Formatter(sb, Locale.ROOT)) { | ||
| fmt.format("import java.util.*;%n"); | ||
| fmt.format("public class ManyNew {%n"); | ||
| // Avoid initialization checker errors; use @Nullable type variable upper bound. | ||
| fmt.format(" static class Box<T extends Object> { T t; Box(){ this.t = (T) (Object) new Object(); } }%n"); | ||
| fmt.format(" void f() {%n"); | ||
| for (int i = 0; i < groups; i++) { | ||
| fmt.format(" Object o%d = new Object();%n", i); | ||
| fmt.format(" ArrayList<String> l%d = new ArrayList<>();%n", i); | ||
| fmt.format(" Box<String> b%d = new Box<>();%n", i); | ||
| fmt.format(" int[] ai%d = new int[10];%n", i); | ||
| fmt.format(" String[] as%d = new String[10];%n", i); | ||
| fmt.format(" Runnable r%d = new Runnable(){ public void run(){} };%n", i); | ||
| } | ||
| fmt.format(" }%n"); | ||
| fmt.format("}%n"); | ||
| } | ||
| try (BufferedWriter w = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) { | ||
| w.write(sb.toString()); | ||
| } | ||
| } | ||
|
|
||
| private static Result timeVariant(Path src, boolean skipFastPath) throws Exception { | ||
| List<Long> timings = new ArrayList<>(); | ||
| for (int i = 0; i < RUNS; i++) { | ||
| timings.add(runOnceMs(src, skipFastPath)); | ||
| } | ||
| return new Result(timings); | ||
| } | ||
|
|
||
| private static Result[] timeInterleaved(Path src, String order) throws Exception { | ||
| List<Long> aTimes = new ArrayList<>(); | ||
| List<Long> bTimes = new ArrayList<>(); | ||
| boolean firstA = !"BA".equalsIgnoreCase(order); | ||
| for (int i = 0; i < RUNS; i++) { | ||
| if (firstA) { | ||
| aTimes.add(runOnceMs(src, /*skipFastPath=*/false)); | ||
| bTimes.add(runOnceMs(src, /*skipFastPath=*/true)); | ||
| } else { | ||
| bTimes.add(runOnceMs(src, /*skipFastPath=*/true)); | ||
| aTimes.add(runOnceMs(src, /*skipFastPath=*/false)); | ||
| } | ||
| } | ||
| return new Result[] { new Result(aTimes), new Result(bTimes) }; | ||
| } | ||
|
|
||
| private static long runOnceMs(Path src, boolean skipFastPath) throws Exception { | ||
| long start = System.nanoTime(); | ||
| int exit = runJavac(src, skipFastPath); | ||
| long end = System.nanoTime(); | ||
| if (exit != 0) { | ||
| throw new RuntimeException("javac failed with exit code " + exit); | ||
| } | ||
| return (end - start) / 1_000_000L; | ||
| } | ||
|
|
||
| private static Result[] timeSeparate(Path src, int warmup) throws Exception { | ||
| // Variant A (fast-path enabled) | ||
| for (int i = 0; i < warmup; i++) { | ||
| runOnceMs(src, /*skipFastPath=*/false); | ||
| } | ||
| List<Long> aTimes = new ArrayList<>(); | ||
| for (int i = 0; i < RUNS; i++) { | ||
| aTimes.add(runOnceMs(src, /*skipFastPath=*/false)); | ||
| } | ||
|
|
||
| // Variant B (fast-path disabled) | ||
| for (int i = 0; i < warmup; i++) { | ||
| runOnceMs(src, /*skipFastPath=*/true); | ||
| } | ||
| List<Long> bTimes = new ArrayList<>(); | ||
| for (int i = 0; i < RUNS; i++) { | ||
| bTimes.add(runOnceMs(src, /*skipFastPath=*/true)); | ||
| } | ||
| return new Result[] { new Result(aTimes), new Result(bTimes) }; | ||
| } | ||
|
|
||
| private static void printResults(String label, Result a, Result b) { | ||
| // Print raw timings | ||
| System.out.println("Variant A (fast-path enabled) timings ms: " + a.timingsMs); | ||
| System.out.println("Variant B (fast-path disabled) timings ms: " + b.timingsMs); | ||
|
|
||
| // Summary table and deltas | ||
| System.out.println(); | ||
| System.out.println("Results (ms) - " + label + ":"); | ||
| System.out.println("Variant | median | average"); | ||
| System.out.println(String.format("A | %7.2f | %7.2f", a.median(), a.average())); | ||
| System.out.println(String.format("B | %7.2f | %7.2f", b.median(), b.average())); | ||
| double medA = a.median(), medB = b.median(); | ||
| double avgA = a.average(), avgB = b.average(); | ||
| System.out.println(); | ||
| System.out.printf("Median delta (B vs A): %.3f%%%n", (medB - medA) / medA * 100.0); | ||
| System.out.printf("Average delta (B vs A): %.3f%%%n", (avgB - avgA) / avgA * 100.0); | ||
| } | ||
|
|
||
| private static int runJavac(Path src, boolean skipFastPath) throws Exception { | ||
| String checkerJar = locateCheckerJar(); | ||
|
|
||
| List<String> cmd = new ArrayList<>(); | ||
| cmd.add(findJavacExecutable()); | ||
| // Add required --add-opens for running Checker Framework on JDK 9+ | ||
| String[] javacPkgs = new String[] { | ||
| "com.sun.tools.javac.api", | ||
| "com.sun.tools.javac.code", | ||
| "com.sun.tools.javac.comp", | ||
| "com.sun.tools.javac.file", | ||
| "com.sun.tools.javac.main", | ||
| "com.sun.tools.javac.parser", | ||
| "com.sun.tools.javac.processing", | ||
| "com.sun.tools.javac.tree", | ||
| "com.sun.tools.javac.util" | ||
| }; | ||
| for (String p : javacPkgs) { | ||
| cmd.add("-J--add-opens=jdk.compiler/" + p + "=ALL-UNNAMED"); | ||
| } | ||
| if (skipFastPath) { | ||
| cmd.add("-J-Dcf.skipNonnullFastPath=true"); | ||
| } | ||
| cmd.addAll(Arrays.asList( | ||
| "-classpath", checkerJar, | ||
| "-processor", "org.checkerframework.checker.nullness.NullnessChecker", | ||
| "-proc:only", | ||
| "-source", "8", | ||
| "-target", "8", | ||
| "-Xlint:-options", | ||
| src.getFileName().toString())); | ||
|
|
||
| ProcessBuilder pb = new ProcessBuilder(cmd); | ||
| pb.directory(src.getParent().toFile()); | ||
| pb.redirectErrorStream(true); | ||
| Process p = pb.start(); | ||
| // Consume output to avoid buffer blockage. | ||
| byte[] out = p.getInputStream().readAllBytes(); | ||
| int code = p.waitFor(); | ||
| // In case of failure, print compiler output for debugging. | ||
| if (code != 0) { | ||
| System.out.write(out); | ||
| } | ||
| return code; | ||
| } | ||
|
|
||
| private static String locateCheckerJar() { | ||
| try { | ||
| String testRoot = System.getProperty("test.root"); | ||
| if (testRoot != null) { | ||
| Path p = Paths.get(testRoot).toAbsolutePath().normalize().getParent().resolve("dist/checker.jar"); | ||
| if (Files.exists(p)) { | ||
| return p.toString(); | ||
| } | ||
| } | ||
| Path p1 = Paths.get("checker/dist/checker.jar").toAbsolutePath().normalize(); | ||
| if (Files.exists(p1)) { | ||
| return p1.toString(); | ||
| } | ||
| Path p2 = Paths.get("../../../dist/checker.jar").toAbsolutePath().normalize(); | ||
| if (Files.exists(p2)) { | ||
| return p2.toString(); | ||
| } | ||
| // Fallback: walk up from the current working dir (jtreg scratch) | ||
| Path cwd = Paths.get(".").toAbsolutePath().normalize(); | ||
| for (int i = 0; i < 8; i++) { | ||
| Path cand = cwd; | ||
| for (int j = 0; j < i; j++) cand = cand.getParent(); | ||
| if (cand == null) break; | ||
| Path jar = cand.resolve("checker/dist/checker.jar"); | ||
| if (Files.exists(jar)) return jar.toString(); | ||
| } | ||
| } catch (Throwable ignore) { | ||
| } | ||
| return "checker/dist/checker.jar"; | ||
| } | ||
|
|
||
| private static String findJavacExecutable() { | ||
| String javaHome = System.getProperty("java.home"); | ||
| // Typical JDK layout: $JAVA_HOME/bin/javac | ||
| File jh = new File(javaHome); | ||
| File bin = new File(jh.getParentFile(), "bin"); | ||
| File javac = new File(bin, isWindows() ? "javac.exe" : "javac"); | ||
| if (javac.exists()) { | ||
| return javac.getAbsolutePath(); | ||
| } | ||
| // Fallback to PATH | ||
| return isWindows() ? "javac.exe" : "javac"; | ||
| } | ||
|
|
||
| private static boolean isWindows() { | ||
| String os = System.getProperty("os.name", "").toLowerCase(); | ||
| return os.contains("win"); | ||
| } | ||
|
|
||
| private static final class Result { | ||
| final List<Long> timingsMs; | ||
| Result(List<Long> t) { this.timingsMs = Collections.unmodifiableList(new ArrayList<>(t)); } | ||
| double average() { return timingsMs.stream().mapToLong(Long::longValue).average().orElse(0); } | ||
| double median() { | ||
| if (timingsMs.isEmpty()) return 0; | ||
| List<Long> copy = new ArrayList<>(timingsMs); | ||
| Collections.sort(copy); | ||
| int n = copy.size(); | ||
| if ((n & 1) == 1) { | ||
| return copy.get(n/2); | ||
| } else { | ||
| return (copy.get(n/2 - 1) + copy.get(n/2)) / 2.0; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addMissingAnnotationalready checks whether the annotation is present, so this change doesn't achieve anything.Do you actually see any performance differences?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed discussion.
I added a small perf harness to measure the effect of the extra
hasEffectiveAnnotation(NONNULL)fast-path.Test file:
checker/jtreg/nullness/perf/NewClassPerf.javaThis generates a synthetic source with thousands of
newexpressions and compares two variants:if)-Dcf.skipNonnullFastPath=true)How to run:
Results (10×400 runs, fixed 2 GB heap, JDK 17, macOS arm64):
Across both AB and BA interleaved protocols, variant B was consistently as fast or slightly faster (≈2–3% median). In other words, the new
ifdoes not bring a measurable benefit, as you mentionedaddMissingAnnotationalready checks for existing annotations, so the extra condition only adds overhead.Final conclusion: After all these investigations, it shows that the repeated
visitNewClasscalls are expected: multiple sub-checkers walk the same AST. This is not a defect, and at this point we don’t see a straightforward or sound optimization to reduce it. I’ll therefore retitle this PR as an investigation and propose to close it.