Skip to content

Commit 2555985

Browse files
impl: avoid loading noopclasses (#201)
Tested with the Benchmark allowed to load the java7/9 packages ``` BEFORE Benchmark Mode Cnt Score Error Units ClassInitBenchmark.forName_init ss 400 8730.958 ± 142.833 us/op ClassInitBenchmark.forName_init:·class.load ss 400 1.166 ± 0.015 classes/sec ClassInitBenchmark.forName_init:·class.load.norm ss 400 72.105 ± 0.112 classes/op ClassInitBenchmark.forName_init:·class.unload ss 400 ≈ 0 classes/sec ClassInitBenchmark.forName_init:·class.unload.norm ss 400 ≈ 0 classes/op ClassInitBenchmark.forName_noinit ss 400 3040.308 ± 42.065 us/op ClassInitBenchmark.forName_noinit:·class.load ss 400 0.605 ± 0.006 classes/sec ClassInitBenchmark.forName_noinit:·class.load.norm ss 400 57.845 ± 0.096 classes/op ClassInitBenchmark.forName_noinit:·class.unload ss 400 ≈ 0 classes/sec ClassInitBenchmark.forName_noinit:·class.unload.norm ss 400 ≈ 0 classes/op AFTER Benchmark Mode Cnt Score Error Units ClassInitBenchmark.forName_init ss 400 8411.036 ± 148.305 us/op ClassInitBenchmark.forName_init:·class.load ss 400 1.136 ± 0.016 classes/sec ClassInitBenchmark.forName_init:·class.load.norm ss 400 71.040 ± 0.113 classes/op ClassInitBenchmark.forName_init:·class.unload ss 400 ≈ 0 classes/sec ClassInitBenchmark.forName_init:·class.unload.norm ss 400 ≈ 0 classes/op ClassInitBenchmark.forName_noinit ss 400 3077.689 ± 52.719 us/op ClassInitBenchmark.forName_noinit:·class.load ss 400 0.612 ± 0.007 classes/sec ClassInitBenchmark.forName_noinit:·class.load.norm ss 400 57.815 ± 0.093 classes/op ClassInitBenchmark.forName_noinit:·class.unload ss 400 ≈ 0 classes/sec ClassInitBenchmark.forName_noinit:·class.unload.norm ss 400 ≈ 0 classes/op ```
1 parent b66605a commit 2555985

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

impl/src/main/java/io/perfmark/impl/SecretPerfMarkImpl.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ public static final class PerfMarkImpl extends Impl {
7777
problems[2] = t;
7878
}
7979
}
80-
if (gen == null) {
81-
generator = new NoopGenerator();
82-
} else {
80+
if (gen != null) {
8381
generator = gen;
82+
} else {
83+
// This magic incantation avoids loading the NoopGenerator class. When PerfMarkImpl is
84+
// being verified, the JVM needs to load NoopGenerator to see that it actually is a
85+
// Generator. By doing a cast here, Java pushes the verification to when this branch is
86+
// actually taken, which is uncommon. Avoid reflectively loading the class, which may
87+
// make binary shrinkers drop the NoopGenerator class.
88+
generator = (Generator) (Object) new NoopGenerator();
8489
}
8590

8691
boolean startEnabled = false;

impl/src/main/java/io/perfmark/impl/Storage.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,15 @@ public final class Storage {
9090
problems[2] = t;
9191
}
9292
}
93-
if (provider == null) {
94-
markRecorderProvider = new NoopMarkRecorderProvider();
95-
} else {
93+
if (provider != null) {
9694
markRecorderProvider = provider;
95+
} else {
96+
// This magic incantation avoids loading the NoopMarkRecorderProvider class. When Storage
97+
// is being verified, the JVM needs to load NoopMarkRecorderProvider to see that it actually
98+
// is a MarkRecorderProvider. By doing a cast here, Java pushes the verification to when
99+
// this branch is actually taken, which is uncommon. Avoid reflectively loading the class,
100+
// which may make binary shrinkers drop the NoopMarkRecorderProvider class.
101+
markRecorderProvider = (MarkRecorderProvider) (Object) new NoopMarkRecorderProvider();
97102
}
98103
try {
99104
if (Boolean.getBoolean("io.perfmark.PerfMark.debug")) {

0 commit comments

Comments
 (0)