Created
September 23, 2022 19:50
-
-
Save navyxliu/1bbb22f3894ca48fe278d76c30aa1244 to your computer and use it in GitHub Desktop.
PartialEATest.java
This file contains 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
package com.amazon.jdkteam; | |
import org.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.infra.Blackhole; | |
import org.openjdk.jmh.profile.GCProfiler; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.RunnerException; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import java.util.concurrent.TimeUnit; | |
// derived from Vladimir Dolzhenko's | |
// https://gist.github.com/vladimirdolzhenko/d85789232d289f72607bebac88d43ab2 | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@Fork(1) | |
@Warmup(iterations = 10, time = 5000, timeUnit = TimeUnit.MILLISECONDS) | |
@Measurement(iterations = 5, time = 5000, timeUnit = TimeUnit.MILLISECONDS) | |
@State(Scope.Benchmark) | |
public class PartialEATest { | |
@Param(value = {"-1", "1"}) | |
private int value; | |
@Param(value = {"10"}) | |
private int param; | |
@Benchmark | |
public void allocate(Blackhole bh) { | |
checkArg(bh, value > 0, "expected non-negative value: %s, %s", value, 1000, "A", 700); | |
} | |
private static void checkArg(Blackhole bh, boolean cond, String msg, Object ... args){ | |
if (!cond){ | |
bh.consume(String.format(msg, args)); | |
} | |
} | |
// 1/param odds to have escaped object -- Object[] args | |
// partial escape analysis requires to inline 'checkArg' | |
// when param == 10, | |
// gc.alloc.rate.norm of cond_allocate() expects to be 2x than allocate() with value == -1. | |
// because cond == false twice. | |
// | |
// we see that c2 reports 4x allocation | |
// PartialEATest.allocate:·gc.alloc.rate.norm 10 -1 avgt 5 608.049 ± 0.002 B/op^ | |
// PartialEATest.cond_allocate:·gc.alloc.rate.norm 10 -1 avgt 5 2320.179 ± 0.005 B/op | |
@Benchmark | |
public void cond_allocate(Blackhole bh) { | |
for (int i = 0; i < 20; ++i) { | |
checkArg(bh, (i % param) != 0, "expected non-negative value: %s, %s", param, 1000, "A", 700); | |
} | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.include(PartialEATest.class.getSimpleName()) | |
.addProfiler(GCProfiler.class) | |
.build(); | |
new Runner(opt).run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following graph is from 'After Partial Escape Analysis'.
The VLA
args
is materialized by271 Alloc
, which is a CommitAllocationNodeIt's worth noting that
271 Alloc
is dominated by the IFTrue branch of46 If
. It means that allocation only takes place when19 ==
is true. The shortcut path(IFFalse) gets away without allocation! The code motion is attributed to Graal's PEA.